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

Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.

 
Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.

 
Output
For each test case, you have to tell the length of the Longest Common Substring of them.
 
Sample Input
banana
cianaic
 
Sample Output
3
 
  这题求最长公共字串,简单的DP并不可以搞定,因为数据范围len<=10^5,所以我用后缀数组做的。
  这里将两个字符串连起来,中间用特殊符号隔开,只要两个在排序上连续的后缀的起点在断点两边,就用其更新答案。
 
 #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的更多相关文章

  1. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  2. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

  3. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  4. Longest Common Substring II SPOJ - LCS2 (后缀自动机)

    Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...

  5. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  8. 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, \(\ ...

  9. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

随机推荐

  1. MVC+EF 的增删改查操作

    1. //创建EF映射对象数据集 static Models.db_JiaoYouEntities DbDeleteData = new Models.db_JiaoYouEntities(); 2. ...

  2. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  3. Eclipse代码自动填充.

    在默认情况下,Eclipse只在程序员输入“.”并用ALT+/组合键强制调用编码提示功能 我们可以通过少量配置,让Eclipse更聪明,实现完全自动编码提示:1.在你的“工作空间”下找到下在文件.me ...

  4. C语言中,如何通过socket得到对端IP地址

    struct sockaddr_in clientaddr1; memset(&clientaddr1, 0x00, sizeof(clientaddr1)); socklen_t nl=si ...

  5. OpenSSL安装及目录介绍

    1. 下载安装OpenSSL 2. 根据需要下载安装Visual C++ Redistributable 3. 安装完成OpenSSL后,目录结构如下所示 其中,bin文件夹下是OpenSSL主程序的 ...

  6. 最简单的基于FFmpeg的移动端例子:IOS 视频解码器-保存

    ===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...

  7. iOS英文 汉化,如调用相册,相机改“cancel”,“photos”为“取消”,“相机”

    调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文,纠结半天,最终在info.plist设置解决问题. 只需要改三个地方: 1.plist文件中: 2. ...

  8. C#入门经典(第五版)学习笔记(一)

    ---------------变量和表达式---------------赋值运算符:+=:-=:*=:/=:%=例如:i+=j 相当于 i=i+j i-=j 相当于 i=i-j以此类推 按位运算符:& ...

  9. ASP.NET页面跳转的三种方法比较

    在ASP.NET下,经常需要在页面之间跳转,下面我们来分别介绍一下关于.NET中Response.Redirect(),Sever.Execute(),Server.Transfer() 三种页面跳转 ...

  10. Python Tutorial 学习(七)--Input and Output

    7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...