POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
来源:互联网 发布:pyrosim软件 编辑:程序博客网 时间:2024/05/17 22:08
POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
http://poj.org/problem?id=3080
题意:
给你n个字符串,要你求出这n个字符串的最长公共连续子序列是哪个,如果存在多个最长的,就输出字典序最小的那个。
分析:
本题只需要枚举第一个串的长度>=3的所有字串,然后用该字串KMP匹配其他剩下n-1个串,看看能否找到匹配点即可。如果长度L符合要求,那么就去找到长度为L且字典序最小的串。
本题枚举字串长度需要二分,找出可行的最长字串长度,然后在当前长度中枚举所有可能串,再找出字典序最小的那个串.
AC代码:0ms
<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;char s[15][100];char P[100];char ans[100];//最终的结果字符串int m,next[100];int n;//表示有多少个主串void getFail(){ m=strlen(P); next[0]=next[1]=0; for(int i=1;i<m;i++) { int j =next[i]; while(j && P[i]!=P[j]) j=next[j]; next[i+1] = (P[i]==P[j])?j+1:0; }}bool find(char *T){ int j=0; getFail(); for(int i=0;i<60;i++) { while(j && T[i]!=P[j]) j=next[j]; if(T[i]==P[j]) j++; if(j==m) return true; } return false;}bool solve_1(int len)//判断第0个base串的长为len的字串中是否有可行串{ for(int i=0;i+len-1<60;i++) { strncpy(P,s[0]+i,len); P[len]=0;//P串末尾加'\0' bool ok=true; for(int j=1;j<n;j++) if(!find(s[j])) { ok=false; break; } if(ok) return true; } return false;}void solve_3(int len)//找出长度为len的可行字串中字典序最小的,放在ans中{ bool first=true; for(int i=0;i+len-1<60;i++) { strncpy(P,s[0]+i,len); P[len]=0;//P串末尾加'\0' bool ok=true; for(int j=1;j<n;j++)if(!find(s[j])) { ok=false; break; } if(ok) { if(first) { strncpy(ans,P,len+1); first=false; } else if(strcmp(ans,P)>0)//字典序小才更新 strncpy(ans,P,len+1); } }}bool solve(){ int L=3,R=60; if(!solve_1(3)) return false; while(R>L) { int m=L+(R-L+1)/2; if(solve_1(m)) L=m; else R=m-1; } //找到了可行字串的最长长度为L,然后需要找出字典序最小的 solve_3(L);//找到长L的字典序最小的字串存在ans中 return true;}int main(){ int kase; scanf("%d",&kase); while(kase--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s",s[i]); if(!solve())//最终结果存在ans中 printf("no significant commonalities\n"); else printf("%s\n",ans); } return 0;}</span>
0 0
- POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
- poj 3080 Blue Jeans (KMP+最长公共子串)
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- POJ 3080 Blue Jeans(KMP 最长公共子串)
- POJ 3080:Blue Jeans:枚举求解n个字符串的最长公共连续子串
- poj 3080 Blue Jeans【KMP】【求公共子串】
- poj 3080 Blue Jeans(最长公共子串)
- POJ3080 Blue Jeans(KMP,求最长公共子串)
- POJ 3450 Corporate Identity(KMP:最长连续公共子序列)
- POJ 3080/ ZOJ 2784 : Blue Jeans - 串匹配,最长公共子串
- poj 3080 Blue Jeans (多串最长公共子串 暴力)
- POJ3080 Blue Jeans 最长公共子串
- POJ 3080 Blue Jeans (求最长公共字符串)
- POJ 3080 Blue Jeans KMP
- POJ 3080 Blue Jeans----kmp
- POJ 3080 Blue Jeans(KMP)
- POJ 3080 Blue Jeans KMP
- poj 3080 Blue Jeans(KMP)
- Confluence 管理独立的页面
- MYSQL修改列名
- 输入流 输入函数
- 初试sp
- JPCAP简介
- POJ 3080 Blue Jeans(KMP:最长连续公共子序列)
- log4j
- 在Hadoop 2.3上运行C++程序各种疑难杂症(Hadoop Pipes选择、错误集锦、Hadoop2.3编译等)
- Java语言程序设计基础篇 第八章 课后编程答案
- 浅析Struts1和Struts2的Action线程安全问题
- 基本类型强制转换
- poi生成excel工具类
- 《黑马程序员》 选择排序和冒泡排序
- 多线程下载