import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MyClass {
public static void main(String args[]) {
int counter=0;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // read an integer from input stream.
String s = scanner.next();
char string_array[] = s.toCharArray();
for(int i=0; i<s.length(); i++){
for(int j=i; j<=s.length(); j++){
if(i != j){
String s1 = s.substring(i,j);
String rev = "";
for(int z=0; z<s1.length(); z++){
rev = rev + s1.charAt(s1.length()-1-z);
}
//System.out.println("s1=" + s1 + "rev=" + rev);
int count = s1.compareTo(rev);
if(count == 0){
counter++;
}
}
}
}
System.out.println(counter);
}
答案 0 :(得分:1)
您的算法没有包含对字符相等性特殊回文要求的检查。以下算法添加了这样的检查:
public class MyClass {
public static void main(String args[]) {
int counter=0;
String s = "abcbaba";
char string_array[] = s.toCharArray();
for(int i=0; i<s.length(); i++){
for(int j=i; j<=s.length(); j++){
if(i != j){
String s1 = s.substring(i,j);
String rev = "";
char complianceChar = s1.charAt(0);
boolean isSpecialPalindrom = true;
boolean isLengthEven = s1.length()%2==0;
for(int z=0; z<s1.length(); z++) {
char c = s1.charAt(s1.length()-1-z);
rev = rev + c;
boolean isMiddleChar = (z == (s1.length()-1)/2);
if(isSpecialPalindrom && (isLengthEven || !isMiddleChar)) {
isSpecialPalindrom = (c == complianceChar);
}
}
int count = s1.compareTo(rev);
if(count == 0 && isSpecialPalindrom){
System.out.println(rev);
counter++;
}
}
}
}
System.out.println(counter);
}
}
输出为:
a
b
bcb
c
b
bab
a
aba
b
a
10