给定一个字符串(数字或大小写字母), 找出最长的对称的子串(如有多个,输出任意一个)。
例如:
输入:“abbaad”
输出:“abba”
因为给定一个字符串,只含数字或大小写字母,可以在每相邻字符之间添加'*',就不用分aba和abba两种情况了。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); // 在每相邻两个字符之间添加‘*’ str = str.replaceAll(".", "*$0").substring(1); int maxArea = 0, maxIndex = 0; for (int i = 0; i < str.length(); i++) { int temp = getArea(str, i); if (temp > maxArea) { maxArea = temp; maxIndex = i; } } System.out.println(str.substring(maxIndex - maxArea, maxIndex + maxArea + 1).replace("*", "")); } private static int getArea(String str, int index) { int area = 0; int left = index - 1, right = index + 1; while (left >= 0 && right <= str.length() - 1) { // 向左右两边扩展 if (str.charAt(left--) == str.charAt(right++)) { area++; } else { break; } } return area; } }
import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String symmetryPart = getSymmetryString(input); System.out.println(symmetryPart); } // 有两种对称 // 一种(way1str):baab式; // 一种(way2str):bab式; public static String getSymmetryString(String str) { String way1str = getSymmetryStringWay1(str); String way2str = getSymmetryStringWay2(str); // 谁长选谁 return (way1str.length() > way2str.length()) ? way1str : way2str; } // bab式; public static String getSymmetryStringWay2(String str) { char[] chars = str.toCharArray(); int repeatTime = 0; int centerIndex = 0; for (int i = 0; i < chars.length - 2; i++) { if (chars[i] == chars[i + 2]) { int repeatTemp = getSymmetryQuantityWay2(chars, i + 1); if (repeatTemp > repeatTime) { repeatTime = repeatTemp; centerIndex = i + 1; } } } return str.substring(centerIndex - (repeatTime - 1) / 2, centerIndex + (repeatTime - 1) / 2 + 1); } // baab式; public static String getSymmetryStringWay1(String str) { char[] chars = str.toCharArray(); int repeatTime = 0; int repeatIndex = 0; for (int i = 0; i < chars.length - 1; i++) { if (chars[i] == chars[i + 1]) { int repeatTemp = getSymmetryQuantityWay1(chars, i); if (repeatTemp > repeatTime) { repeatTime = repeatTemp; repeatIndex = i; } } } return str.substring(repeatIndex - repeatTime / 2 + 1, repeatIndex + repeatTime / 2 + 1); } // bab式; private static int getSymmetryQuantityWay2(char[] chars, int i) { int repeat = 1; int indexLeft = i - 1; int indexRight = i + 1; while (chars[indexLeft] == chars[indexRight]) { indexLeft--; indexRight++; repeat += 2; if (indexLeft < 0 || indexRight > chars.length - 1) { return repeat; } } return repeat; } // baab式; private static int getSymmetryQuantityWay1(char[] chars, int i) { int repeat = 0; int indexLeft = i; int indexRight = i + 1; while (chars[indexLeft] == chars[indexRight]) { indexLeft--; indexRight++; repeat += 2; if (indexLeft < 0 || indexRight > chars.length - 1) { return repeat; } } return repeat; } }//开始以为只有abba式,结果不能完全通过,增加了aba式就通过了。代码有很多重复的地方,应该重构一下。
public class Main{ public static boolean ispalindrome(String sub) { for(int i=0; i < sub.length()/2; i++) { if(sub.charAt(i) != sub.charAt(sub.length() - i - 1)) return false; } return true; } public static void main(String[] args) throws IOException { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String str = buf.readLine(); String palin_str = str.substring(0,1); for (int i = 0; i < str.length(); i++) { for (int j = i + 1; j < str.length() + 1; j++) { if (ispalindrome(str.substring(i, j))){ if(str.substring(i, j).length() > palin_str.length()) palin_str = str.substring(i,j); } } } System.out.println(palin_str); } }