题目描述:
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码 进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况 (abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的 有效密码串吗?
代码:
package lanqiao;import java.util.*;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()){String s = sc.nextLine();int max = 1;for(int i = 0;i < s.length();i ++){for(int j = s.length();j > i;j --){String str1 = s.substring(i,j);String str2 = new StringBuffer(str1).reverse().toString();if(str1.equals(str2)){max = max > (j - i) ? max : j - i;break;}}}System.out.println(max);}}
}