poj3450 Corporate Identity
来源:互联网 发布:ps软件破解版 编辑:程序博客网 时间:2024/06/06 03:19
题意:给你若干个字符串,求它们的最长公共子串。对于相同长度的最长公共子串,取字典序最小的那个。
思路:枚举第一个字符串的所有可能子串,对后面所有字符串进行匹配。
#include<iostream>#include<string>#include<algorithm>using namespace std;const int maxn=1001;bool kmp(string a,string b){int i,j;int fail[maxn];j=-1;memset(fail,-1,sizeof(fail));for(i=1;i<a.length();i++){ while(j>-1 && a[j+1]!=a[i]) j=fail[j];if(a[j+1]==a[i]) j++;fail[i]=j;}j=-1;for(i=0;i<b.length();i++){while(j>-1 && a[j+1]!=b[i]) j=fail[j];if(a[j+1]==b[i]) j++;if(j==a.length()-1) return 1;} return 0; } int main(){int n;string a[10001];while(cin>>n && n){for(int i=1;i<=n;i++)cin>>a[i];int maxlen=0;string maxsub;for(int f1=0;f1<a[1].length();f1++){string sub;int nowlen=maxlen;bool flag=1; while(flag && f1+nowlen<a[1].length()){nowlen++;sub=a[1].substr(f1,nowlen); for(int i=2;i<=n;i++){if(!kmp(sub,a[i])) {flag=0;nowlen--;break;}}}if(nowlen>maxlen){maxlen=nowlen;maxsub=a[1].substr(f1,nowlen);}else if(nowlen==maxlen){flag=1;sub=a[1].substr(f1,nowlen);if(sub.length()==nowlen){for(int i=2;i<=n;i++)if(!kmp(sub,a[i])){flag=0;break;}if(flag && (sub<maxsub))maxsub=sub;}}}if(maxlen==0) cout<<"IDENTITY LOST"<<endl;else cout<<maxsub<<endl;}return 0;}
- poj3450 Corporate Identity
- Corporate Identity poj3450
- POJ3450 Corporate Identity
- poj3450 Corporate Identity
- POJ3450:Corporate Identity(后缀数组)
- POJ3450 Corporate Identity 【后缀数组】
- POJ3450 Corporate Identity 最长公共子串
- poj3450 Corporate Identity kmp || 后缀数组
- POJ3450 Corporate Identity 【KMP 暴力strstr可过】
- poj3450 Corporate Identity(后缀数组+二分答案)
- POJ3450 Corporate Identity(kmp,最长公共子串)
- Corporate Identity
- Corporate Identity Design
- poj 3450 Corporate Identity
- hdu 2328 Corporate Identity
- hdu 2328 Corporate Identity
- POJ-3450-Corporate Identity
- POJ 3450 Corporate Identity
- VS2008使用Access数据库,不能更新数据库中数据的原因
- Visual Stdio 2008 的卸载
- User not in the sudoers file. This incident will be reported
- IE中缓存中的图片数据清除问题
- C++头文件的包含顺序研究
- poj3450 Corporate Identity
- mysql
- ruby开启
- NetBeans学习笔记2
- c/c++中出现“undefined reference to”的解决
- 简单折叠面板的制作(来源于CSS技术彻底研究)
- jar命令 创建和解压jar文件包
- 如何在Windows下发布QT应用程序?
- 让linux用户隶属于多个组