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
  3.  
  4. 这道题和之前一道题类似,都是多个字符串匹配问题,只是这道题多了倒序的匹配。直接reverse就可以了
    遍历第一个串的所有后缀,然后匹配每个字符串的正反序。得到公共匹配的最小。然后枚举所有后缀取最大
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7. int _,n,Next[];
  8. vector<string> t;
  9.  
  10. void prekmp(string s) {
  11. int len=s.size();
  12. int i,j;
  13. j=Next[]=-;
  14. i=;
  15. while(i<len) {
  16. while(j!=-&&s[i]!=s[j]) j=Next[j];
  17. Next[++i]=++j;
  18. }
  19. }
  20.  
  21. int kmp(string t,string p) {
  22. int lent=t.size(),lenp=p.size();
  23. int i=,j=,ans=-,res;
  24. res=-;
  25. while(i<lent) {
  26. while(j!=-&&t[i]!=p[j]) j=Next[j];
  27. ++i;++j;
  28. res=max(res,j);
  29. }
  30. ans=max(ans,res);
  31. reverse(t.begin(),t.end());
  32. res=-;
  33. i=,j=;
  34. while(i<lent) {
  35. while(j!=-&&t[i]!=p[j]) j=Next[j];
  36. ++i;++j;
  37. res=max(res,j);
  38. }
  39. ans=max(ans,res);
  40. return ans;
  41. }
  42.  
  43. int main() {
  44. // freopen("in","r",stdin);
  45. for(scanf("%d",&_);_;_--) {
  46. scanf("%d",&n);
  47. string s;
  48. for(int i=;i<n;i++) {
  49. cin>>s;
  50. t.push_back(s);
  51. }
  52. string str=t[],tempstr;
  53. int len=str.size(),maxx=-;
  54. for(int i=;i<len;i++) {
  55. tempstr=str.substr(i,len-i);
  56. int ans=0x3f3f3f;
  57. prekmp(tempstr);
  58. for(int j=;j<t.size();j++) {
  59. ans=min(kmp(t[j],tempstr),ans);
  60. }
  61. maxx=max(maxx,ans);
  62. }
  63. printf("%d\n",maxx);
  64. t.clear();
  65. }
  66. }
  1.  

kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  4. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  5. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

随机推荐

  1. PDM/CDM中进行搜索

    Option   Explicit ValidationMode   =   True InteractiveMode =   im_Batch Dim   mdl   '当前model '获取当前活 ...

  2. leetcode429

    这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...

  3. eclipse 报错:GC overhead limit exceeded

    还是eclipse内存问题 修改eclipse.ini -Xms512m -Xmx1024m 必要的情况下, 添加 -XX:MaxPermSize=1024M  表示在编译文件时一直占有最大内存

  4. 如何让DIALOG点击确定按钮之后由于数据不合法不关闭

    public void SetDialogIsClose(DialogInterface pDialog, Boolean pisClose) { try { Field _Field = pDial ...

  5. springBoot数据库jpa+对接mybatis

    1  spring Data jpa hibernate引领数据访问技术,使用orm对象关系映射来进行数据库访问,通过模型和数据库进行映射,通过操作对象实现对数据库操作,把数据库相关操作从代码中独立出 ...

  6. day35-hibernate映射 03-Hibernate持久态对象自动更新数据库

    持久态对象一个非常重要的能力:自动更新数据库. package cn.itcast.hibernate3.demo1; import static org.junit.Assert.*; import ...

  7. Angular22 HttpClient的使用

    1 HttpClient介绍 HttpClient时Http的演进,注意:Http在@angular/http中,而HttpClient在@angular/common/http中: 使用前需要在模块 ...

  8. Opengl创建几何实体——四棱锥和立方体

    //#include <gl\glut.h>#include <GL\glut.h>#include <iostream> using namespace std; ...

  9. ROS Learning-022 learning_tf-06(编程) 现在与过去中穿梭 (Python版) --- waitForTransformFull() 函数

    ROS Indigo learning_tf-06 现在与过去中穿梭 (Python版) - waitForTransformFull() 函数 我使用的虚拟机软件:VMware Workstatio ...

  10. Python Matplotlib.plot Update image Questions

    1. 最近在测试一款设备,采集了一些设备后需要一帧一帧显示图像,经常使用Python,所以选用了Matplotlib进行图像操作 数据结构: timesatamp polar_distance hor ...