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
题意:找n个字符串里的公共子串,相反方向的也算公共子串
题解:枚举第一个的子串和后面的进行kmp
(1a的感觉真tm爽)
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=(<<)-,inf=0x3f3f3f3f; int Next[N],slen,plen;
string a[N],ptr,str; void getnext()
{
int k=-;
Next[]=-;
for(int i=;i<slen;i++)
{
while(k>-&&str[k+]!=str[i])k=Next[k];
if(str[k+]==str[i])k++;
Next[i]=k;
}
}
bool kmp()
{
int k=-;
for(int i=;i<plen;i++)
{
while(k>-&&str[k+]!=ptr[i])k=Next[k];
if(str[k+]==ptr[i])k++;
if(k==slen-)return ;
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i=;i<n;i++)cin>>a[i];
int ans=;
for(int i=;i<=a[].size();i++)
{
for(int j=;j<=a[].size()-i;j++)
{
str=a[].substr(j,i);
slen=str.size();
getnext();
bool flag=;
for(int k=;k<n;k++)
{
ptr=a[k];
plen=a[k].size();
if(kmp())continue;
reverse(ptr.begin(),ptr.end());
if(kmp())continue;
flag=;
break;
}
if(flag)ans=max(ans,slen);
}
}
cout<<ans<<endl;
}
return ;
}

hdu1238 kmp的更多相关文章

  1. hdu-1238(kmp+枚举)

    题意:给你n个字符串,问你这里面最长的公共子串的长度是多少,一个公共子串的反串也算,比如样例二: 解题思路:随便找一个字符,枚举它的子串然后跑kmp就行了,很多人的博客都是用string类里面的函数来 ...

  2. hdu1238 Substrings 扩展KMP

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

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

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

  4. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  5. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  6. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  7. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  8. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  9. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

随机推荐

  1. 能让程序做的事情坚决不用人来做——批量修复markdownlint MD034警告

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  2. Linux基础命令---paste

    paste 将指定的文件按照列的方式合并,将结果显示到标准输出设备上,相当于两个并列的cat命令. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.F ...

  3. java android 环境变量配置备忘录

    java android 环境变量配置备忘录,全新重装系统要用到,备忘下来; JAVA_HOME D:\Java\jdk1.6.0_07 CLASSPATH  .;%JAVA_HOME%\lib\dt ...

  4. Django框架----Web框架本质

    Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...

  5. Linux系统的vi命令

    Linux系统的vi命令 vi编辑命令 1,格式: #vi filename 2,用法: //打开或新建文件,并将光标置于第一行首 #vi + filename //打开文件,并将光标置于第n行首 # ...

  6. C_Learning(3)

    / 结构体 / 声明结构体类型的一般形式: struct 结构体名[--表示的是这个结构体的类型] {    成员列表 }; [不要漏掉这个";"] / 声明结构可以放在main函 ...

  7. VC++开机自动启动程序的几种方法 (转载)

    转载:http://blog.csdn.net/zhazhiqiang/article/details/51909703 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能 ...

  8. 为 Android 8.0 添加开机启动脚本【转】

    本文转载自:https://zhuanlan.zhihu.com/p/32868074 本人对于 SELinux for Android 理解不深,下文中的各文件及安全规则虽都是我所编写,但也是一边查 ...

  9. 棋盘状态压缩dp

    状态压缩入门DP整理 只针对入门 一般都是用2进制的方法,压缩成一个数,所以n的范围都会特变小 一些套路 状态一般是很多的,可以搜索或者位运算筛选一下,基本都是这样的吧 当要存两个状态或者数组存不下的 ...

  10. Python time strptime()与time strftime()

    time strftime()接收时间元组,返回表示时间的字符串. time strptime()把时间字符串,解析成一个时间元组. import time t = time.strftime('%Y ...