题目大意:
  求两个字符串的LCS。

思路:
  对其中一个字符串构建SAM,然后用另一个字符串在里面匹配,按照SAM的边一直往下走,匹配到的连续的字符数就是公共字串的长度。

 #include<string>
#include<cstring>
#include<iostream>
std::string s,t;
class SuffixAutomaton {
private:
static const int SIGMA_SIZE=;
int idx(const char ch) {
return ch-'a';
}
struct State {
State *link,*go[SIGMA_SIZE];
int len;
State(const int l) {
link=NULL;
len=l;
memset(go,,sizeof go);
}
};
State *root,*last;
void extend(const char ch) {
int w=idx(ch);
State *p=last;
State *new_p=new State(p->len+);
while(p!=NULL&&p->go[w]==NULL) {
p->go[w]=new_p;
p=p->link;
}
if(p==NULL) {
new_p->link=root;
} else {
State *q=p->go[w];
if(q->len==p->len+) {
new_p->link=q;
} else {
State *new_q=new State(p->len+);
memcpy(new_q->go,q->go,sizeof q->go);
new_q->link=q->link;
q->link=new_p->link=new_q;
while(p!=NULL&&p->go[w]==q) {
p->go[w]=new_q;
p=p->link;
}
}
}
last=new_p;
}
public:
void build(std::string &s) {
last=root=new State();
for(std::string::iterator i=s.begin();i<s.end();i++) extend(*i);
}
int match(std::string &s) {
int ret=,tmp=;
State *p=root;
for(std::string::iterator i=s.begin();i<s.end();i++) {
int w=idx(*i);
if(p!=NULL&&p->go[w]!=NULL) {
p=p->go[w];
tmp++;
} else {
while(p!=NULL&&p->go[w]==NULL) p=p->link;
if(p==NULL) {
p=root;
tmp=;
} else {
tmp=p->len+;
p=p->go[w];
}
}
ret=std::max(ret,tmp);
}
return ret;
}
};
SuffixAutomaton sam;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cin>>s;
sam.build(s);
std::cin>>s;
std::cout<<sam.match(s)<<std::endl;
return ;
}

[SPOJ-LCS]Longest Common Substring的更多相关文章

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

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

  2. SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机

    Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...

  3. SPOJ LCS - Longest Common Substring 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...

  4. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

  5. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  6. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

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

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

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

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

  9. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  10. 【SPOJ】Longest Common Substring II

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

随机推荐

  1. Strusts2笔记8--文件的上传和下载

    文件的和上传和下载: (1)文件的上传: Struts是通过拦截器实现文件上传的,而默认拦截器栈中包含了文件上传拦截器,故表单通过Struts2可直接将文件上传,其底层是通过apache的common ...

  2. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  3. mysql -> 事务&事务锁_09

    事务的特性 redo undo 锁的隔离级别

  4. 2->集群架构主机优化流程

    集群架构优化流程: 有道笔记分享链接

  5. WPF Devexpress GridControl Value与Display转换

    直入主题吧!开发中往往需要将代码转换成中文显示在表格中. 如下图 下面就直接贴代码了. C#代码 using System; using System.Collections.Generic; usi ...

  6. L1和L2特征的适用场景

    How to decide which regularization (L1 or L2) to use? Is there collinearity among some features? L2 ...

  7. 安装node版本管理工具之NVM

    nvm是个啥?nvm是一个可以让你在同一台机器上安装和切换不同版本node的工具. 你可能会问,为什么会有这个工具?有时候在开发的时候,对node版本有强制要求,有的要求用最新版本,有的要求用稳定版本 ...

  8. java基础40 可变参数、自动装箱和自动拆箱

    一.可变参数 可变参数是jdk1.5新特性 1.1.可变参数的格式 数据类型...变量名 // 数据类型...变量名public static void sum(int...arr){ } 1.2.可 ...

  9. 使用Netty4实现基本的消息分发

    示例工程代码 可从附件下载 具体的说明和用法在后面介绍 需求与目的 一个游戏服务端需要处理各种业务逻辑,每一种业务逻辑都对应着一个请求消息和一个响应消息.那么服务端需要把这些不同的消息自动分发到对应的 ...

  10. 80端口被System占用 造成Apache不能启动的解方案

    运行netstat -aon | findstr :80 ,发现pid是4的进程占用着80端口,这还是一个系统进程,kill不掉.所以只能另想办法: 1.打开注册表:regedit 2.找到:HKEY ...