第一行包含T,测试数据的组数。后面跟有T行,每行包含一个字符串。
如果可以删去一个字母使它变成回文串,则输出任意一个满足条件的删去字母的位置(下标从0开始)。例如:
bcc
我们可以删掉位置0的b字符。
3 aaab baa aaa
3 0 -1
/*
思路:回文数函数,每次去掉一个数后进行判断
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
for(int i = 0;i<N;i++){
String str = br.readLine();
if(isHui(str)){
System.out.println(-1);
continue;
}else{
for(int j = 0;j<str.length();j++){
if(j==0){
String newstr = str.substring(1);
if(isHui(newstr)){
System.out.println(j);
break;
}
}else if(j== str.length()-1){
String newstr = str.substring(0,j);
if(isHui(newstr)){
System.out.println(j);
break;
}
}else{
String newstr = str.substring(0,j) + str.substring(j+1);
if(isHui(newstr)){
System.out.println(j);
break;
}
}
}
}
}
}
public static boolean isHui(String str){
if(str.length()<=1)return true;
int left = 0,right = str.length()-1;
while(left<right){
if(str.charAt(left) == str.charAt(right)){
left++;
right--;
}else{
return false;
}
}
return true;
}
} import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for(int k = 0;k < n;k++){
findIndex(sc.nextLine());
}
}
public static void findIndex(String str){
int i = 0;
int j = str.length() - 1;
while(i < j){
if(str.charAt(i) != str.charAt(j)){
if(str.charAt(i) != str.charAt(i+1) && str.charAt(j) == str.charAt(i+1)){
System.out.println(i);
return;
}else{
System.out.println(j);
return;
}
}
i++;j--;
}
System.out.println(-1);
}
} 脑子笨,就只能暴力法玩玩了