kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
(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 这道题和之前一道题类似,都是多个字符串匹配问题,只是这道题多了倒序的匹配。直接reverse就可以了
遍历第一个串的所有后缀,然后匹配每个字符串的正反序。得到公共匹配的最小。然后枚举所有后缀取最大
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int _,n,Next[];
vector<string> t; void prekmp(string s) {
int len=s.size();
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int kmp(string t,string p) {
int lent=t.size(),lenp=p.size();
int i=,j=,ans=-,res;
res=-;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
reverse(t.begin(),t.end());
res=-;
i=,j=;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
return ans;
} int main() {
// freopen("in","r",stdin);
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
string s;
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
string str=t[],tempstr;
int len=str.size(),maxx=-;
for(int i=;i<len;i++) {
tempstr=str.substr(i,len-i);
int ans=0x3f3f3f;
prekmp(tempstr);
for(int j=;j<t.size();j++) {
ans=min(kmp(t[j],tempstr),ans);
}
maxx=max(maxx,ans);
}
printf("%d\n",maxx);
t.clear();
}
}
kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
		
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
 - kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
		
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
 - kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
		
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
 - kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
		
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
 - kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
		
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
 - kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
		
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
 - kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
		
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
 - kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
		
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
 - kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
		
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
 
随机推荐
- 数据库学习笔记(二)MySQL数据库进阶
			
MySQL 进阶 关于连表 左右连表: join 上下连表: union #自动去重 (当两张表里的数据,有重复的才会自动去重) union all #不去重 #上下连表示例: select sid, ...
 - 2015.3.12Arinc424 Tools中SiniArincCls.csParserFile(string sFile)函数正则表达式理解
			
原文: string RegEx1 = @"(\[ITEM\]\s*=[\S\s]*?(?=\[ITEM\])|\[ITEM\]\s*=[\S\s]*)";//用来识别主记录和后续 ...
 - 初识DDD
			
DDD强调专注于业务问题域的需要:其专业术语.为何开发该软件的关键原因,以及对于业务来说什么才是成功 问题域涉及你当前正在构建软件的主题领域 DDD强调的是,在致力于为大型复杂业务系统创建软件时,专注 ...
 - DAY12-前端之CSS
			
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...
 - MySQL 删除字段数据某关键字后的所有数据
			
),'开发商') WHERE Compay LIKE '%开发商%'; sql附上
 - Windows平台上通过git下载github的开源代码
			
常见指令整理: (1)检查ssh密钥是否已经存在.GitBash. 查看是否已经有了ssh密钥:cd ~/.ssh.示例中说明已经存在密钥 (2)生成公钥和私钥 $ ssh-keygen -t rsa ...
 - springmvc配置式开发下的视图解析器
			
多个视图解析器优先级:
 - Android禁止程序自动旋转的配置
			
在想要禁止的Activity中加入 android:screenOrientation="portrait" 属性,其中,portrait是竖屏,landscape是横屏
 - zabbix监控报错zabbix server is not running: the information displayed may not be current
			
zabbix监控搭建完后打开web界面http://xxx/zabbix报错: zabbix server is not running: the information displayed may ...
 - day17-jdbc 2.jdbc介绍
			
SQL是一种非过程性语言,只能写一条嘛,你写多条不行嘛.每个数据库都有自己的存储过程.你可以做编程,你可以写多条SQL语句把它放到一起.这就是存储过程.然后用的时候一调它就执行这个逻辑结构了.因为多条 ...