求出公共子序列  要求最长  字典序最小

枚举第一串的所有子串   然后对每一个串做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)的更多相关文章

  1. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  2. POJ 3080 Blue Jeans(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...

  3. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)

    题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...

  4. POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)

    <题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1.  最长公共串长度小于3输出   no significant co ...

  5. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  6. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  7. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  8. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  9. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

随机推荐

  1. python中的commands模块

    commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.ge ...

  2. everything搜索工具小技巧

    everything工具平时用的也不多,但是有时候使用的时候却总是找不着北. everything支持五种搜索方式,如下图: 正则匹配搜索: 当你选择正则匹配之后,你可能需要匹配某个文件夹里面的某个文 ...

  3. java的Serialization 机制

    基本使用方法               Serialization是指把类或者基本的数据类型持久化(persistence)到数据流(Stream)中,包括文件.字节流.网络数据流.         ...

  4. Highcharts20151130

    $(function () { $('#container').highcharts({ chart: { type: 'spline' // 图的类型 }, title: { text: null ...

  5. 【转】蓝牙ble app开发(三) -- 抓包

    原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...

  6. 异常处理 Exception

    一.异常类 1.在C#中所有的异常都是使用一个异常类型的示例对象表示的,这些异常类型都是继承自System.Exception类型,或者直接使用System.Exception类型的实例对象: 2.在 ...

  7. 枚举类型的单例模式(java)

    Inspired by Effective Java. Singleton模式是在编程实践中应用最广泛的几种设计模式之一.以前知道的,实现单例的方法有两种(下面的A.B).刚刚在读<Effect ...

  8. mac系统 虚拟机安装教程

    http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html

  9. [Tommas] 测试场景 VS 测试用例 哪个更好?(转)

    分享一篇网上别人的感悟:      6年前,我在一家中型跨国公司工作的时候,我建议与其浪费时间在准备充分的测试用例,还不如编写描述测试场景的文档.所有的人都对我的建议.投以烦恼的目光.他们的脸上清晰地 ...

  10. Kafka Unknow host

    今天在测试kafka时遇到 Unknow host问题,经过Google,找到了解决办法,总结如下: 原因:Linux主机名称为null 第一步通过修改/etc/sysconfig/network 中 ...