POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203
后缀数组的买1送2题。。。
HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA。。原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1.。。。不懂可以去看罗大神的论文。。。
就是利用后缀数组模板求最长公共子串。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(a,b,i) for(i=a;i<=b;++i)
#define For(a,b,i) for(i=a;i<b;++i)
#define N 1000000007
using namespace std;
inline void RD(int &ret)
{
char c;
do
{
c=getchar();
}
while(c<'0'||c>'9');
ret=c-'0';
while((c=getchar())>='0'&&c<='9')
{
ret=ret*10+(c-'0');
}
}
inline void OT(int a)
{
if(a>=10)
{
OT(a/10);
}
putchar(a%10+'0');
}
char f[2000005];
int rank[1000005],sa[1000005],top[1000005],tmp[1000005],height[1000005],wa[1000005],wb[1000005];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void makesa(int n)//后缀数组模板
{
int i,j,p=0,*t,*x=wa,*y=wb,m=300;
for(i=0; i<m; i++)
{
top[i]=0;
}
for(i=0; i<n; i++)
{
top[x[i]=f[i]]++;
}
for(i=1; i<m; i++)
{
top[i]+=top[i-1];
}
for(i=n-1; i>=0; i--)
{
sa[--top[x[i]]]=i;
}
for(j=1; p<n; j+=j,m=p)
{
for(p=0,i=n-j; i<n; i++)
{
y[p++]=i;
}
for(i=0; i<n; i++)
{
if(sa[i]>=j)
{
y[p++]=sa[i]-j;
}
}
for(i=0; i<n; i++)
{
tmp[i]=x[y[i]];
}
for(i=0; i<m; i++)
{
top[i]=0;
}
for(i=0; i<n; i++)
{
top[tmp[i]]++;
}
for(i=1; i<m; i++)
{
top[i]+=top[i-1];
}
for(i=n-1; i>=0; i--)
{
sa[--top[tmp[i]]]=y[i];
}
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
{
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
}
void makeheight(int n)
{ int j,i,k;
for(i=1; i<=n; i++)
{
rank[sa[i]]=i;
}
for(i=0,k=0; i<n; height[rank[i++]]=k)
{
for(k?k--:0,j=sa[rank[i]-1]; f[i+k]==f[j+k]; k++);
}
}
int main()
{
int i,l,ll,sum;
while(scanf("%s",f)!=EOF)
{
ll=strlen(f);
l=ll;
f[ll]='&';
scanf("%s",f+l+1);
ll=strlen(f);
makesa(ll+1);//就是这里啊,一语惊醒梦中人
makeheight(ll);
sum=0;
for(i=2;i<ll;++i)
{
if(height[i]>sum)
{
if((sa[i]>l&&sa[i-1]<l)||(sa[i]<l&&sa[i-1]>l))
{
sum=height[i];
}
}
}
OT(sum);
printf("\n");
}
return 0;
}
POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203的更多相关文章
- 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
先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...
- HDU 1403 Longest Common Substring(最长公共子串)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...
- hdu 1403 Longest Common Substring 后缀数组 模板题
题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...
- 【HDOJ】1403 Longest Common Substring
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
随机推荐
- inno setup 跳过(Welcome)欢迎界面
原文 http://zwkufo.blog.163.com/blog/static/25882512010816049549/ 在InnoSetup中,我们很容易用 function ShouldSk ...
- stl_map,set 用法
set: 集合a,b加起来,去重 hdu 1406 #include <iostream> #include<cstdio> #include<set> using ...
- C errors recods
error: unterminated #ifndef 1,权限问题 2,少了#endif
- ios中block中的探究
http://blog.csdn.net/jasonblog/article/details/7756763
- pomelo初探
最近发现了一个比较好玩的东西pomelo.地址:点击打开链接 这个东西是网易开发的一套基于node.js的高性能,分布式游戏服务器框架.这套框架不仅可以用来开发游戏服务器,也可用于开发高实时web应用 ...
- C#初步接触
如同非常多刚開始学习的人一样,刚接触C#的时候,也是一头雾水,学习了好长时间,都搞不清楚一些基本名称是什么.什么是C#?什么是.net?什么是visual studio?它们之间有什么关系?以下我们就 ...
- Cooley-Tukey算法 (蝶形算法)
Cooley-Tukey算法差别于其它FFT算法的一个重要事实就是N的因子能够随意选取.这样也就能够使用N=rS的Radix-r算法了.最流行的算法都是以r=2或r=4为基的,最简单的DFT不须要不论 ...
- DevExpress VCL 2014.1.2 for C++BUILDER XE6
DevExpress VCL 2014.1.2 for C++BUILDER XE6 1)下载 DevExpress VCL 2014.1.2下载链接:http://pan.baidu.com ...
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- 【数学水题】【TOJ4113】【 Determine X】
题目大意: yuebai has a long sequence of integers A1,A2,-,AN. He also has such a function: F(x)=∑i=1N(⌊Ai ...