后缀数组:HDU1043 Longest Common Substring
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5375 Accepted Submission(s): 1910
For example:
str1 = banana
str2 = cianaic
So the Longest Common Substring is "ana", and the length is 3.
Process to the end of file.
cianaic
#include <iostream>
#include <cstring>
#include <cstdio> const int maxn=;
int Ws[maxn],Wv[maxn],Wa[maxn],Wb[maxn],lcp[maxn],sa[maxn],rank[maxn],r[maxn];
bool cmp(int *p,int i,int j,int l)
{return p[i]==p[j]&&p[i+l]==p[j+l];} void DA(int n,int m)
{
int i,j,p,*x=Wa,*y=Wb;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)
sa[--Ws[x[i]]]=i;
for(j=,p=;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++)y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j; for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)Wv[i]=x[y[i]];
for(i=;i<n;i++)++Ws[Wv[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[Wv[i]]]=y[i];
for(std::swap(x,y),p=,i=,x[sa[]]=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void LCP(int n)
{
int i,j,k=;
for(i=;i<=n;i++)
rank[sa[i]]=i; for(i=;i<n;lcp[rank[i++]]=k)
for(k?--k:k,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
}
char str1[maxn>>],str2[maxn>>];
int main()
{
int i,len1,len2,ans;
while(~scanf("%s",str1)){
for(len1=;str1[len1];len1++)
r[len1]=str1[len1];
r[len1]='$';
scanf("%s",str2);
for(len2=;str2[len2];len2++)
r[len1+len2+]=str2[len2];
DA(len1+len2+,);
LCP(len1+len2+);
ans=;
for(i=;i<len1+len2+;i++)
if(sa[i]<len1&&sa[i-]>len1||sa[i-]<len1&&sa[i]>len1)
ans=std::max(ans,lcp[i]);
printf("%d\n",ans);
}
return ;
}
后缀数组:HDU1043 Longest Common Substring的更多相关文章
- hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- Longest Common Substring II SPOJ - LCS2 (后缀自动机)
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
随机推荐
- unlocker208安装之后看不到Apple macos选项,解决办法.
前段时间升级了win10,最新的unlocker208安装以后看不到mac os的选项,为什么呢?我们在管理员允许win-install.cmd的过程中,会在cmd中看到这么一句话:LookupErr ...
- 《REWORK》启示录 招聘笔杆子——程序员为什么值得写博客
Hire Great Writers 仿佛这是写给自己看的,不过这在其中也有着相当有趣的意义 .虽然自己算是一个能写的人,或许这算是一种不算才华的才华,写博文的意义通常不会在于去描述自己怎样,怎样.通 ...
- Struts.properties(转)
原文地址:http://blog.csdn.net/wfcaven/article/details/5937567 Struts2提供了很多可配置的属性,通过这些属性的设置,可以改变框架的行为,从而满 ...
- HDU 4296 Buildings(贪心)
题意: 给定n个建筑物,每个建筑物都有两个属性w, s,每个建筑物都有一个PDV = (Σw j)-s i .意思就是它上面的所有的w相加减去它的s,让求怎么放置这个建筑物使得每个建筑物当中PDV最大 ...
- webfont自定义字体的实现方案
对于做Web前端的人来说,现在不知道webfont为何物似乎显得有点low了.webfont固然可爱,但似乎仍只可远观,不可亵玩.原因就在于中文字体库体积庞大,远比26个字母来的复杂.于是有同学就说了 ...
- SQL server抽疯后修改sa密码无法成功的处理办法
今天上班打开电脑,发现尼玛所有项目启动后都报错,原因是说数据库sa的验证错误,无法进行数据库链接等等东西,简单地说---SQL server抽疯了!!!:( 昨天还好好的.而且没有修改过东西.为啥会出 ...
- "库未注册"(Library not registered)异常.
启发链接:http://social.msdn.microsoft.com/Forums/vstudio/en-US/f25b80bc-ecd4-4c37-8be3-9106a765b072/libr ...
- 怎么捕获和记录SQL Server中发生的死锁
我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...
- ios 动画效果CATransition笔记
初学ios开发,很多概念还不清楚,所以只有边学边做例子.又怕学了后面忘了前面,因此用自己的博客来纪录自己的学习历程,也是对自己学习不要懈怠做个监督. 刚学ios做动画效果.因为ios封装得很好,实现i ...
- 【HDU3802】【降幂大法+矩阵加速+特征方程】Ipad,IPhone
Problem Description In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which ...