Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14283 | Accepted: 6356 |
Description
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
题意: 求出多组字符串的最大相邻公共子串,若长度小于三则输出“no significant commonalities”,否则,对于输出公共子串,对于同样长度的公共子串,输出按字母排序较小的串
思路:暴搜暴搜暴搜~~~
#include<cstdio> #include<iostream> #include<cstring> using namespace std; char a[15][65]; char anss[65]; int main(){ int n; scanf("%d",&n); while(n--){ int m; scanf("%d",&m); int i,j,k; for(i=0;i<m;i++){ scanf("%s",a[i]); } int ans=0; int len=strlen(a[0]); char sub[65]; for(i=0;i<len;i++){ for(j=i+2;j<len;j++){ strncpy(sub,a[0]+i,j-i+1); sub[j-i+1]='\0'; int flag=1; for(k=1;flag&&k<m;k++){ if(strstr(a[k],sub)==0){ flag=0; } } if(flag&&(ans<strlen(sub)||(ans==strlen(sub)&&strcmp(anss,sub)>=0))){//记得比较一下 ans=strlen(sub); strcpy(anss,sub); } } } if(ans<3) printf("no significant commonalities"); else printf("%s",anss); printf("\n"); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/taluoyaxin/article/details/47723779