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. Postgresql命令行和数据库备份与恢复

    进入Postgresql   1 进入数据库   默认安装会创建postgres 用户,,使用postgres用户,psql命令会直接进入数据库: Bash代码   $ su postgres     ...

  2. Maven系列--pom.xml 配置详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. DrawerLayout带有侧滑功能的布局类(2)

    ActionBarDrawerToggle: 在前一张中我们并没有使用drawLayout.setDrawerListener(); 对应的参数对象就是DrawerLayout.DrawerListe ...

  4. Douglas Crockford: entityify & deentityify

    大神之字符与字符实体的相互转换方法 // & to & if (!String.prototype.entityify) { String.prototype.entityify = ...

  5. 在线演示平台 | Highcharts中文网 (曲线图、区域图、3D图等等)

    http://www.hcharts.cn/ 在线演示平台 | Highcharts中文网

  6. 004--VS C++ 绘制封闭图形

    //全局变量HPEN hPen;HBRUSH hBru[4];int sBru[4] = { HS_VERTICAL, HS_HORIZONTAL, HS_CROSS, HS_DIAGCROSS }; ...

  7. scrapy 错误

    1. 安装win32时候 Unable to find vcvarsall.bat 解决方法: 1.如果你没有安装vc,去微软下个 VS2008 的免费版就能解决此问题. 2.如果你安装的是VS201 ...

  8. skill-如何禁止浏览器的默认行为

    在默认情况下,点击链接,浏览器会向href所指的地址发送请求, 点击表单提交,浏览器会将表单中的数据进行发送 如果要禁止,可以使用如下语句:

  9. Notes of the scrum meeting(11/3)

    meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...

  10. [转载]char * 和char []的区别---之第二篇

    原文地址:http://blog.sina.com.cn/s/blog_74a4593801019keb.html main() { char *p="abc123ABC";//c ...