HDU-1238 Substrings stl的运用

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
2
3
ABCD
BCDFF
BRCD
2
rose
orchid
Sample Output
2
2
题意:
求每组给定字符串的公共子串的最长大小。
解题思路:
数据量小,直接枚举最小字符串的每个子串,依次匹配,注意STL的运用。
代码:

#include
#include
#include
#include
using namespace std;
bool cmp(string x,string y){return x.size()>y.size();}
string::size_type idx,id;
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		int m,maxx=0,flag=1;
		scanf("%d",&m);
		vector<string>q;
		for(int i=0;i<m;++i){
			string s;
			cin>>s;
			q.push_back(s);
		}
		sort(q.begin(),q.end(),cmp);		//其实没必要,只是试一下自定义排序
		string s=q[0];
		int len=s.size();
		for(int i=0;i<len;++i)
			for(int l=len;l>0;--l){
				flag=1;
				for(int j=1;j<m;++j){
					string ss=s.substr(i,l);
					string sss=ss;
					reverse(sss.begin(),sss.end());			//字符串反转
					idx=q[j].find(ss);id=q[j].find(sss);		//查找字串
					if(idx==string::npos&&id==string::npos){	//没找到
						flag=0;
						break;
					}
				}
				if(flag&&l>maxx){
					maxx=l;
					flag=1;
				}
			}
		printf("%d\n",maxx);
	}
	return 0;
}

你可能感兴趣的:(hdu,ACM_搜索)