[SPOJ-LCS]Longest Common Substring
题目大意:
求两个字符串的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的更多相关文章
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- SPOJ LCS - Longest Common Substring 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982392.html 题目传送门 - SPOJ LCS 题意 求两个字符串的最长公共连续子串长度. 字符串长$\ ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
随机推荐
- 2016.6.20——Plus One
Plus One 本题收获 1.vector<int> 和vector<char>的区别,及与int转换 从vector<int> nums 转换为int res型 ...
- 【codeforces】【比赛题解】#960 CF Round #474 (Div. 1 + Div. 2, combined)
终于打了一场CF,不知道为什么我会去打00:05的CF比赛…… 不管怎么样,这次打的很好!拿到了Div. 2选手中的第一名,成功上紫! 以后还要再接再厉! [A]Check the string 题意 ...
- JDK1.8源码TreeMap
基于红黑树(Red-Black tree)的 NavigableMap 实现:键的排序由构造方法决定:自然排序,Comparator排序:非线程安全(仅改变与现有键关联的值不是结构上的修改):线程安全 ...
- 你的组织使用了 windows defender 应用程序控制来阻止此应用
=============================================== 2018/2/8_第1次修改 ccb_warlock === ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- 15 Defer, Panic, and Recover
Defer, Panic, and Recover 4 August 2010 Go has the usual mechanisms for control flow: if, for, switc ...
- Scrapy命令行工具简介
Windows 10家庭中文版,Python 3.6.4,virtualenv 16.0.0,Scrapy 1.5.0, 在最初使用Scrapy时,使用编辑器或IDE手动编写模块来创建爬虫(Spide ...
- gbdt和xgboost api
class xgboost.XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True, objective= ...
- Little C Loves 3 I
CF#511 div2 A 现场掉分赛(翻车),就是这道题被叉了...qwq 其实就是一道水题: 因为CF有spj,所以直接构建特殊情况就行了. 当 n 是3的倍数的时候,显然 1,1,(n-2) 显 ...
- CVE-2010-2553 Microsoft Windows Cinepak 编码解码器解压缩漏洞 分析
Microsoft Windows是微软发布的非常流行的操作系统. Microsoft Windows XP SP2和SP3,Windows Vista SP1和SP2,以及Win ...