POJ 3080 Blue Jeans (KMP)
求出公共子序列 要求最长 字典序最小
枚举第一串的所有子串 然后对每一个串做KMP。找到目标子串
学会了 strncpy函数的使用 我已可入灵魂
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; char str[20][70];
char tmp[70],ans[70];
int f[70];
int n; void getfail(char *P)
{
int m=strlen(P);
f[0]=0;f[1]=0;
for(int i=1;i<m;i++)
{
int j=f[i];
while(j&&P[i]!=P[j])j=f[j];
f[i+1]=P[i]==P[j]?j+1:0;
}
} bool find(char *P,char *T)
{
int n=strlen(T);
int m=strlen(P);
getfail(P);
int j=0;
for(int i=0;i<n;i++)
{
while(j&&P[j]!=T[i])j=f[j];
if(P[j]==T[i])j++;
if(j==m)return true;//printf("%d\n",i-m+1);
}
return false;
} bool work(char *tmp,int l)//l : tmp len: ans
{
for(int i=2;i<=n;i++)
{
if(!find(tmp,str[i]))return false;
}
int len=strlen(ans); if(l>len)strcpy(ans,tmp);//最长
else if(l==len && strcmp(tmp,ans)<0)strcpy(ans,tmp);//字典序最小 return true;
} int main()
{
int CASE;
scanf("%d",&CASE); while(CASE--)
{
scanf("%d",&n); for(int i=1;i<=n;i++)
scanf("%s",str[i]); memset(ans,0,sizeof(ans)); memset(tmp,0,sizeof(tmp)); int len=strlen(str[1]); for(int i=1;i<=len;i++)
{
for(int j=0;j<len-i+1;j++)
{
strncpy(tmp,str[1]+j,i);
work(tmp,i);
}
}
if(strlen(ans)>=3)printf("%s\n",ans);//题目中说要至少3个长度
else printf("no significant commonalities\n");
}
return 0;
}
POJ 3080 Blue Jeans (KMP)的更多相关文章
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 3080 Blue Jeans(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- POJ 2406 Power Strings(KMP)
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
随机推荐
- Android开发之R文件丢失
在进行android开发的过程中,不知道怎么回事,代码中出现R代码有红色波浪线了,于是进行了clean,结果还是有红色波浪线,然后就重启了eclipse,重启以后还是这个样子,随后发现工程的R文件丢失 ...
- 武汉北大青鸟解读2016年10大IT热门岗位
武汉北大青鸟解读2016年10大IT热门岗位 2016年1月5日 13:37 北大青鸟 这是IT从业者的辉煌时代,IT行业的失业率正处在历史的低点,而且有的岗位——例如网络和安全工程师以及软件开发人员 ...
- hdu4630No Pain No Game (多校3)(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=4630 给的题解没看懂..搜解题报告看 了N久 终于在cui大神的指点下 搞明白咋回事了 将1-N中的每个数ai ...
- Codeforces Round #224 (Div. 2)
题目:http://codeforces.com/contest/382 A Ksenia and Pan Scales 一个求天平是否能够平衡的题目...水题,注意一下结果的输出就行. #inclu ...
- jsoi2014前两轮回眸
今天从常州回来了,第二轮考得惨不忍睹 大概来总结一下前两轮: 第一轮是4个小时,3道题,一道网络流,一道环形DP,一道线段树 最后一道题ahoi的原题(传送bzoj1798),非常水的线段树,是个很好 ...
- SharePoint 2007 页面定制(一)
转:http://www.nanmu.net/SharePoint-MOSS-WSS-Silverlight/Lists/Posts/Post.aspx?ID=74 本文主要包括以下几方面内容: 1. ...
- 《深入Java虚拟机学习笔记》- 第13章 逻辑运算
<深入Java虚拟机学习笔记>- 第13章 浮点运算
- Myeclipse *.link用法
引用路径 path=D:\\ProgramData\\MyEclipse\\adt
- C++ 继承之虚继承与普通继承的内存分布
仅供互相学习,请勿喷,有观点欢迎指出~ class A { virtual void aa(){}; }; class B : public virtual A { ]; //加入一个变量是为了看清楚 ...
- MapReduce 支持的部分数据挖掘算法
MapReduce 支持的部分数据挖掘算法 MapReduce 能够解决的问题有一个共同特点:任务可以被分解为多个子问题,且这些子问题相对独立,彼此之间不会有牵制,待并行处理完这些子问题后,任务便被解 ...