博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPOJ 1811LCS Longest Common Substring
阅读量:7050 次
发布时间:2019-06-28

本文共 2566 字,大约阅读时间需要 8 分钟。

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

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: alsdfkjfjkdsalfdjskalajfkdsla Output: 3

Notice: new testcases added

Source

[]   []   []  

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

转载地址:http://ndpol.baihongyu.com/

你可能感兴趣的文章
linux查看系统类型和版本
查看>>
ThinkPHP将上传问件添加到数据库
查看>>
python 不同目录间的模块调用
查看>>
centos7 安装 chrome
查看>>
IOS 关于上传图片裁剪以及压缩,确保高清
查看>>
HDU - 6115 Factory (LCA 倍增)
查看>>
unity客户端与c++服务器之间的简单通讯_1
查看>>
Python_反射
查看>>
Codeforces-963 D Frequency of String
查看>>
MyBatis-mybatis全局映射文件解析
查看>>
WebApi 跨域解决方案 --CORS
查看>>
MySQL系列详解五: xtrabackup实现完全备份及增量备份详解-技术流ken
查看>>
单独编译Android源代码中的模块
查看>>
manjaro安装mysql5.7
查看>>
记录零散的知识点
查看>>
H5上传图片并使用canvas制作海报
查看>>
springmvc学习笔记
查看>>
LRU算法的设计
查看>>
Java util包中常用的类和方法
查看>>
[R] 之 管理工作空间函数
查看>>