Problem Description
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.
 
Input
The 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. 
 
Output
There should be one line per test case containing the length of the largest string found.
 
Sample Input
2
ABCD
BCDFF
BRCD
2
rose
orchid
 
Sample Output
2
2
题意&思路:和poj 3080 类似
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int nextt[];
bool vis[];
void getnext(string s){
nextt[]=;
int len=s.length();
for(int i=,j=;i<=len;i++){
while(j>&&s[i-]!=s[j]) j=nextt[j];
if(s[i-]==s[j]) j++;
nextt[i]=j;
}
}
bool kmp(string t,string p){
int len1=p.length();
int len2=t.length();
for(int i=,j=;i<=len1;i++){
while(j>&&p[i-]!=t[j]) j=nextt[j];
if(p[i-]==t[j]) j++;
if(j==len2)
return true;
}
return false;
}
string s[];
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>s[i];
int len=s[].length();
//cout<<len<<endl;
int ans=;
for(int i=;i<=len;i++){
for(int j=;j<=len-i;j++){
string temp=s[].substr(j,i);
getnext(temp);
bool f=;
for(int k=;k<=n;k++){
string pre=s[k];
if(kmp(temp,pre)) continue;
reverse(pre.begin(),pre.end());
if(kmp(temp,pre)) continue;
f=;
break;
}
if(f) ans=max(ans,i);
}
}
cout<<ans<<endl;
}
return ;
}

hdu 1238 Substrings(kmp+暴力枚举)的更多相关文章

  1. HDU 5778 abs (暴力枚举)

    abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem De ...

  2. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  3. 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)

    题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...

  4. poj-3080(kmp+暴力枚举)

    题意:给你多个字符串,问你这几个字符串的最长公共子串是哪个,如果有多个,输出字典序最大的那个,如果最长的公共子串长度小于3,输出一个奇怪的东西: 解题思路:首先看数据,数据不大,开始简单快乐的暴力之路 ...

  5. hdu 5510 Bazinga (KMP+暴力标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...

  6. HDU - 5128The E-pang Palace+暴力枚举,计算几何

    第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个 ...

  7. HDU 5339 Untitled (暴力枚举)

    题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...

  8. (KMP 字符串处理)Substrings -- hdu -- 1238

    http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS     Memory Limit:32768KB ...

  9. hdu_2328_Corporate Identity(暴力枚举子串+KMP)

    题目链接:hdu_2328_Corporate Identity 题意: 给你n个串,让你找这n个串的最大公共子串 题解: 串比较小,暴力枚举第一个的子串,然后KMP判断是否可行 #include&l ...

随机推荐

  1. java线程池实现原理

    (1):线程池存在哪些状态,这些状态之间是如何进行切换的呢? (2):线程池的种类有哪些? (3):创建线程池需要哪些参数,这些参数的具体含义是什么? (4):将任务添加到线程池之后运行流程? (5) ...

  2. 【学亮IT手记】使用Map代替switch...case语句

  3. 下拉框插件select2的使用

    它的优点有: 样式还算好看,支持多选,支持索搜 下面来介绍下select2的用法 1.最简单的用法 只需要加载css和js即可使用 <select name="" id=&q ...

  4. C# Note7:MVVM模式之数据绑定

    一.资源说明 (1)本文参考自: 一步步走进WPF的MVVM模式(二):数据绑定 WPF之数据绑定总结 二.正文  数据绑定 (Data Binding)是WPF最重要的特性之一,也是实现 MVVM( ...

  5. LODOP打印控件如何提示用户升级下载安装新版本

    Lodop.C-Lodop在不断完善功能和更新中,新版本修复了很多问题,以及增加很多有利的功能,网站如何更新版本,提示用户下载新版本呢?更新版本很简单,只需要三步:1.替换提示安装部分的自己放置的路径 ...

  6. LODOP中page-break-before:always给div分页

    Lodop中超过超文本打印项高度会自动分页:Lodop打印控件 超文本自动分页Lodop中还有NewPage和NewPageA,用于手动分页:Lodop强制分页LODOP.NewPage()和LODO ...

  7. 使用@Validated分组遇到的坑

    在使用@Validate注解分组校验时,如果指定分组,所有的需要验证的属性都必须添加指定分组才会校验 解决办法: 没有指明分组的属性都属于Default,所以分组接口继承Default就可以解决

  8. 排列组合n选m算法

    找10组合算法,非递归 http://blog.csdn.net/sdhongjun/article/details/51475302

  9. jsp页面给字体加颜色

    jsp页面给字体加颜色<span style="color:red">要加颜色的部分</span>

  10. hdu-5536(字典树)

    题意:给你n个数,让你在n个数中选三个,使得(a1+a2)^a3的值最大,a1!=a2!=a3(下标不等于): 解题思路:01字典树可以写,因为数据小,我们可以先把n个数建一颗字典树,然后两边for找 ...