POJ Blue Jeans(3080)-STL&&KMP
来源:互联网 发布:昆仑万维 知乎 编辑:程序博客网 时间:2024/06/10 23:03
Input
Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
- A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
- m lines each containing a single base sequence consisting of 60 bases.
Output
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.
题意: TT组测试数据, 每组n个字符串, 寻找所有串中最长的公共序列 ;
解(1): 暴力搜, 有STL代码方便了很多.
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>`#include <algorithm>#include <cctype>#include <set>#include <vector>#define INF 0x7fffffff#define eps (1e-9)#define clearto(s,x) memset(s,x,sizeof(s))using namespace std;int n,m,tot=0;int letMeAC(){ int i,k,t,l; string s[15]; string smax=""; for(i=0;i<n;i++) cin>>s[i]; for(i=0;i<s[0].size();i++) for(l=3;l+i<=s[0].size();l++) { string stp= s[0].substr(i,l); int ok= 1; for(t=1;t< n;t++) if(s[t].find(stp)==string::npos) { ok=0; break; } if(ok==0) continue; if((l==smax.size())&&(stp<smax)) smax =stp; else if(l> smax.size()) smax =stp; } if(smax.size()<3) printf("no significant commonalities\n"); else printf("%s\n",smax.c_str() );}int main(){ freopen("D:\data.txt","r",stdin); int TT,i,k; scanf("%d",&TT); while(TT--){ scanf("%d",&n); letMeAC(); } return 0;}
解(2) :应用KMP算法,参考网上题解又交了个,时间优化很多:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>`#include <algorithm>#include <cctype>#include <set>#include <vector>#define INF 0x7fffffff#define eps (1e-9)#define clearto(s,x) memset(s,x,sizeof(s))using namespace std;int n,m,tot=0;int kmp(char st[],char p[]){ int next[100]; next[0]= -1; next[1]= 0; int i =0,k,t, ls =strlen(st), lp =strlen(p); while(++i <lp-1){ t =next[i]; while(t!=-1 && p[i]!=p[t]) t =next[t]; next[i+1] = t+1 ; } i = t =0; while(i<ls&&t<lp) { if(t==-1 || st[i]==p[t]){ i++; t++; } else t =next[t]; } return (t==lp);}int main(){ //freopen("D:\data.txt","r",stdin); int TT,i,k,t; char stp[100],ans[100]; char s[20][100]; scanf("%d",&TT); while(TT--){ scanf("%d",&n); for(i=0;i<n;i++) scanf("%s",s[i]); int flag=0; int len,ll =strlen(s[0]); for(len =ll;len>2&&flag==0; len--) //从最长的子串开始枚举 { strcpy(ans,"ZZ"); for(i=0;i+len<=ll;i++) //在len长度下顺序枚举s[0] { for(t=0;t<len;t++) stp[t]=s[0][i+t]; stp[t]='\0'; for(k=1;k < n;k++) if(kmp(s[k],stp)==0) break; if(k==n) { if(strcmp(ans,stp)>0) strcpy(ans,stp); flag=1; } } } if(flag==1) printf("%s\n",ans); else printf ("no significant commonalities\n"); } return 0;}
0 0
- POJ Blue Jeans(3080)-STL&&KMP
- 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)
- poj-3080 Blue Jeans(KMP)
- [poj 3080]Blue Jeans [kmp]
- POJ 3080 Blue Jeans KMP
- POJ 3080 Blue Jeans【stl】
- 【Kmp】Blue Jeans POJ
- poj 3080 Blue Jeans kmp+枚举
- [KMP或者暴力]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)
- 亲和数(杭电2040)
- 抽象渗漏法则
- class文件的构成以及jvm的内存分配
- 动态规划---最长公共子序列
- Ext动态改变fieldLabel的值
- POJ Blue Jeans(3080)-STL&&KMP
- 冒泡排序
- SDUTOJ2128 二叉排序树
- Getting Spark Setup in Eclipse
- Android加速度传感器的使用:摇一摇功能的实现
- 鸡尾酒排序
- live555杂谈系列(一)---source,sink简介
- 事件与委托
- 一个适合的火车站的idea