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 ...
随机推荐
- 1136. Parliament(二叉树)
1136 先由后左 再父 建一个二叉树 #include <iostream> #include<cstdio> #include<cstring> #includ ...
- Deployed component GUIs and figures have different look and feel than MATLAB desktop
原文:http://www.mathworks.com/support/bugreports/1293244 Description Deployed GUIs and figures look an ...
- 学习面试题Day07
1.打印出100以内的素数 该编程题的思路大致如下: (1)完成一个判断某整数是否为素数的方法: (2)循环1--100: (3)每循环一次就判断一次,返回true则打印:package com.ex ...
- HDU 4393 Throw nails
Throw nails Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SSL 通信原理及Tomcat SSL 配置
SSL 通信原理及Tomcat SSL 双向配置 目录1 参考资料 .................................................................. ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- 生日小助手V4.0——迁移到Python3
生日小助手V4.0——迁移到Python3 生日小助手V4.0只支持Linux系统,依赖命令行软件lunar Ubuntu系统安装方法:1.安装lunarsudo apt-get install lu ...
- 【转载】grep,egrep,fgrep详解
[转载自]http://blog.csdn.net/homking/article/details/6000711 egrep 等价于 grep -E fgrep 等价于 grep -F grep - ...
- MapReduce概述,原理,执行过程
MapReduce概述 MapReduce是一种分布式计算模型,运行时不会在一台机器上运行.hadoop是分布式的,它是运行在很多的TaskTracker之上的. 在我们的TaskTracker上面跑 ...
- work2
回答问题: 描述在这么多相似的需求面前, 你怎么维护你的设计 (父类/子类/基类, UML, 设计模式, 或者其它方法) 让整个程序的架构不至于崩溃的? 答:诚然,问题给出了很多选项如-a,-v,- ...