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. WPF:将HTML RGB颜色值转化为Color对象的两种方式

    (1)方式一: Color color1 = (Color)System.Windows.Media.ColorConverter.ConvertFromString("#E0E0E0&qu ...

  2. JQuery中ajax跨域问题

    var url = "http://apis.juhe.cn/idcard/index?key=e0a6277420506b2816b82f7d7821976c&cardno=&qu ...

  3. EXCLE使用宏生成目录

    宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...

  4. 转:CentOS 7 安装Nginx

    一.准备工作: 1.安装必备工具: ? 1 2 3 $ yum -y install gcc gcc-c++ autoconf automake   $ yum -y install zlib zli ...

  5. linux下操作

    一.没有正确安装GNOME电源管理器的默认配置 二.oracle启停 1. linux下启动oraclesu - oraclesqlplus /nologconn /as sysdbastartupe ...

  6. 16.如何设置Quartus II Programmer,保护pof不被读出

    Program时,把security bit勾上,点击start 这样examine时就不能正确的读出pof 读出来的pof 除文件头外,其余的内容全为0 怎么样,大家试试!

  7. JS获取字符串长度(英文占1个字符,中文汉字占2个字符)

    方法一: String.prototype.gblen = function() { var len = 0; for (var i = 0; i < this.length; i++) { i ...

  8. SQL中的5种聚集函数

    作为一个刚毕业进入这行的菜鸟,婶婶的觉的那种大神.大牛到底是怎样炼成的啊,我这小菜鸟感觉这TMD要学的东西这多啊,然后就给自己定了许多许多要学习的东西,可是有人又不停地给你灌输:东西不在多而要精通!我 ...

  9. 如何做一个脚本自动打开IE浏览器

    打开记事本,输入start iexplore "http://www.baidu.com"这个是打开百度,如果只要打开IE就输入start iexplore然后另存为--保存类型改 ...

  10. char,wchar_t,WCHAR,TCHAR,ACHAR的区别----LPCTSTR

    转自http://blog.chinaunix.net/uid-7608308-id-2048125.html 简介:这是DWORD及LPCTSTR类型的了解的详细页面,介绍了和类,有关的知识,加入收 ...