求 N 个字符串的最长连续公共子串,N 范围是 10 ,每个串最长 60,所以可以暴力……
本来是没什么意思的,不过可以学习下string的几个函数
参考:
字符串分割 http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html
string substr,find
http://www.cnblogs.com/chhyong88/archive/2011/12/04/2275376.html
http://www.cnblogs.com/shuaiwhu/archive/2008/11/01/2065107.html
Code:
来自 http://blog.youkuaiyun.com/stormdpzh/article/details/7390978
#include<iostream>
#include<string>
using namespace std;
/*
涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos
2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串
*/
string str[11];
int main()
{
int i,j,k,n,t;
cin>>t;
while(t--){
cin>>n;
for(i=0;i<n;cin>>str[i++]);
string res="";
for(i=3;i<61;i++){//长度小于3的不考虑
for(j=0;j<61-i;j++){
string tmp = str[0].substr(j,i);//取 j 到 i-1 子串
int flag=1;
for(k=1;k<n;k++){
if(str[k].find(tmp) == string::npos){
flag=0;break;
}
}
if( flag && ( tmp.size()>res.size() || tmp.size()==res.size() && tmp<res) )
res = tmp;
}
}
if(res == "")cout<<"no significant commonalities"<<endl;
else cout<<res<<endl;
}
return 0;
}