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的更多相关文章

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

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

  2. 666 专题三 KMP &#38; 扩展KMP &#38; Manacher

    KMP: Problem A.Number Sequence d.求子串首次出现在主串中的位置 s. c. #include<iostream> #include<stdio.h&g ...

  3. 扩展KMP算法

    一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...

  4. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  5. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  6. UVA5876 Writings on the Wall 扩展KMP

    扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...

  7. hdu4333 扩展KMP

    慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...

  8. 扩展KMP

    刘雅琼论文 http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 论文讲的非常详细. 给定母串S,子串T,n=strlen(S),m=st ...

  9. HDU 3336 扩展kmp

    题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...

随机推荐

  1. cookie session localstorage sessionStorage区别

    cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...

  2. ngIf 和 template的结合使用

    前提: 当遇到 一个种情况,一个元素中既可以显示 一个字符串变量,也可以显示一个模板 实现: // html <span class="ant-alert-message" ...

  3. elementUI和iview兼容么

    听说iview的作者居然是91年的,我要赶快加油了. https://zhuanlan.zhihu.com/p/25739512

  4. html css 伪样式

    伪类的分类及作用: 引自W3School教程伪元素的分类及作用: 这里就不进行多的描述,原文地址:http://www.it165.net/design/html/201406/2643.html

  5. python面试题(转自https://www.cnblogs.com/wupeiqi/p/9078770.html)

    第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C++等其他语言的对比? 简述解释型和编译型编程语言? P ...

  6. English trip V1 - 5.That's Amazing! 棒极了! Teacher:Patrick Key: can or can't

    In this lesson you will learn to describe what people can do. 在本课中,您将学习如何描述人们可以做什么. STARTE drive a c ...

  7. Android程序员眼中世界上最遥远的距离

    世界上最遥远的距离,是我在if里你在else里,似乎一直相伴又永远分离: 世界上最痴心的等待,是我当case你是switch,或许永远都选不上自己: 世界上最真情的相依,是你在try我在catch. ...

  8. 『Scipy』常用方法记录

    优化器使用教程 J = lambda wb: self.get_cost_grad(wb, X, Y_one_hot) theta = self.wb_init(X,Y_one_hot) result ...

  9. Hackintosh Power Management

    Also, be aware that hibernation (suspend to disk or S4 sleep) is not supported on hackintosh. You sh ...

  10. JS常用操作方法图表

    截取字符串方法 方法名 参数 返回值 例子 String.prototype.substr() (indexStart, length) 都可为负数,length为负数时自动转为0,length不传默 ...