17ACM Qingdao 1003 The Dominator of Strings
2017-09-20 23:37
405 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6208
分析:选任意一个最长的字符串,用KMP判断另外的串是否都在这个串内。
代码:
分析:选任意一个最长的字符串,用KMP判断另外的串是否都在这个串内。
代码:
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int Tmax=100005; int n,p[Tmax],f[Tmax],maxlen,maxn,alen; char s[Tmax],tmp[Tmax],a[Tmax],MT[Tmax]; bool ok; void getfail() { int i=0,j=-1; f[0]=-1; alen=strlen(a); while(i<alen) { if(j==-1||a[i]==a[j]) { i++; j++; f[i]=j; } else j=f[j]; } return; } bool KMP() { int i=0,j=0; while(i<maxlen) { if(j==-1||MT[i]==a[j]) { i++; j++; } else j=f[j]; if(j==alen) return true; } return false; } int main() { int T,i,j,len; scanf("%d",&T); while(T--) { p[1]=1; scanf("%d",&n); maxlen=0; ok=true; for(i=1;i<=n;i++) { scanf("%s",tmp); len=strlen(tmp); if(len>maxlen) { maxlen=len; maxn=i; } for(j=0;j<len;j++) s[p[i]+j]=tmp[j]; p[i+1]=p[i]+len; } for(i=0;i<maxlen;i++) MT[i]=s[p[maxn]+i]; MT[i]='\0'; for(i=1;i<=n;i++) { if(i==maxn) continue; len=p[i+1]-p[i]; for(j=0;j<len;j++) a[j]=s[p[i]+j]; a[j]='\0'; getfail(); if(KMP()==false){ printf("No\n"); ok=false; break; } } if(ok==true) printf("%s\n",MT); } return 0; }
相关文章推荐
- 【2017 ACM/ICPC Asia Regional Qingdao Online 1003】hdu 6208 The Dominator of Strings
- HDU-2017 ACM/ICPC Asia Regional Qingdao Online-1003-The Dominator of Strings
- 1003 The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings
- 2017 ACM/ICPC Asia Regional Qingdao Online:1003 The Dominator of Strings
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6208 1003 The Dominator of Strings(AC自动机)
- HDU - 6208 The Dominator of Strings (2017 ACM-ICPC 亚洲区 (青岛赛区) 网络赛 1003)
- 2017ICPC青岛网络赛-The Dominator of Strings
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- 2017青岛赛区亚洲区域赛网络赛 The Dominator of Strings
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- 【hdu 6208】The Dominator of Strings
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- HDU 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- HDU6208 The Dominator of Strings【字符串】
- hdu 6208 The Dominator of Strings 后缀自动机 LCS