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. Ruby实现wordCounter

    做解密发现的: m={} File.open("test1.txt") do |f| f.each_line do |line| line.chomp! #去除每行结尾的回车(\n ...

  2. Python判断是否是数字(无法判断浮点数)(已解决)

    s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...

  3. SQL基础篇----select语句与排序问题

    一.检索--输出所有的列 SELECT * FROM my_friends WHEREfirst_name = 'cake'; 知识点1 * 代表选择出所有的行-----(什么行呢?)就是first_ ...

  4. MVC4.0 利用IActionFilter实现简单的后台操作日志功能

    首先我们要了解MVC提供了4种常用的拦截器:IActionFilter(Action拦截器接口).IExceptionFilter(异常拦截器接口).IResultFilter(Result拦截器接口 ...

  5. bootstrap API地址

    http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#pagination-table

  6. JS如何获取多个相同class标签并分别再输出各自的文本

    function getClass(boo) { var span = $("span.w");//获取所有的span标签 <span class="w" ...

  7. CRC校验算法

    typedef unsigned char UCHAR;typedef unsigned char BOOL; /* 计算cnt字节数据的crc,最后一个字节的低7比特必须是0,实际上求的是(cnt× ...

  8. powerdesigner 技巧

    1.修改建表脚本生成规则.如果每个表格都有相同的字段,可以如下修改: Database -> Edit Current DBMS 展开 Script -> Object -> Tab ...

  9. Android -- 获取汉字的首字母

    转换                                                                                             获取一个汉 ...

  10. 【每日scrum】5.3

    团队的Backlog 初期总目标:完成需求分析,做好软件前期的一切准备. 任务 认领人 估算完成时间 查找铁大离线电子地图 验证地图的准确性 万彤 司新红 3天 接口需求 (用户界面和软件接口等) 曹 ...