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. char const*, char*const, const char *const的区别

    C++标准规定,const关键字放在类型或变量名之前等价的.所以,const char*和 char const*是一样的. const char*   //常量指针---指向常量的指针----指针指 ...

  2. ED/EP系列2《文件结构》

    电子存折/电子钱包应用是为持卡人进行金融交易而设计的一种应用.对于一张金融 IC 卡来说,它可以同时支持电子存折和电子钱包两种应用,也可以只支持其中的一种.卡片上两种应用的存在情况可以由应用类型标识( ...

  3. MIFARE系列1《MIFARE简介》

    随着社会的发展,智能卡在很多领域得到了广泛的应用.特别是非接触卡,由于使用方便以及功能强大的特点,在管理.公交.工作证.身份识别等领域得到了快速的普及和推广. 非接触卡已经逐步发展成为一个独立的跨学科 ...

  4. MySQL命令输入错误 取消命令

    mysql命令行输入错误字符需要取消执行时,可在分号“:”出现前输入“\c”来取消该条命令,注意c前边的“\”.   1 2 3 4 mysql> select -> user() -&g ...

  5. Centos 安装 p7zip,即Linux下的7z

    Centos 无法直接通过yum安装7z,我们一般通过repoforge,rpmforge的软件包进行安装,你只需要下载一个对应的包,直接安装就可以 p7zip-9.20.1-1.el4.rf.i38 ...

  6. mini6410-JNI-led

    一.编写JNI模块 当安装好NDK编译环境后,会在它的目录下找到sample目录,它里面有一些例子,可以参考这些例子来写我们自已的模块. 1.在/home/android/文件夹下,新建“ledjni ...

  7. java判断某个字符串包含某个字符串的个数

    /** * 判断str1中包含str2的个数 * @param str1 * @param str2 * @return counter */ public static int countStr(S ...

  8. 在EDK里面添加ISE IP core的方法

    (1)在ISE下,使用core generator,可以得到xilinx的IP的*.v和*.ngc 文件,将这两个文件拷贝出来: (2)在EDK下使用“Create or Import Periphe ...

  9. 【String to Integer (atoi) 】cpp

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  10. 《我是IT一只小小鸟》读后感

    <我是IT一只小小鸟>读后感 首先,非常感谢我的老师给我推荐了这么一本书,虽然刚开始因为这门课学分太低,所以我对老师布置了字数这么多的作业存在有很大的不满,但在看了这本书后我的不满立马得到 ...