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

枚举第一串的所有子串   然后对每一个串做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. 第二部分 overlay 架构初探

    1 overlay可能支持的颜色格式/* possible overlay formats可能支持的颜色格式 */enum {    OVERLAY_FORMAT_RGBA_8888    = HAL ...

  2. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  3. 设计模式 - chain of Responsibility

    Chain of Responsibility也就是职责链模式,通过使用链式结构,使对象都有机会处理请求,从而避免请求的发送者与接受者间的耦合关系.将这些对象连成链,并沿着该链传递请求,直到有对象处理 ...

  4. HNOI2008玩具装箱 (斜率优化)

    总算A了,心情好激动…… 如果会了一类斜率优化,基本上这类题就成了套模版了…… 只是k函数不同 var n,l,x,tail,head,m:int64; i,j:longint; dp,q,s:..] ...

  5. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

  6. Android学习系列(23)--App主界面实现

    在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...

  7. SGU 134 Centroid

    题意:给出一个树,每个点有一个value,value的意义是去掉这个点之后所有连通分量中点最多的那个连通分量的点数,这棵树的重心为所有点value的最小值,求重心,及重心都有谁. 解法:貌似是个树形d ...

  8. Java was started but returned exit code=13 C:\ProgramData\Oracle\Java\javapath\javaw.exe

    ---------------------------Eclipse---------------------------Java was started but returned exit code ...

  9. 黑盒测试用例设计方法&理论结合实际 -> 错误推断法

    一 概念 基于经验和直觉推测程序中所有可能存在的各种错误, 从而有针对性的设计测试用例的方法. 二 错误推断法的应用  基本思想:列举出程序中所有可能有的错误和容易发生错误的特殊情况,根据他们选择测试 ...

  10. ASP.NET运行原理_2

    当一个HTTP请求到服务器并被IIS接收到之后,IIS首先通过客户端请求的页面类型为其加载相应的.dll文件,然后在处理过程中将这条请求发送给能够处理这个请求的模块.在ASP.NET 3.5中,这个模 ...