http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html
我采用的是方法三。

注意:当长度相同时,取字典序最小的。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
/*
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html
我采用的是方法三。 注意:当长度相同时,取字典序最小的。
*/
using namespace std;
const int maxn=;
char str[maxn][];
int n;
int minlen; //n个字符串中最短的长度
int id; //长度最短字符串的编号
int next[]; void getNext(char*P){
int k;
int lm=strlen(P);
next[]=;
k=;
for(int i=;i<lm;i++){
while(k> && P[k]!=P[i])
k=next[k];
if(P[k]==P[i])
k++;
next[i+]=k;
}
} int kmp(char*T,char*P){
int k,c;
c=; //表示字符串P在T中能够匹配的最大长度
int ln=strlen(T),lm=strlen(P);
for(int i=;i<ln;i++){
while(k>&&P[k]!=T[i])
k=next[k];
if(P[k]==T[i])
k++;
if(k>c)
c=k;
if(k==lm)
return k;
}
return c;
}
int main()
{
int l;
while(scanf("%d",&n)!=EOF){
if(n==)
break;
minlen=;
for(int i=;i<=n;i++){
scanf("%s",str[i]);
l=strlen(str[i]);
if(l<minlen){
minlen=l;
id=i;
}
}
char tmp[],s[];
int minl,maxl=,cnt;
//minl为枚举的后缀在其余n-1个字符串中都能匹配的长度
//maxl为公共子串的最大长度
char ans[]; //所求公共子串
for(int i=;i<=minlen;i++){
strncpy(tmp,str[id]+minlen-i,i);
tmp[i]='\0';
getNext(tmp);
minl=i+;
for(int j=;j<=n;j++){
if(j!=id){
cnt=kmp(str[j],tmp);
minl=min(cnt,minl);
}
}
if(minl>maxl){
maxl=minl;
strncpy(ans,tmp,minl);
ans[minl]='\0';
}
//如果相等长度,则输出字典序最小的
else if(minl==maxl){
strncpy(s,tmp,minl);
s[minl]='\0';
if(strcmp(s,ans)<)
strcpy(ans,s);
}
}
if(maxl==)
printf("IDENTITY LOST\n");
else
printf("%s\n",ans); }
return ;
}

POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)的更多相关文章

  1. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  2. POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  3. POJ 3450 Corporate Identity(KMP)

    [题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...

  4. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

  5. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  6. POJ 3415 不小于k的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9248   Accepted: 3071 ...

  7. POJ 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

  8. POJ 3450 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...

  9. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

随机推荐

  1. Resource is out of sync with the file system

    Resource is out of sync with the file system解决办法: 在eclipse或mycelipse中,启动run on server时或查看项目文件时报错:Res ...

  2. linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)

    Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...

  3. hdu 1434 幸福列车

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1434 幸福列车 Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长 ...

  4. asp.net实现手机号码归属地查询

    protected void Button1_Click(object sender, EventArgs e)        {            if (Regex.IsMatch(TextB ...

  5. jquery 源码学习(一)

    从上边的注释看,jQuery的源码结构相当清晰.条理,不像代码那般晦涩和让人纠结   1. 总体架构 1.1 自调用匿名函数 self-invoking anonymous function 打开jQ ...

  6. wifi-sdio接口

    1.sdio接口层解析 SDIO总线 SDIO总线和USB总线类似,SDIO也有两端,其中一端是HOST端,另一端是device端.所有的通信都是由HOST端发送命令开始的,Device端只要能解析命 ...

  7. libevent 信号示例

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. 使用 RestEasy 和 Apache Tomcat 构建 RESTful Web 服务

    第一次,用这个RestEasy框架,用的时候,总是提示,404的错误,郁闷,呵呵,不过经过努力,终于解决问题,特别留个标记. 关于404的错误,上网找了一大堆,也还不行. 我感觉应该是lib下面架包的 ...

  9. Swift function how to return nil

    这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...

  10. java数据结构和算法------顺序查找

    package iYou.neugle.search; public class Sequence_search { public static int SequenceSearch(double[] ...