hdu1238 Substrings 扩展KMP
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.
找出最长的一个子串,所有串中都出现过它或它的逆序
扩展KMP水题
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; const int maxn=;
int nxt[][maxn],ext[][maxn];
char s[][maxn];
int len[],ans[maxn]; void EKMP(char s[],char t[],int lens,int lent,int c){
int i,j,p,l,k;
nxt[c][]=lent;j=;
while(j+<lent&&t[j]==t[j+])j++;
nxt[c][]=j;
k=;
for(i=;i<lent;i++){
p=nxt[c][k]+k-;
l=nxt[c][i-k];
if(i+l<p+)nxt[c][i]=l;
else{
j=max(,p-i+);
while(i+j<lent&&t[i+j]==t[j])j++;
nxt[c][i]=j;
k=i;
}
} j=;
while(j<lens&&j<lent&&s[j]==t[j])j++;
ext[c][]=j;k=;
for(i=;i<lens;i++){
p=ext[c][k]+k-;
l=nxt[c][i-k];
if(l+i<p+)ext[c][i]=l;
else{
j=max(,p-i+);
while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
ext[c][i]=j;
k=i;
}
}
} int main(){
int T;
scanf("%d",&T);
while(T--){
memset(ans,0x3f,sizeof(ans));
int n;
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%s",s[i]);
for(int i=;i<=n;++i)len[i]=strlen(s[i]);
s[][len[]]='#';
for(int i=;i<=len[];++i)s[][len[]+i]=s[][len[]-i];
s[][len[]+len[]+]=;
len[]=strlen(s[]);
int maxx=;
for(int i=;i<len[];++i){
for(int j=;j<=n;++j){
int cnt=;
EKMP(s[j],s[]+i,len[j],len[]-i,j);
for(int k=;k<len[j];++k){
if(ext[j][k]>cnt)cnt=ext[j][k];
}
if(cnt<ans[i])ans[i]=cnt;
}
if(ans[i]>maxx)maxx=ans[i];
}
printf("%d\n",maxx);
}
return ;
}
hdu1238 Substrings 扩展KMP的更多相关文章
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- 666 专题三 KMP & 扩展KMP & Manacher
KMP: Problem A.Number Sequence d.求子串首次出现在主串中的位置 s. c. #include<iostream> #include<stdio.h&g ...
- 扩展KMP算法
一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- KMP和扩展KMP
文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...
- UVA5876 Writings on the Wall 扩展KMP
扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...
- hdu4333 扩展KMP
慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...
- 扩展KMP
刘雅琼论文 http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 论文讲的非常详细. 给定母串S,子串T,n=strlen(S),m=st ...
- HDU 3336 扩展kmp
题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...
随机推荐
- 如何给wpf的按钮添加背景图片
1:简单实用 <Button Height="143" HorizontalAlignment="Left" Margin="30,34,0,0 ...
- 绑定任意格式的XML文档到WPF的TreeView
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- 20170612xlVBA含方框文档填表
Sub mainProc() Application.ScreenUpdating = False Application.DisplayAlerts = wdAlertsNone 'Dim xlAp ...
- spojPlay on Words
题意:给出几个词语,问能不能接龙. 一开始猜只要所有字母连通,并且只有一个字母出现在开头次数为奇,一个字母末尾为奇,其它偶,就行.后来发现全为偶也行.而且条件也不对,比如ac,ac,ac就不行.实际上 ...
- PHP函数总结 (一)
<?php /** * 原理: * 函数不调用不执行,定义函数时,会将 * 函数放到内存中代码段,当调用函数时去内存 * 中函数名称所在位置中执行函数体,执行完后 * 将控制权移交回给调用函数的 ...
- python-day39--mysql基本操作
1.修改密码: mysqladmin -uroot password 123 2.忘记密码如何修改密码: 1.干掉data目录---> 重新初始化 (不推荐,所有授权信息全部丢失!!!) 2 ...
- hdu-1907-反nim博弈
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- Vim:replace with foobar (y/n/a/q/l/^E/^Y)?
y:to substitute this match n:to skip this match a:to substitute this and all remaining matches q:to ...
- spring的FactoryBean
(以下内容翻译自spring/docs/3.2.18.RELEASE) 为具有工厂属性的对象实现FactoryBean接口. FactoryBean接口是spring IoC 容器实例化逻辑的一点补充 ...
- linux和window是文件挂载
1. 首先在windows下面创建share文件夹并设置共享(右键->属性->共享)2. 确认ubuntu安装了mount.cifs,apt-get install mount.cifs ...