poj3080(Blue Jeans)kmp求多个串公共子串
来源:互联网 发布:新疆航天信息开票软件 编辑:程序博客网 时间:2024/05/17 09:19
题意:给出1-10个长度为60的字符串,求出最长的公共子串(长度不能小于3),如果有多个一样长的,输出字典序最短的。
解法:想到kmp时,自己第一反应枚举第一个串的所有子串,在其他所有串中走一遍kmp,复杂度为10*60*60*60,但是发现只需枚举第一个串后缀就可以,每次枚举记录在所有串能走最远中走的最短的那个长度。这样复杂度就成了10*60*60,0ms AC。
代码:
/***************************************************** author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>using namespace std;#define eps 1e-8typedef long long LL;char str[10][61];int n;char ans[61];char tool[61];int out=0;char Next[61];void getnext(){ int i=0; int j=Next[0]=-1; int len=strlen(tool); while(i<len) { while(j!=-1&&tool[i]!=tool[j]) j=Next[j]; Next[++i]=++j; }}int kmp(int k){ int i=0,j=0; int res=0; for(int i=0; i<60; i++) { while(j!=-1&&str[k][i]!=tool[j]) j=Next[j]; j++; res=max(res,j); } return res;}int main(){ int t; cin>>t; while(t--) { scanf("%d",&n); memset(ans,0,sizeof ans); memset(tool,0,sizeof tool); out=0; for(int i=0; i<n; i++) scanf("%s",str[i]); int len=strlen(str[0]); for(int i=0; i<len; i++) { memset(tool,0,sizeof tool); strncpy(tool,str[0]+i,len-i); int ma=60; getnext(); for(int j=1; j<n; j++) ma=min(ma,kmp(j)); if(ma>out) { out=ma; strncpy(ans,tool,out); ans[out]=0; } else if(ma==out) { if(strcmp(ans,tool)>0) strncpy(ans,tool,out); ans[out]=0; } } if(strlen(ans)<3)printf("no significant commonalities\n"); else printf("%s\n",ans); } return 0;}
0 0
- poj3080(Blue Jeans)kmp求多个串公共子串
- POJ3080 Blue Jeans(KMP,求最长公共子串)
- POJ3080 Blue Jeans 最长公共子串
- poj3080 Blue Jeans-------KMP
- POJ3080 Blue jeans(未完待续,KMP,寻找多个子串的公共串)
- Blue Jeans(poj3080,后缀数组,求最长公共子串)
- POJ3080-Blue Jeans(KMP,水)
- POJ3080 Blue Jeans(kmp & 暴力)
- POJ3080 Blue Jeans(暴力kmp)
- poj 3080 Blue Jeans (KMP+最长公共子串)
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- poj 3080 Blue Jeans【KMP】【求公共子串】
- POJ3080 Blue Jeans 【KMP 暴力水过】
- poj 3080 Blue Jeans(最长公共子串)
- poj 3080 Blue Jeans(KMP匹配,枚举子串)
- POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
- poj3080 Blue Jeans(串的暴力枚举)
- 市电过零扫描电路及程序
- Eclipse中10个最有用的快捷键组合
- 苹果电脑快捷键
- C++开源跨平台类库及在VC++.net中应用的配置
- epoll使用详解(精髓)
- poj3080(Blue Jeans)kmp求多个串公共子串
- linux下top命令参数解释+free -m
- C语言当中几个获取时间和处理的函数
- 更了解自己的自行车
- 求范围内质(素)数,无暇质数对。
- 雅虎网站页面性能优化的34条黄金守则
- epoll 使用详解
- 对象数组与JSON字符串的相互转换
- c++ 简单的实现椭圆曲线加密算法