Substrings

来源:互联网 发布:轻而易举软件培训视频 编辑:程序博客网 时间:2024/05/19 11:47

Substrings

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string. 

Output

There should be one line per test case containing the length of the largest string found.

Sample Input

23ABCDBCDFFBRCD2roseorchid

Sample Output

22

Author

Asia 2002, Tehran (Iran), Preliminary

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool comp(string str1,string str2){
    return str1.length()<str2.length();
}


string str[112];
int main(){
        int t;
            cin>>t;
        while(t--){
            int n;
                cin>>n;
            int i=0;
            for(i=0;i<n;i++)
                cin>>str[i];
            sort(str,str+n,comp);
            string::iterator p1;
            string::iterator p2;
            string::iterator p0;
            int ans=0,max=0;
            for(p1=str[0].begin();p1<str[0].end();p1++)
                for(p2=str[0].end()-1;p2>=p1;p2--){
                    string ss1=str[0].substr(p1-str[0].begin(),p2-p1+1);
                    string ss2;
                    for(string::iterator it=p2;it>=p1;it--){
                        ss2+=*it;
                    }
                ans=ss1.length();
                int flag=1;
                for(i=1;i<n;i++){
                int x=str[i].find(ss1);
                int y=str[i].find(ss2);
                    if(x==-1&&y==-1){
                    flag=0;
                    break;
                    }
                }
            if(flag==1&&max<=ans)
                max=ans;
            }
          cout<<max<<endl;
        }
    return 0;
}