Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13551 | Accepted: 6011 |
Description
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
Source
South Central USA 2006
题意是找出最长,字典序最小的子串,且长度大于等于3,否则输出
no significant commonalities
我的思路是暴力枚举出第一个串的长度大于2的所有子串,以下为代码。
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; char s[30][70]; char temp[3600][70]; int main() { int n,t; scanf("%d",&t); while(t--) { memset(s,0,sizeof(s)); memset(temp,0,sizeof(temp)); scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s",s[i]); int q=0; for(int l=3;l<=60;l++)//枚举出所有子串 { for(int i=0;i<60;i++) { strncpy(temp[q++],s[0]+i,l);//函数为从第i个位置复制长度为l的串给temp if(i+l>=60) { break; } temp[q][l]='\0'; } } char ans[70]; memset(ans,0,sizeof(ans)); int okok=0; for(int i=0;i<q;i++)//暴力枚举 { int sum=0; for(int j=1;j<n;j++) { for(int w=0;s[j][w]!='\0';w++) { int ok=1; for(int k=0;temp[i][k]!='\0';k++) { if(s[j][w+k]!=temp[i][k]) { ok=0; break; } } if(ok) { sum++; break; } } } if(sum==n-1)//子串和所有串都匹配 { if(strlen(temp[i])>strlen(ans))//长度判断 { okok=1; memset(ans,0,sizeof(ans)); strcpy(ans,temp[i]); } else if (strlen(temp[i])==strlen(ans)) { if(strcmp(temp[i],ans)==-1)//字典序判断 { okok=1; memset(ans,0,sizeof(ans)); strcpy(ans,temp[i]); } } } } if(okok) printf("%s\n",ans); else printf("no significant commonalities\n"); } return 0; }