标签:aaa writing memory eterm logs sts single RKE sign
题目链接:http://poj.org/problem?id=3080
学习博客:https://www.cnblogs.com/acjiumeng/p/6818213.html
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21342 | Accepted: 9467 |
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
首先你必须遍历第一个串每一个子串长度的(60*60),然后求每一个子串的next[]数组,最大为60,最后和剩下的串进行比较,最大10*60,所以这道题最大的时间复杂度是(60*60*60*10*60),差不多刚好卡在时限左右
看代码:
#include<iostream> #include<string.h> #include<map> #include<cstdio> #include<cstring> #include<stdio.h> #include<cmath> #include<ctype.h> #include<math.h> #include<algorithm> #include<set> #include<queue> typedef long long ll; using namespace std; const ll mod=1000; const int maxn=60+10; const int maxk=5e3+10; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 string a[15]; int next[maxn]; void cal_next(string s) { int len=s.length(); int k=-1; next[0]=-1; for(int i=1;i<len;i++) { while(k>-1&&s[k+1]!=s[i]) { k=next[k]; } if(s[k+1]==s[i]) k++; next[i]=k; } } bool kmp(string x,string y) { int k=-1; for(int i=0;i<x.length();i++) { while(k>-1&&y[k+1]!=x[i]) { k=next[k]; } if(x[i]==y[k+1]) k++; if(k==y.length()-1) return true; } return false; } int main() { int t,n,flag=0; scanf("%d",&t); while(t--) { string ans=""; cin>>n; for(int i=0;i<n;i++) { cin>>a[i]; } for(int i=1;i<=a[0].size();i++)//遍历每一个长度 { for(int j=0;j+i<=a[0].size();j++)//取每一个子串 { flag=0; string op=a[0].substr(j,i); cal_next(op);//求每个子串的next[] for(int k=1;k<n;k++) { if(!kmp(a[k],op))//和每一个串进行比较,看是否有不符合的,有点话直接break { flag=1; break; } } if(flag==0) { if(op.size()>ans.size()) ans=op; else if(op.size()==ans.size()) ans=min(op,ans); } } } if(ans.size()<3) cout<<"no significant commonalities"<<endl; else cout<<ans<<endl; } return 0; }
标签:aaa writing memory eterm logs sts single RKE sign
原文地址:https://www.cnblogs.com/caijiaming/p/9692991.html