后缀数组的买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的更多相关文章

  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

    先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

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

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

  4. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

  5. HDU 1403 Longest Common Substring(最长公共子串)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...

  6. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

  7. 【HDOJ】1403 Longest Common Substring

    后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...

  8. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  9. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀自动机)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

随机推荐

  1. Unix/Linux环境C编程入门教程(26) 字符数字那些事儿

    1.gcvt() strtod() strtol() strtoul() toascii() tolower() toupper函数介绍 gcvt(将浮点型数转换为字符串,取四舍五入) 相关函数 ec ...

  2. Linux下安装nfs服务器

    1. 安装nfs服务 $sudo apt-get install nfs-kernel-server portmap 2. 在配置文件/etc/exports中添加以下内容/home/jxhui/nf ...

  3. UIView添加事件

    UIView *loadView = [[UIControl alloc]initWithFrame:CGRectMake(0,0,320,480)]; loadView.backgroundColo ...

  4. android安卓开发问题集 XMPP篇

    1.消息推送查了下资料,后面还是使用了androidpn (1)java.security.KeyStoreException: KeyStore jks implementation not fou ...

  5. linux命令之mv

    linux下的mv即move的意思 该命令的一般形式: mv [选项] 参数1 参数2 选项: -b                如果已存在相同文件名,则覆盖前进行备份 -f             ...

  6. OpenStack IdentityService Keystone V3 API Curl实战

    v3 API Examples Using Curl <Tokens> 1,Default scope 获取token Get an token with default scope (m ...

  7. mongodb启动关闭;

    [正确关闭方法] 方法一 ps  -ef |grep mongodb 找到你要查找的进程号 kill -2  pid    杀掉 方法二 也可以进入到mongo数据库里面进行操作./mongouse ...

  8. MapReduce工作机制

    MapReduce是什么? MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,MapReduce程序本质上是并行运行的,因此可以解决海量数据的计算问题. MapReduce ...

  9. jQuery Custom Selector JQuery自定义选择器

    什么是JQuery的选择器呢,详见JQuery中的Selector: http://docs.jquery.com/Selectors 比如 $("div:contains('John')& ...

  10. 彻底解决TAP(点透)提升移动端点击响应速度

    使用fastclick 尼玛使用太简单了,直接一句: FastClick.attach(document.body); 于是所有的click响应速度直接提升,刚刚的!什么input获取焦点的问题也解决 ...