1811. Longest Common Substring

Problem code: LCS

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

后缀自动机SAM的模板题。

重新搞一遍SAM

 /* ***********************************************
Author :kuangbin
Created Time :2013-9-8 23:27:46
File Name :F:\2013ACM练习\专题学习\后缀自动机\new\SPOJ_LCS.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int CHAR = ;
const int MAXN = ;
struct SAM_Node
{
SAM_Node *fa, *next[CHAR];
int len;
int id, pos;
SAM_Node(){}
SAM_Node(int _len)
{
fa = ;
len = _len;
memset(next,,sizeof(next));
}
};
SAM_Node SAM_node[MAXN*], *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 = ;
SAM_root = SAM_last = newSAM_Node();
SAM_node[].pos = ;
}
void SAM_add(int x,int len)
{
SAM_Node *p = SAM_last, *np = newSAM_Node(p->len + );
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 + )
{
np->fa = q;
return;
}
SAM_Node *nq = newSAM_Node(q);
nq->len = p->len + ;
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 = ;i < len;i++)
SAM_add(s[i] - 'a', i+);
}
char str1[MAXN], str2[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%s%s",str1,str2) == )
{
SAM_build(str1);
int len = strlen(str2);
SAM_Node *tmp = SAM_root;
int ans = ;
int t = ;
for(int i = ;i < len;i++)
{
if(tmp->next[str2[i]-'a'])
{
tmp = tmp->next[str2[i]-'a'];
t++;
}
else
{
while(tmp && !tmp->next[str2[i]-'a'])
tmp = tmp->fa;
if(tmp == NULL)
{
tmp = SAM_root;
t = ;
}
else
{
t = tmp->len + ;
tmp = tmp->next[str2[i]-'a'];
}
}
ans = max(ans,t);
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. Android N 多窗口模式,你需要知道的一切
  2. 做的一个HTML表白页面
  3. IntelliJ中的main函数和System.out.println()快捷键
  4. 解决vi/vim中粘贴会在行首多很多缩进和空格的问题
  5. Robot Framework自动化测试(一)---第一个脚本
  6. Word2003使用VBA教程
  7. Mysql 5.6 新特性(转载)
  8. 从某一日期开始过day天的日期
  9. Thinkphp 模版
  10. 牟大哥:《App自我促销》连载2 直立人迁移走
  11. ImageMagick利用蒙版合成图片
  12. Python Counter class
  13. [HNOI2013]消毒
  14. 【伯乐在线】这些 Git 技能够你用一年了
  15. shell的exec命令
  16. 简单透析cookies,sessionStorage和localStorage
  17. 32.Mysql Cluster
  18. qhfl-2 ContentType组件
  19. 『翻译』Access USB Devices on the Web
  20. CentOS 5.5 防火墙开启、关闭以及开放指定端口

热门文章

  1. SQLite数据库初步
  2. html5拖拽初窥
  3. 洛谷P3367并查集
  4. python包安装-centos7/windows
  5. c语言双向循环链表
  6. 《精通Python设计模式》学习之工厂方法
  7. OpenCV处理直方图
  8. abtest分流随机链接方法(javascript)
  9. 使用scss + react + webpack + es6实现幻灯片
  10. POJ - 1743 后缀自动机