import java.io.*;
class Main {
static int count = 0;
public static int recursion(String s, int l, int r) {
count++;
if (l >= r) return 1;
else if (s.charAt(l) != s.charAt(r)) return 0;
else return recursion(s, l + 1, r - 1);
}
public static int isPalindrome(String s) {
return recursion(s, 0, s.length() - 1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String s = br.readLine();
int palindrome = isPalindrome(s);
bw.write(palindrome + " " + count);
}
bw.flush();
bw.close();
br.close();
}
}
3
AAA
ABBA
ABABA
1 2
1 5
1 8
-> 값이 누적되는 문제가 발생했다!! 이 문제는 값을 초기화하면 된다.
import java.io.*;
public class Main {
static int count = 0;
public static int recursion(String s, int l, int r) {
count++;
if (l >= r) return 1;
else if (s.charAt(l) != s.charAt(r)) return 0;
else return recursion(s, l + 1, r - 1);
}
public static int isPalindrome(String s) {
return recursion(s, 0, s.length() - 1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
count = 0; // !!각 문자열을 검사하기 전에 count를 0으로 초기화!!
String s = br.readLine();
int palindrome = isPalindrome(s);
bw.write(palindrome + " " + count);
bw.write("\n");
}
bw.flush();
bw.close();
br.close();
}
}