【题目链接】 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. 有关MyISAM引擎的锁定机制

    本文介绍下,mysql数据库中MyISAM引擎的锁定机制的相关知识,感兴趣的朋友可以参考下. 本节内容: MyISAM引擎的锁定机制 在mysql数据库中,MyISAM存储引擎适合于读频率远大于写频率 ...

  2. Customizing Zend Studio Using the Welcome Page

    Customizing Zend Studio Using the Welcome Page Zend Studio enables you to add or remove plugins from ...

  3. ios 应用程序之间的跳转(内置程序的实现)

    ios 应用程序之间的跳转(内置程序的实现) 一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URLtypes,再展开I ...

  4. Storm并发度和Grouping方式

    Storm并发度和Grouping方式 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans ...

  5. 如何正确理解正则表达式中的分隔符 \b

    前言:好久不见,博客园. 最近在学习研究regex,其中有个特迷惑自己的知识点是分隔符 ( word boundary) [\b] (注:为了方便,后文都以[]来包含字符,并不是reg规则里面的[] ...

  6. 非常值得学习的java 绘图板源代码

    package minidrawpad; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; impor ...

  7. Microsoft Project 2010 简体中文专业版 + 永久激活密钥

    Microsoft Project 2010 简体中文专业版 + 永久激活密钥 (2014-02-17 11:44:16) 转载▼ 标签: 文化 分类: 学习 Microsoft Project已成为 ...

  8. Cortex-M3学习日志(三)-- 外部中断0

    无论是哪款单片机应该都有对应的中断的功能,中断在嵌入式系统的地位毋庸置疑.LPC1768微处理器包括4个外部中断,分别是EINT0.EINT1.EINT2.EINT3对应的引脚分别是P2.10~P2. ...

  9. C语言入门(9)——局部变量与全局变量

    变量有效性的范围称变量的作用域.C语言中所有的量都有自己的作用域.变量说明的方式不同,其作用域也不同. C语言中的变量,按作用域范围可分为两种,即局部变量和全局变量.   局部变量 局部变量也称为内部 ...

  10. poj2196---Specialized Four-Digit Numbers

    #include <stdio.h> #include <stdlib.h> int main() { int i,sum,sumOfH,sumOfd,tmp1,tmp2,tm ...