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

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

SPOJ 1811LCS Longest Common Substring的更多相关文章

  1. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  2. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  3. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  4. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

  5. SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  6. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  7. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  8. SPOJ 1812 Longest Common Substring II

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  9. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

随机推荐

  1. H264的RTP负载打包的数据包格式,分组,分片

    H264的RTP负载打包的数据包格式,分组,分片 1.    RTP数据包格式 RTP报文头格式(见RFC3550 Page12): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ...

  2. 3.3 文件I/O

    错误的解决方法请参考:http://liangruijun.blog.51cto.com/3061169/673776 3.3.2 访问手机中的存储文件夹 3.3.3 读取assets中的文件 pac ...

  3. git拉取远端改变,但是不覆盖本地的修改

    1.git stash 2.git  fetch weixin-old-remote 3.git rebase weixin-old-remote/main151028_wxpay_main15100 ...

  4. PCB MS SQL 排序应用(row_number rank dense_rank NTILE PARTITION)

    一.排序前,准备数据 --表变量 ),流程数 int) insert into @table union all union all union all union all --查看一下 select ...

  5. 七牛php上传下载类,集成官方文档的方法

    <?phpuse Qiniu\Auth;use Qiniu\Storage\UploadManager;class qiniu{ public $_accesskey = null; publi ...

  6. 牛客练习赛19 -E-托米的饮料

    题目描述 好了,现在是小托米的故事啦~~~ 可爱的小托米得到了n瓶饮料. 但他不小心把开盖的工具弄丢了,所以他只能利用饮料瓶来开盖. 已知第i个瓶子的品牌为ai,且其能打开bi品牌的瓶子. 问有几瓶饮 ...

  7. lsit集合去重复 顶级表达式

    updateList = updateList.Where((x, i) => updateList.FindIndex(z => z.ID == x.ID) == i).ToList() ...

  8. 最简单的多线程死锁案例代码(Java语言)

    package com.thread.test; public class DeadLock { private static Object firstMonitor = new Object(); ...

  9. 【C++】cin、cout的效率比scanf和printf低的解决方法

    玩竞赛的同学应该发现了C++中直接调用cout.cin的效率要比printf和scanf的效率要低. 要解决这个问题,只需要在前面加上一句 std::ios::sync_with_stdio(fals ...

  10. (转)基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理

    http://www.cnblogs.com/wuhuacong/p/4777720.html 在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能 ...