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. 【原创】运维基础之Docker(5)docker部署airflow

    部署方式:docker+airflow+mysql+LocalExecutor 使用airflow的docker镜像 https://hub.docker.com/r/puckel/docker-ai ...

  2. python-GIL、死锁递归锁及线程补充

    一.GIL介绍 GIL全称 Global Interpreter Lock ,中文解释为全局解释器锁.它并不是Python的特性,而是在实现python的主流Cpython解释器时所引入的一个概念,G ...

  3. Codeforces 671D Roads in Yusland [树形DP,线段树合并]

    洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...

  4. ios 本地存储文件夹的使用注意

    文件夹 tmp 属于临时文件夹,不需要自己删除,系统会在应用退出后清空   文件夹 Library 下面的子文件 Caches 也是用来存储的,,但是Library 基本上不会被清除,但是在内存不足的 ...

  5. Confluence 6 修改警告的阈值和表现

    修改警告的阈值 一些警告的阈值是可以被配置的.如果你发现一些阈值很容易就触发警告了,你可以对这些阈值进行调整让你的系统警告不容易被触发. 访问 Recognized System Properties ...

  6. Confluence 6 启用 HTTP 压缩

    在屏幕的右上角单击 控制台按钮 ,然后选择 基本配置(General Configuration) 链接. 在左侧的面板中选择 通用配置(General Configuration). 启用 HTTP ...

  7. Confluence 6 CSS 编辑快速入门

    希望编辑空间的 CSS 样式表: 进入空间后,然后从边栏的底部选择 空间工具(Space tools) > 外观和感觉(Look and Feel) . 然后选择 样式表(Stylesheet) ...

  8. yum -y 与yum有何区别(转载)

    在linux中,经常使用yum来进行软件的安装,更新与卸载,那我们会发现,在使用yum的时候,通常有下面两种指令模式:    ①yum install  xxx     ②yum -y install ...

  9. ActiveMQ消息的消费原理

    消费端消费消息: 在 初识ActiveMQ 中我提到过,两种方法可以接收消息,一种是使用同步阻塞的ActiveMQMessageConsumer#receive方法.另一种是使用消息监听器Messag ...

  10. list的add()方法与addAll()方法简介

    简单描述:月读别人的代码,发现了一个有意思的东西,list的一个方法,addAll(),然后就去度娘了一下,发现这个还挺有用的. 吐槽一下:为什么自己没发现这个方法呢?因为平时自己写list的时候,基 ...