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. 2进制,16进制,BCD,ascii,序列化对象相互转换

    public final static char[] BToA = "0123456789abcdef".toCharArray() ; 1.16进制字符串转为字节数组 /** * ...

  2. css 图片平铺

    背景图尺寸(数值表示方式): #background-size{ background-size:200px 100px; } 背景图尺寸(百分比表示方式): #background-size2{ b ...

  3. php+正则将字符串中的字母数字和中文分割

    原文出处 如果一段字符串中出现字母数字还有中文混排的情况,怎么才能将他们区分开呢,经过一番思索,得到了如下代码,分享给大家 如:$str="php如何将字 符串中322的字母数字sf f45 ...

  4. 查询可用的Nuget服务地址

    解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...

  5. 段落排版--对齐(text-aliagn)

    想为块状元素中的文本.图片设置居中样式吗?可以使用text-align样式代码,如下代码可实现文本居中显示.(那么什么是块状元素呢?后面会讲到呢~) h1{ text-align:center; } ...

  6. Objective-C消息机制的原理

    http://desheng.me/2012/03/31/objective-c%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E7%9A%84%E5%8E%9F%E7%90 ...

  7. php中文乱码

    一.         首先是PHP网页的编码 1.     php文件本身的编码与网页的编码应匹配 a.     如果欲使用gb2312编码,那么php要输出头:header(“Content-Typ ...

  8. 『重构--改善既有代码的设计』读书笔记----Introduce Local Extension

    同Introduce Foreign Method一样,很多时候你不能修改编辑原始类,你需要为这些服务类增加一些额外的函数,但你没有这个权限或者入口.如果你只需要一个或者两个外加函数那么你可以放心的使 ...

  9. 反射给对象赋值遇到的问题——类型转换[转http://blog.csdn.net/xiaohan2826/article/details/8536074]

    发布时间:2012-10-25 10:49浏览次数:225 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致.   创建对象实例的 ...

  10. MySql数据库3【优化2】sql语句的优化

    1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...