后缀自己主动机裸题....

Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]  
[Go Back]   [Status]  

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

[Submit]  
[Go Back]   [Status]  

#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;
}

最新文章

  1. Webmin 安装 (centos7 rpm 方式)
  2. Enter直接登录
  3. bzoj3272 3638
  4. html 包含一个公共文件
  5. [每日一题jQuery] jQuery选择器总结:进一步过滤、同级操作、后代操作
  6. Visual Studio 2015 中文企业版及专业版 正式版下载地址 激活秘钥 正版key
  7. SGU 194 Reactor Cooling ——网络流
  8. 获取访问者IP
  9. mysql的一点小错误
  10. [转帖]LCD与LED的区别之背光原理与优缺点对比介绍
  11. ES6(promise)_解决回调地狱初体验
  12. VS2013安装和单元测试
  13. 第1章 CLR的执行模型
  14. bootstrap样式
  15. IPC_Binder_java_1
  16. 九度OJ 1183 守形数 (模拟)
  17. JavaScript权威指南--window对象
  18. Hdu1255 覆盖的面积
  19. supervisor error: &lt;class &#39;socket.error&#39;&gt;, [Errno 110]
  20. 【leetcode刷题笔记】Regular Expression Matching

热门文章

  1. 10.3.3 WebView的几个常见功能
  2. nodejs安装express
  3. WebService开发-Hessian
  4. 在linux系统中,使用tomcat的shutdown.sh脚本停止应用,但是进程还在的解决办法
  5. 微信小程序左右滑动切换页面示例代码--转载
  6. 5.13redis图形化工具---idea中配置redis密码
  7. Rabbit--ack机制
  8. Android第三方微博、无线传输、动画特效、商城应用等源码
  9. 阶乘问题-----sum随变量改变而改变
  10. 【Oracle】解决oracle sqlplus 中上下左右backspace不能用