【题目链接】 http://poj.org/problem?id=3450

【题目大意】

  求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的。

【题解】

  我们对第一个串的每一个后缀和其余所有串做kmp,取匹配最小值的最大值就是答案。

【代码】

#include <cstring>
#include <cstdio>
#include <algorithm>
const int N=4050,M=210;
using namespace std;
int nxt[M],n;
char dict[N][M];
void get_nxt(char *a,int n){
int i,j;
for(nxt[0]=j=-1,i=1;i<n;nxt[i++]=j){
while(~j&&a[j+1]!=a[i])j=nxt[j];
if(a[j+1]==a[i])j++;
}
}
int LongestPre(char *s,int len){
get_nxt(s,len);
for(int i=1;i<n;i++){
char *p=dict[i];
int ans=0;
for(int j=-1;*p;p++){
while(~j&&s[j+1]!=*p)j=nxt[j];
if(s[j+1]==*p){
j++; ans=max(ans,j+1);
}if(j==len-1)j=nxt[j];
}len=min(len,ans);
}return len;
}
int main(){
while(scanf("%d",&n)&&n){
getchar();
for(int i=0;i<n;i++)gets(dict[i]);
int len=strlen(dict[0]),ans=0,pos=0;
for(int i=0;i<len;i++){
int tmp=LongestPre(dict[0]+i,len-i);
if(tmp>=ans){
if(tmp>ans)ans=tmp,pos=i;
else{
bool flag=1;
for(int t=0;t<ans;t++){
if(dict[0][pos+t]>dict[0][i+t])break;
else if(dict[0][pos+t]<dict[0][i+t]){flag=0;break;}
}if(flag)pos=i;
}
}
}if(ans){
for(int i=0;i<ans;i++)putchar(dict[0][pos+i]);
puts("");
}else puts("IDENTITY LOST");
}return 0;
}

  

POJ 3450 Corporate Identity(KMP)的更多相关文章

  1. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

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

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

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

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

  4. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  5. HDU - 2328 Corporate Identity(kmp+暴力)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...

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

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

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

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

  8. poj 3450 Corporate Identity

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

  9. POJ 2185 Milking Grid(KMP)

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4738   Accepted: 1978 Desc ...

随机推荐

  1. QF——UI之UIViewController

    程序一经启动,AppDelegate的实例就会创建一个充满屏幕的window,它是App唯一的,一个App对应一个window.window是UIWindow类型的,继承于UIView,是种特殊的UI ...

  2. Swift 控制流

    Swift 1,顺序结构 2,分支结构 switch 中每一个case块完成后会自动终止switch语句, 不用手动终止 case 可有多值,如 case "A","a& ...

  3. Android 数据适配器

    把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上.   arrayAdapter(数组适配器):      用于绑定一些格式单一的数据,数据源:数据或者集合.   private Li ...

  4. activiti框架 数据库设计说明

    1.结构设计 1.1.    逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...

  5. js、css、html判断浏览器的各种版本

    利用正则表达式来判断ie浏览器版本 判断是否IE浏览器 if (document.all) { alert("这个是ie浏览器");} 判断是否IE6浏览器 方法一:if ( /M ...

  6. C#开发者通用性代码审查清单

    这是为C#开发者准备的通用性代码审查清单,可以当做开发过程中的参考.这是为了确保在编码过程中,大部分通用编码指导原则都能注意到.对于新手和缺乏经验(0到3年工作经验)的开发者,参考这份清单编码会很帮助 ...

  7. js屏蔽浏览器(IE和FireFox)的刷新和右键等功能

    //一.js屏蔽浏览器(IE和FireFox)的刷新功能 document.onkeydown=function() { if ((window.event.keyCode==116)|| //屏蔽 ...

  8. python初学笔记(二)

    注释: 任何时候,我们都可以给程序加上注释.注释是用来说明代码的,给自己或别人看,而程序运行的时候,Python解释器会直接忽略掉注释,所以,有没有注释不影响程序的执行结果,但是影响到别人能不能看懂你 ...

  9. Oracle EBS-SQL (GL-5):从发票追溯到接收

    SELECT destination_type_code, distribution_line_number, line_type, amount,vat_code, tax_code_id, tax ...

  10. 【Windows 8 Store App】学习二:ResourceLoader

    原文 http://www.cnblogs.com/java-koma/archive/2013/05/22/3093308.html 在项目开发时,通常有一些资源信息需要存储起来,比如请求的URL, ...