标签:默认 cstring names lis with ant cep size OLE
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:23922 | Accepted: 10572 |
Description
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
//由于只查找是否出现过,算法复杂度差距不大,所以这里给出简单一些的写法 #include<stdio.h>//网上大佬代码 #include<algorithm> #include<iostream> #include<string.h> using namespace std; const int maxn = 65; char str[maxn]; string ansstr; string str2[maxn]; int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",str); str2[i]=str; } int ans=0; string tmp=str2[0]; int tmplen=tmp.size(); for(int i=0;i<tmplen;i++) { for(int j=1;i+j<=tmplen;j++) { int cnt=1; string ss=tmp.substr(i,j); //返回一个string, //包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos, //即不加参数会默认拷贝整个s) //ss为tmp从i开始长度为j的子串; for(int k=1;k<n;k++) { if(str2[k].find(ss)!=-1)//判断str2k串中有没有子串ss; cnt++;//记录有的数量,从而判断所有字符串是否都有子串ss; } if(cnt==n)//判断是否所有字符串都有子串ss; { if(ans<j) { ans=j; ansstr=ss; } else if(ans==j) { ansstr=min(ansstr,ss);//根据字典序比大小; } } } } if(ans<3) printf("no significant commonalities\n"); else printf("%s\n",ansstr.c_str());//这是为了与c语言兼容,在c语言中没有string类型, //故必须通过string类对象的成员函数c_str()把string //对象转换成c中的字符串样式。 //ansstr为string类型,ansstr.c_str()转化为了char类型; } }
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxn=65; char str[maxn]; string ansstr; string str2[maxn]; int main(){ int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",str); str2[i]=str; } int cnt=0; for(int i=0;i<60;i++){ for(int j=1;j+i<=60;j++){ int ans=1; string ss=str2[0].substr(i,j); for(int k=1;k<n;k++){ if(str2[k].find(ss)!=-1){ ans++; } } if(ans==n){ // printf("%s\n",ss.c_str()); if(cnt<j){ cnt=j; ansstr=ss; }else if(cnt==j){ ansstr=min(ansstr,ss); }else{ continue; } }else{ continue; } } } if(cnt<3){ printf("no significant commonalities\n"); }else{ printf("%s\n",ansstr.c_str()); } } return 0; }
标签:默认 cstring names lis with ant cep size OLE
原文地址:https://www.cnblogs.com/qqshiacm/p/11587372.html