标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8707 Accepted Submission(s): 4046
/*状态AC*/ import java.util.Scanner; public class hdu1238_Substrings { static String[] str = new String[105]; public static void main(String[] args) { Scanner sc =new Scanner (System.in); int tcase = sc.nextInt(); while(tcase-->0){ int Min=1000,k=0; int n =sc.nextInt(); /*找到最小串*/ for(int i=0;i<n;i++){ str[i] = sc.next(); if(str[i].length()<Min) { Min = str[i].length(); k = i; } } int Max = 0; String str1,str2;//子串以及反子串 boolean flag = true; //是否出现过 for(int i=0;i<str[k].length();i++){//枚举子串的头 for(int j=i;j<str[k].length();j++){//子串的尾 str1 = str[k].substring(i,j+1); str2 = reverse(str1); //System.out.println(str1+str2); int len = str1.length(); /*枚举所有串*/ for(int t=0;t<n;t++){ if(str[t].indexOf(str1)==-1&&str[t].indexOf(str2)==-1) {//当正反子串在母串中都未发现时即跳出 flag = false; break; } } if(flag&&len>Max) Max = len; flag = true; } } System.out.println(Max); } } public static String reverse(String s) { char ch[] = s.toCharArray(); int start = 0, end = ch.length - 1; char temp; while (start < end) { temp = ch[start]; ch[start] = ch[end]; ch[end] = temp; start++; end--; } String s1 = String.copyValueOf(ch); return s1; } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5150871.html