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 ...
随机推荐
- MQ-传输方式Topic和Queue的对比
Jms规范里的两种message传输方式Topic和Queue,两者的对比如下表(): Topic Queue 概要 Publish Subscribe messaging 发布订阅消息 Poi ...
- The Impact of Garbage Collection on Application Performance
As we’ve seen, the performance of the garbage collector is not determined by the number of dead obje ...
- vfp 操作excel
VFP全面控制EXCEL 收藏 VFP和Excel都可以用来进行处理数据库表格,如果巧妙地将二者的优点结合起来,将会大大方便我们的工作.比如我们可以利用VFP进行处理数据,而利用Excel的预览打印功 ...
- JAX-RS入门 二 :运行
上一节,已经成功的定义了一个REST服务,并且提供了具体的实现,不过我们还需要把它运行起来. 在上一节的装备部分,列举了必须的jar(在tomcat中运行)和可选的jar(作为一个独立的应用程序运行) ...
- CVTE公司面经
1.先是网上测评,通过后通知你参加一面. 2.关于一面:一共进行了10分钟左右,三四个人一起面,没有问什么技术. 一共问了3个问题:a.你为什么选择我们公司的这个职位.我答的大概意思是本科研究生期间, ...
- NGINX(一)内存结构
ngx_buf_t和ngx_chain_t是nginx中操作内存的重要手段, 很多的数据都需要通过这个结构进行保存. 其中ngx_buf_t中保存一块可用内存, ngx_chain_t则是将内存块连接 ...
- C++ static_cast dynamic_cast reinterpret_cast const_cast转换
static_cast <type-id> ( expression ) 和C风格的类型转换相似,可以转换一个指针到基类,或者派生类.不做Run-time类型检查,这样转换并不总是安全的. ...
- HDU 1520-Anniversary party(树形dp入门)
题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1 ...
- opencv2.4.4 背景减除算法收集
算法集合:https://code.google.com/p/bgslibrary/ 测试:AdaptiveBackgroundLearning算法 #include <iostream> ...
- 【译】 AWK教程指南 附录D-AWK的内置变量
因内置变量的个数不多,此处按其相关性分类说明,并未按其字母顺序排列. ARGC ARGC表示命令行上除了选项 -F, -v, -f 及其所对应的参数之外的所有参数的个数.若将"awk程序&q ...