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 3 ABCD BCDFF BRCD 2 rose orchid
 
Sample Output
2 2
 
直接暴力kmp即可  
有一个函数 :reverse(p2.begin(),p2.end()); 可以直接反转 
注意如果没有则输出0;
 
第一次的写法  虽然艰难的过了 但是时间为500ms
 
#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define N 100+5
#define mod 10007
string s,p;
int can[];
string temp[N];
int nex[N];
string str;
int lenp,lens;
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp-)
{
if(k==-||p[k]==p[j])
nex[++j]=++k;
else k=nex[k];
}
}
int kmp(string s)
{
int lens=s.size();
int j=,i=;
while(i<lens&&j<lenp)
{
if(s[i]==p[j]||j==-)
{
i++;
j++;
}
else j=nex[j];
if(j==lenp)
{
return ;
}
}
return ;
}
int main()
{ int cas;
RI(cas);
while(cas--)
{
int n;RI(n);
int minn=inf;
rep(i,,n)
{
cin>>temp[i];
if(temp[i].size()<minn )minn=temp[i].size();
}
rep(i,,n)
if(temp[i].size()==minn)
{
str=temp[i];
break;
}
int end1=;
int maxx=;
for(lenp=minn;lenp>=;lenp--)
{
if(end1)break;
for(int j=;j<=minn;j++)
if(j+lenp-<minn)
{
int ok=;
p=str.substr(j,lenp);
getnext();
rep(i,,n)
{
can[i]=; if(kmp(temp[i]))
can[i]=;
}
reverse(p.begin(),p.end());
getnext();
rep(i,,n)
{ if(kmp(temp[i]))
can[i]=; if(can[i]==)
{
ok=;break;
}
}
if(ok)
{
maxx=lenp;end1=;
break;
}
}
}
cout<<maxx<<endl;
}
return ;
}
 
下面的为70ms
 因为其实大部分情况下是不匹配的  匹配的情况微乎其微   所以直接两个一起判断即可  即使每次判断都要更行next
 
#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define N 100+5
#define mod 10007
string s,p;
int can[];
string temp[N];
int nex[N];
string str;
int lenp,lens;
void getnext()
{
nex[]=-;
int k=-,j=;
while(j<lenp-)
{
if(k==-||p[k]==p[j])
nex[++j]=++k;
else k=nex[k];
}
}
int kmp(string s)
{
int lens=s.size();
int j=,i=;
while(i<lens&&j<lenp)
{
if(s[i]==p[j]||j==-)
{
i++;
j++;
}
else j=nex[j];
if(j==lenp)
{
return ;
}
}
return ;
}
int main()
{
int cas;
RI(cas);
while(cas--)
{
int n;RI(n);
int minn=inf;
rep(i,,n)
{
cin>>temp[i];
if(temp[i].size()<minn )minn=temp[i].size();
}
rep(i,,n)
if(temp[i].size()==minn)
{
str=temp[i];
break;
}
int maxx=;
int end1=;
for(lenp=minn;lenp>=;lenp--)
{
if(end1)break;
for(int j=;j<=minn;j++)
if(j+lenp-<minn)
{
string p1=str.substr(j,lenp);
string p2=p1;
reverse(p2.begin(),p2.end()); int i;
for(i=;i<=n;i++)
{
int flag=;
p=p1;
getnext();
if(kmp(temp[i]))
flag=;
p=p2;
getnext();
if(kmp(temp[i]))
flag=;
if(flag==)
break;
}
if(i==n+&&maxx<lenp)
maxx=lenp,end1=;
}
}
cout<<maxx<<endl;
}
return ;
}
 
 
 

Substrings kmp的更多相关文章

  1. hdu 1238 Substrings(kmp+暴力枚举)

    Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...

  2. POJ1226 - Substrings(KMP+二分)

    题目大意 给定n个字符串,字符串可逆序可顺序,求它们的最长公共子串 题解 在输入的过程中记录一下最短的那个字符串,然后枚举起点,然后进行二分求出子串末位置,然后再验证是否是公共子串,记录最长的公共子串 ...

  3. A题:Common Substrings(KMP应用)

    原题链接 注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换 #include<cstdio> #include<cstring&g ...

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

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

  5. Codeforces 917F Substrings in a String - 后缀自动机 - 分块 - bitset - KMP

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个字母串,要求支持以下操作: 修改一个位置的字母 查询一段区间中,字符串$s$作为子串出现的次数 Solution 1 Bitset 每 ...

  6. hdu1238 Substrings 扩展KMP

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

  7. PKU 1226 Substrings(字符串匹配+暴搜KMP模板)

    原题大意:原题链接 给出n个字符串,找出一个最长的串s,使s或者s的反转字符串(只要其中一个符合就行)同时满足是这n个串的子串. 对于样例,第一组ABCD   BCDFF  BRCD最长的串就是CD; ...

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

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

  9. Many Equal Substrings CodeForces - 1029A (kmp next数组应用)

    题目大意 题目看样例也能猜到就是输出最短的循环串. 吐槽 明明是div3第一题为啥子还会用到kmp的知识? 解法 这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来.这 ...

随机推荐

  1. MySQL将查询出来的一组数据拼装成一个字符串

    1 前言 由于项目中有一个需求,需要把当日当周的排行榜数据归档,以便后期查询和发放奖励.然而发现,mysql的变量只能存一个变量值,然后如果要储存一条记录,可以使用CONCAT_WS,如果要储存多条记 ...

  2. CXF使用

    一.服务端: 1.web.xml配置 <servlet> <servlet-name>cxf</servlet-name> <servlet-class> ...

  3. JS如何截取一段字符中,某一个字符的前面和后面的字符

    比如这里的1992631934@qq.com如何获取到@前面的1992631934和@后面的qq.com js代码如下: var mail="1992631934@qq.com" ...

  4. liunx 利用nginx 实现负载均衡

    一般采用软件实现负载均衡的有Nginx.apache.nginx 近年来使用频繁,其官网上面显示可以承载5万并发访问量,太牛了. nginx 相比 apache优势明显:Nginx 服务程序比较稳定, ...

  5. nginx之访问控制http_access_module与http_auth_basic_module

    http_access_module 作用 基于IP的访问控制 语法 使用 局限性 解决办法 1. http_x_forwarded_for http_auth_basic_module 作用 基于用 ...

  6. Executor多线程框架使用

    在我们的JDK1.5的时候JAVA推出一款为了更加方便开发的多线程应用而封装的框架(Executor),相比传统的Thread类,Executor更加的方便,性能好,更易于管理,而且支持线程池.一般在 ...

  7. spfa+01 规划

    尼玛的哪里错了.. /* 在有向图上找一个环,使结点权值和/边权和的比例值最大 01规划,设比例为l,那么将每条边的权值改成a[u]-l*w,如果有正权环,则比例l可行 如何判图中存在正权环?将 权值 ...

  8. C++ Primer 笔记——迭代器

    iostream迭代器 1.虽然iostream类不是容器,但是标准库定义了可以用于IO的迭代器.创建一个流迭代器的时候必须指定要读写的类型.我们可以对任何具有输入运算符(>>)的类型定义 ...

  9. C++ Primer 笔记——const 限定符

    1.因为const对象一旦创建后其值就不能再改变,所以const对象必须初始化. 2.默认情况下const对象只在文件内有效,如果想在多个文件之间共享const对象,必须在变量的定义之前添加exter ...

  10. Oracle索引(Index)介绍使用

    1.什么是引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据:Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引:索引由根节点.分支节点和叶子 ...