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

【题目大意】

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

【题解】

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

【代码】

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <algorithm>
  4. const int N=4050,M=210;
  5. using namespace std;
  6. int nxt[M],n;
  7. char dict[N][M];
  8. void get_nxt(char *a,int n){
  9. int i,j;
  10. for(nxt[0]=j=-1,i=1;i<n;nxt[i++]=j){
  11. while(~j&&a[j+1]!=a[i])j=nxt[j];
  12. if(a[j+1]==a[i])j++;
  13. }
  14. }
  15. int LongestPre(char *s,int len){
  16. get_nxt(s,len);
  17. for(int i=1;i<n;i++){
  18. char *p=dict[i];
  19. int ans=0;
  20. for(int j=-1;*p;p++){
  21. while(~j&&s[j+1]!=*p)j=nxt[j];
  22. if(s[j+1]==*p){
  23. j++; ans=max(ans,j+1);
  24. }if(j==len-1)j=nxt[j];
  25. }len=min(len,ans);
  26. }return len;
  27. }
  28. int main(){
  29. while(scanf("%d",&n)&&n){
  30. getchar();
  31. for(int i=0;i<n;i++)gets(dict[i]);
  32. int len=strlen(dict[0]),ans=0,pos=0;
  33. for(int i=0;i<len;i++){
  34. int tmp=LongestPre(dict[0]+i,len-i);
  35. if(tmp>=ans){
  36. if(tmp>ans)ans=tmp,pos=i;
  37. else{
  38. bool flag=1;
  39. for(int t=0;t<ans;t++){
  40. if(dict[0][pos+t]>dict[0][i+t])break;
  41. else if(dict[0][pos+t]<dict[0][i+t]){flag=0;break;}
  42. }if(flag)pos=i;
  43. }
  44. }
  45. }if(ans){
  46. for(int i=0;i<ans;i++)putchar(dict[0][pos+i]);
  47. puts("");
  48. }else puts("IDENTITY LOST");
  49. }return 0;
  50. }

  

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. mysql自定义循环函数

    FUNCTION deyes.f_getSplitStringByIndex1_8(stringIn text, delimiter varchar(10), indexIn int) RETURNS ...

  2. 比callback更简洁的链式执行promise

    promise自己理解的也不够深刻,具体知识点不在这里细说了 直接上个例子,清晰明了,自己去悟吧 <script type="text/javascript"> //模 ...

  3. 从零开始PHP学习 - 第一天

    写这个系列文章主要是为了督促自己  每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...

  4. 基本数据结构简介--ath9k网卡驱动开发总结(二)

    ath9k驱动代码主要数据结构概览. (1)在ath9k的驱动中,几乎是最顶层的数据结构是ath_softc,这个数据结构几乎随处可见.ath_softc是硬件与MAC层进行交互的中间载体,很多有用的 ...

  5. Umbraco Content属性

    总算是有个内容还算多的Content的属性介绍,保存一下. https://our.umbraco.org/documentation/Reference/Management-v6/Models/C ...

  6. 深入理解Spring Redis的使用 (二)、RedisTemplate事务支持、序列化

    RedisTemplate api详解 1. RedisTemplate的事务 private boolean enableTransactionSupport = false; private bo ...

  7. sql like 通配符 模糊查询技巧及特殊字符

    最近碰到like模糊匹配的问题,找到一些答案接触迷惑,觉得有知识是自己忽略的,现在整理出来,既强化记忆,又是一次记录,以下转自一篇Blog,关于sql server like的通配符和字符带通配符的处 ...

  8. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  9. poj2328---"right on"进入下一个case的模板(while)

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; ,end=; w ...

  10. poj1799---解析几何

    sin(a)=r/R-r,反三角asin(r/R-r),乘以2n=2pi,去化简,得到r 收获:define pi acos(-1) 这样pi的精度会高很多<math.h>(cos,sin ...