给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
("回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。)
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。
数据范围:字符串长度满足
输入一个字符串S 例如“aabcb”(1 <= |S| <= 50), |S|表示字符串S的长度。
符合条件的字符串有"a","a","aa","b","c","b","bcb"
所以答案:7
aabcb
7
import java.util.*; /* 利用indexof和substring方法来进行操作 依次把首尾相同的字符串进行回文数判断 */ public class Main{ 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; } public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.next(); int count = str.length(); for(int i = 0;i<str.length()-1;i++){ String substr = str.substring(i+1); while(substr.contains(Character.toString(str.charAt(i)))){ String subsubstr = substr.substring(i,substr.indexOf(str.charAt(i))+1); if(isHui(subsubstr)) count++; substr = substr.substring(substr.indexOf(str.charAt(i))+1); } } System.out.println(count); } }请教下大神们,我这种方法为什么会只有30%,系统后面会报错呢?实在想不明白哪里出问题了,求解答
import java.util.Scanner; /** * 运行时间:45ms * * 占用内存:10548k * */ public class Main { static int count=0; static String s; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); s = scanner.next(); for (int i = 0; i < s.length(); i++) { ////考虑两种情况:aba 和 abba centerSpread(i,i); centerSpread(i,i+1); } System.out.println(count); } static void centerSpread(int i,int j){ while (i>=0&&j<s.length()&&s.charAt(i)==s.charAt(j)){ count++; i--; j++; } } }
public class Main { //蛮力法 public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int n = str.length(); int count = n; for (int i=0;i<n;i++){ for (int j=i+1;j<n;j++){ if (judge(str,i,j)) count++; } } System.out.println(count); } private static boolean judge(String str,int i,int j){ while (i < j){ if (str.charAt(i) != str.charAt(j)) return false; i++;j--; } return true; } }
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int n = str.length(); int count = n; int[][] dp = new int[n][n]; dp[0][0] = 1; for (int j=1;j<n;j++){ dp[j][j] = 1; for (int i=j-1;i>=0;i--){ if (str.charAt(i) == str.charAt(j) && (i+1 > j-1 || dp[i+1][j-1] == 1)){ dp[i][j] = 1; count++; } } } System.out.println(count); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String string = input.nextLine(); // 1.单个字符均是回文串(可合并到2.中) int count = string.length(); // 找到对称轴,向两边同步扩展,可找到所有回文串 // 2.对称轴在元素上面 for (int i = 0; i < string.length(); i++) { int max = i < string.length() / 2 ? i : string.length() - 1 - i; for (int j = 1; j <= max; j++) { if (string.charAt(i - j) == string.charAt(i + j)) count++; else break; } } // 3.对称轴在元素中间 for (int i = 0; i < string.length(); i++) { int max = i < string.length() - i ? i : string.length() - i; for (int j = 1; j <= max; j++) { if (string.charAt(i - j) == string.charAt(i + j - 1)) count++; else break; } } System.out.println(count); } }