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. Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决

    备注 在创建证书时候的背景信息: 'keytool -genkeypair' 命令将会创建秘钥对,包括公钥和关联的私钥,然后存储到  keystore 中.这个命令打包公钥为  X.509 v3 自签 ...

  2. Confluence 6 用户目录图例 - 和 Jira 连接到 Crowd

      上面的图:Confluence, JIRA 和其他应用程序连接到 Crowd 作为用户管理. https://www.cwiki.us/display/CONF6EN/User+Managemen ...

  3. Spark-SQL之DataFrame操作

    Spark SQL中的DataFrame类似于一张关系型数据表.在关系型数据库中对单表或进行的查询操作,在DataFrame中都可以通过调用其API接口来实现.可以参考,Scala提供的DataFra ...

  4. ionic2 子页面隐藏去掉底部tabs导航,子页面全占满显示方法(至今为止发现的最靠谱的方法)

    项目中遇到 tabs 字页面 可以用以下代码隐藏的方式: imports: [ BrowserModule, // IonicModule.forRoot(MyApp), HttpModule, Io ...

  5. PDF怎么去除页眉页脚,PDF页眉页脚编辑方法

    我们在使用文件的时候需要编辑页眉页脚的时候,这个时候我们应该怎么做呢,相信别的文件大家都知道怎么编辑了,PDF文件大家都知道吗,最开始接触这个文件的时候小编觉得很难,之后找到技巧之后也并没有很难,今天 ...

  6. 部署MySQL5.7时的权限问题

    本周部署MySQL5.7的时候遇到这样的问题,在初始化的时候,总是失败,并且报错: 2019-01-09T09:47:13.957685Z 0 [ERROR] InnoDB: Operating sy ...

  7. AI-视图组件-五个接口的最终简化版

    五个接口最终版 #url.py # 序列化最贱版本 url(r'^customer/$', views.CustomerView.as_view({"get":"list ...

  8. VOC数据集生成代码使用说明

    #split.py 文件 输入格式为images ,和标签txt文件,txt中的数据为坐标值共8个. import os import numpy as np import math import c ...

  9. hive和hbase比较(整理)

    hive1.可以理解为一种SQL执行引擎,对SQL的支持最终转换为map/reduce任务2.不支持更新.删除操作,但可以插入3.任务不是实时执行,用时一般为数分钟到数小时4.本身可以不存储数据,只存 ...

  10. Linux(centos)系统各个目录的作用详解 推荐

    文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.SHELL脚本.二进制的可执行文件等,可用cat ...