You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. 

InputThe first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string. 
OutputThere should be one line per test case containing the length of the largest string found. 
Sample Input

  1. 2
  2. 3
  3. ABCD
  4. BCDFF
  5. BRCD
  6. 2
  7. rose
  8. orchid

Sample Output

  1. 2
  2. 2
    题意:找n个字符串里的公共子串,相反方向的也算公共子串
    题解:枚举第一个的子串和后面的进行kmp
    1a的感觉真tm爽)
  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<iomanip>
  9. #include<cstdlib>
  10. #include<cstring>
  11. #include<iostream>
  12. #include<algorithm>
  13. #define pi acos(-1)
  14. #define ll long long
  15. #define mod 10007
  16. #define ls l,m,rt<<1
  17. #define rs m+1,r,rt<<1|1
  18.  
  19. using namespace std;
  20.  
  21. const double g=10.0,eps=1e-;
  22. const int N=+,maxn=(<<)-,inf=0x3f3f3f3f;
  23.  
  24. int Next[N],slen,plen;
  25. string a[N],ptr,str;
  26.  
  27. void getnext()
  28. {
  29. int k=-;
  30. Next[]=-;
  31. for(int i=;i<slen;i++)
  32. {
  33. while(k>-&&str[k+]!=str[i])k=Next[k];
  34. if(str[k+]==str[i])k++;
  35. Next[i]=k;
  36. }
  37. }
  38. bool kmp()
  39. {
  40. int k=-;
  41. for(int i=;i<plen;i++)
  42. {
  43. while(k>-&&str[k+]!=ptr[i])k=Next[k];
  44. if(str[k+]==ptr[i])k++;
  45. if(k==slen-)return ;
  46. }
  47. return ;
  48. }
  49. int main()
  50. {
  51. ios::sync_with_stdio(false);
  52. cin.tie();
  53. // cout<<setiosflags(ios::fixed)<<setprecision(2);
  54. int t,n;
  55. cin>>t;
  56. while(t--){
  57. cin>>n;
  58. for(int i=;i<n;i++)cin>>a[i];
  59. int ans=;
  60. for(int i=;i<=a[].size();i++)
  61. {
  62. for(int j=;j<=a[].size()-i;j++)
  63. {
  64. str=a[].substr(j,i);
  65. slen=str.size();
  66. getnext();
  67. bool flag=;
  68. for(int k=;k<n;k++)
  69. {
  70. ptr=a[k];
  71. plen=a[k].size();
  72. if(kmp())continue;
  73. reverse(ptr.begin(),ptr.end());
  74. if(kmp())continue;
  75. flag=;
  76. break;
  77. }
  78. if(flag)ans=max(ans,slen);
  79. }
  80. }
  81. cout<<ans<<endl;
  82. }
  83. return ;
  84. }

hdu1238 kmp的更多相关文章

  1. hdu-1238(kmp+枚举)

    题意:给你n个字符串,问你这里面最长的公共子串的长度是多少,一个公共子串的反串也算,比如样例二: 解题思路:随便找一个字符,枚举它的子串然后跑kmp就行了,很多人的博客都是用string类里面的函数来 ...

  2. hdu1238 Substrings 扩展KMP

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  5. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  6. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  7. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  8. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  9. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

随机推荐

  1. addEventListener的click和onclick的区别

    前两节都和addEventListener的click有关,于是在想它与onclick有什么区别呢,自己调试了一下,网上也有相关资料 事件绑定 onclick绑定方式 优点: - 简洁 - 处理事件的 ...

  2. 异常和TCP通讯

    第七章 异常处理 * 异常处理机制中的try-catch * 语法: * try{ * 代码片段 * }catch(XXXException e){ * 当try中的代码片段出现了XXXExcepti ...

  3. spring MVC @Resource不支持Lazy加载及解决方法

    今天迁一系统时发现有个bean使用@Resource注入了另外一个bean,这个被注入的bean是将被deprecated的类,而且只有一两个功能使用到,为了先调整进行测试,增加了@Lazy注解,启动 ...

  4. SCP报错:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    经过google,出现这个问题的原因是,这是ssh的问题, GkFool大神说(第一次使用SSH连接时,会生成一个认证,储存在客户端的known_hosts中) 我的解决办法是: ssh-keygen ...

  5. 20165310 Java实验五《网络编程与安全》

    20165310 Java实验五<网络编程与安全> 任务一 题目:①编写MyBC.java实现中缀表达式转后缀表达式的功能:②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表 ...

  6. 0xc0000005:读取位置时发生访问冲突

    这是空指针,比如: A* a=NULL; a->fun();//会提示标题错误,因为a没有分配空间

  7. 《js高级程序设计》--第三章数据类型

    一.关键字 二.保留字 三.数据类型 (数据类型具有动态性)   1.Undefined 声明变量却未对其加以初始化(赋值) 2.Null null值表示一个空对象指针,而这也正是使用typeof操作 ...

  8. FJUT 奇怪的数列(线性选择算法)题解

    题意:找出无需数列中位数(偶数为两个中位数平均数向下取整) 思路:用nth_element(a + first,a + k,a+ end + 1)找出中位数,复杂度一般为O(n).这个STL能将 [ ...

  9. LightOJ 1296 Again Stone Game(sg函数)题解

    题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...

  10. linux下安装/升级openssl

    (2810)  (1) 安装环境: 操作系统:CentOs7 OpenSSL Version:openssl-1.0.2j.tar.gz 安装: 目前版本最新的SSL地址为 http://www.op ...