题目链接
题目大意
就是找几个字符串中的最长的公共子序列,比较简单,可以暴力求解
思路
从第一个字符串中一次选取字符串(长度从1到60),然后与后面几组相比较
代码
#include <iostream>
#include <queue>
#include <fstream>
#include <string.h>
#include <string>
#include <stack>
using namespace std;
int main()
{
// ifstream fin("data.txt");
char s[11][66];
int N;
// fin>>N;
cin>>N;
while(N--)
{
int n;
//fin>>n;
cin>>n;
for(int i=0; i<n; i++)
//fin>>s[i];
cin>>s[i];
char ans[66];
int len = 1;
int maxlen = 0;
int start=0;
while(1)
{
//从1选字符串截取的长度
char tmp[66];
int tt = start;
if(tt+len>60) //1选完了之后是2
{
len++;
start=0;
if(len>60)
break;
continue;
}
int j;
for(j=0; j<len; j++)
tmp[j] = s[0][tt++];
tmp[j]='\0';
int ff=1;
for(int k=1; k<n; k++) //将第一个串中的数据与之后的比较
{
if(!strstr(s[k],tmp))
{
ff=0;
break;
}
}
if(ff==1)
{
if(maxlen < len)
{
maxlen = len;
strcpy(ans,tmp);
}
if(maxlen == len)
{
if(strcmp(ans,tmp)>0)
strcpy(ans,tmp);
}
}
start++;
}
if(maxlen<3)
cout<<"no significant commonalities"<<endl;
else
cout<<ans<<endl;
}
return 0;
}