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

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2 这道题和之前一道题类似,都是多个字符串匹配问题,只是这道题多了倒序的匹配。直接reverse就可以了
遍历第一个串的所有后缀,然后匹配每个字符串的正反序。得到公共匹配的最小。然后枚举所有后缀取最大
 #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int _,n,Next[];
vector<string> t; void prekmp(string s) {
int len=s.size();
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int kmp(string t,string p) {
int lent=t.size(),lenp=p.size();
int i=,j=,ans=-,res;
res=-;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
reverse(t.begin(),t.end());
res=-;
i=,j=;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
return ans;
} int main() {
// freopen("in","r",stdin);
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
string s;
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
string str=t[],tempstr;
int len=str.size(),maxx=-;
for(int i=;i<len;i++) {
tempstr=str.substr(i,len-i);
int ans=0x3f3f3f;
prekmp(tempstr);
for(int j=;j<t.size();j++) {
ans=min(kmp(t[j],tempstr),ans);
}
maxx=max(maxx,ans);
}
printf("%d\n",maxx);
t.clear();
}
}

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. Runtime机制的使用整理

    一.基本概念 1.1.RunTime简称运行时,就是系统在运行的时候的一些机制,其中最主要的是消息机制. 1.2.对于C语言,函数的调用在编译的时候会决定调用哪个函数,编译完成之后直接顺序执行,无任何 ...

  2. openGL 预定义变量04

    OpenGL4.0 GLSL预定义变量 GLSL为不同的渲染阶段定义了一些特定的变量.这些预定义(也叫做内置变量)有特定的属性. 所有的预定义变量都以gl_开头.用户定义的变量不能以此开头. 下面分类 ...

  3. 9-EasyNetQ之基于主题的路由

    RabbitMQ有一个很酷的功能,基于主题的路由,这个功能允许订阅者基于多个条件去过滤消息.一个主题是由点号分隔的单词列表,随消息一同发布.例如:"stock.usd.nyse" ...

  4. SQLiteopenhelper创建database的过程

    首先由于SQLiteOpenHelper是一个抽象类,所以我们要创建一个自己的类实现它,并实现抽象方法, public void onCreate(SQLiteDatabase db) public ...

  5. 关于android写入SD卡数据的学习代码

    String path = "data/data/com.example.qqlogin/login.txt"; FileOutputStream fos = new FileOu ...

  6. 4-5 父节点watcher事件

    三种方式设置watcher:ls.stat.get

  7. Python 网络爬虫 008 (编程) 通过ID索引号遍历目标网页里链接的所有网页

    通过 ID索引号 遍历目标网页里链接的所有网页 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyChar ...

  8. 杭电acm 1108题

    这是一道求两个整数最大公倍数的问题,题目比较简单.... 直接使用穷举法计算,数据不是很大.... #include "iostream" using namespace std; ...

  9. 异常日志记录 DDLog

    项目中如果想把异常捕获再写入文件,有个十分容易使用的库DDLog. 首先导入库,在git上下载. 一:在项目初始化指定全局LogLeve ,一般在xxxapp.m中 staticconstint dd ...

  10. getline()函数的两种用法

    getline()函数的输入流对象可以是标准输入流对象cin,也可以是一个文件输入流对象fin; (1)输入流对象的成员函数(有三个参数,一般除非需要自己选定停止符,并不推荐使用): basic_is ...