SPOJ 1811LCS Longest Common Substring
2024-09-22 08:00:34
后缀自己主动机裸题....
Time Limit: 2000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is simple, for two given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains exactly two lines, each line consists of no more than 250000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn't exist, print "0" instead.
Example
Input: alsdfkjfjkdsal
fdjskalajfkdsla Output: 3
Notice: new testcases added
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int CHAR=26,maxn=251000; struct SAM_Node
{
SAM_Node *fa,*next[CHAR];
int len,id,pos;
SAM_Node(){}
SAM_Node(int _len)
{
fa=0; len=_len;
memset(next,0,sizeof(next));
}
}; SAM_Node SAM_node[maxn*2],*SAM_root,*SAM_last;
int SAM_size; SAM_Node *newSAM_Node(int len)
{
SAM_node[SAM_size]=SAM_Node(len);
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} SAM_Node *newSAM_Node(SAM_Node *p)
{
SAM_node[SAM_size]=*p;
SAM_node[SAM_size].id=SAM_size;
return &SAM_node[SAM_size++];
} void SAM_init()
{
SAM_size=0;
SAM_root=SAM_last=newSAM_Node(0);
SAM_node[0].pos=0;
} void SAM_add(int x,int len)
{
SAM_Node *p=SAM_last,*np=newSAM_Node(p->len+1);
np->pos=len;SAM_last=np;
for(;p&&!p->next[x];p=p->fa)
p->next[x]=np;
if(!p)
{
np->fa=SAM_root;
return ;
}
SAM_Node *q=p->next[x];
if(q->len==p->len+1)
{
np->fa=q;
return ;
}
SAM_Node *nq=newSAM_Node(q);
nq->len=p->len+1;
q->fa=nq; np->fa=nq;
for(;p&&p->next[x]==q;p=p->fa)
p->next[x]=nq;
} void SAM_build(char *s)
{
SAM_init();
int len=strlen(s);
for(int i=0;i<len;i++)
SAM_add(s[i]-'a',i+1);
} char A[maxn],B[maxn]; int main()
{
scanf("%s%s",A,B);
SAM_build(A);
int m=strlen(B),ans=0,temp=0;
SAM_Node *now=SAM_root;
for(int i=0;i<m;i++)
{
int c=B[i]-'a';
if(now->next[c])
{
now=now->next[c];
temp++;
}
else
{
while(now&&!now->next[c])
now=now->fa;
if(now)
{
temp=now->len+1;
now=now->next[c];
}
else
{
temp=0;
now=SAM_root;
}
}
ans=max(ans,temp);
}
printf("%d\n",ans);
return 0;
}
最新文章
- Webmin 安装 (centos7 rpm 方式)
- Enter直接登录
- bzoj3272 3638
- html 包含一个公共文件
- [每日一题jQuery] jQuery选择器总结:进一步过滤、同级操作、后代操作
- Visual Studio 2015 中文企业版及专业版 正式版下载地址 激活秘钥 正版key
- SGU 194 Reactor Cooling ——网络流
- 获取访问者IP
- mysql的一点小错误
- [转帖]LCD与LED的区别之背光原理与优缺点对比介绍
- ES6(promise)_解决回调地狱初体验
- VS2013安装和单元测试
- 第1章 CLR的执行模型
- bootstrap样式
- IPC_Binder_java_1
- 九度OJ 1183 守形数 (模拟)
- JavaScript权威指南--window对象
- Hdu1255 覆盖的面积
- supervisor error: <;class &#39;socket.error&#39;>;, [Errno 110]
- 【leetcode刷题笔记】Regular Expression Matching