●POJ 2774 Long Long Message】的更多相关文章

题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 36438   Accepted: 14614 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece o…
题目链接:POJ - 2774 题目分析 题目要求求出两个字符串的最长公共子串,使用后缀数组求解会十分容易. 将两个字符串用特殊字符隔开再连接到一起,求出后缀数组. 可以看出,最长公共子串就是两个字符串分别的一个后缀的 LCP ,并且这两个后缀在 SA 中一定是相邻的. 那么他们的 LCP 就是 Height[i] ,当然,Height[i] 的最大值不一定就是 LCS ,因为可能 SA[i] 和 SA[i-1] 是在同一个字符串中. 那么判断一下,如果 SA[i] 与 SA[i - 1] 分别…
Long Long Message   Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a bi…
Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 24756   Accepted: 10130 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is ge…
Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21228   Accepted: 8708 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes…
题意:输入2个长度不超过100000的字符串,问它们最长公共子串的长度. 题目链接:http://poj.org/problem?id=2774 ——>>后缀数组!后缀数组!-从LJ的<训练指南>,到许智磊的论文+PPT,吉大的模版,学长的博客,这路还真不容易走... 最后决定用LJ<训练指南>的写法,感觉挺精辟的. 合并两个串,中间放一个特殊字符,根据条件(len为第一个串的长度,n为合并后串的长度): (sa[i] >= 0 && sa[i]…
[题目链接] http://poj.org/problem?id=2774 [题目大意] 求最长公共子串 [题解] 将两个串中间嵌一个字符相连,求一遍后缀数组 如果排名相邻的两个后缀的开端是分属于两个串的, 那么他们的最长公共前缀就可以用来更新答案 [代码] #include <cstdio> #include <cstring> using namespace std; const int N=4000010; int n,rank[N],sa[N],h[N],tmp[N],cn…
题链: http://poj.org/problem?id=2774题解: 后缀自动机 使用后缀自动机匹配,思路如下: 即如果当前的x字符匹配失败了,就可以从当前已经匹配的串的后缀去继续匹配. 然后不难发现,对于失配的状态s的parent[s]恰好是可能能够继续匹配x且Right集合是最小的包含s的Right集合(即允许的合法长度除了s外是最长的)的状态. 所以就沿着parent指针向上跳,直到trans[*][x]存在或者到了0号节点. 代码: #include<cstdio> #inclu…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 34473   Accepted: 13834 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days…
后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就是利用后缀数组模板求最长公共子串. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<cmat…
[题目分析] 用height数组RMQ的性质去求最长的公共子串. 要求sa[i]和sa[i-1]必须在两个串中,然后取height的MAX. 利用中间的字符来连接两个字符串的思想很巧妙,记得最后还需要空一个位置避免冲突. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set>…
借用罗大神的模板,开始搞后缀数组 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define N 222222 /****后缀数组模版****/ #define F(x)((x)/3+((x)%3==1?0:tb)) //F(x)求出原字符串的suffix(x)在新的字符串中的起始位置 #define G(…
题目链接 题意:给两个长度不超过1e5的字符串,问两个字符串的连续公共子串最大长度为多少? 思路:两个字符串连接之后直接后缀数组+LCP,在height中找出max同时满足一左一右即可: #include<iostream> #include<cstdio> #include<cstring> #include<string.h> #include<algorithm> #include<map> #include<queue&…
题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到height数组,由于随意两个后缀的公共前缀必然是某些height值中的最小值,而这个值假设最大则一定是height中的最大值.在此题中还要注意height最大一定要在两个值所代表的后缀分属不同的字符串地前提下. #include<cstdio> #include<cstring> #incl…
题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长度字串,二分查找在第一个母串的子串中存不存在. 代码: #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cst…
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 \(Solution\) 任何一个子串一定是某个后缀的前缀 可以将两个字符串拼在一起,中间用一个从未出现过的字符隔开,这样ht[]的最大值就是答案? 不一定,最大的ht[]可能是由同一个字符串得到的,判一下属于哪个字符串即可 //3772K 516MS //SPOJ:26M 0.11s(N=5e5)…
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 确实比后缀数组快多了(废话→_→). \(Description\) 求两个字符串最长公共子串 \(Solution\) 对串A建立后缀自动机. A的SAM中包含A的所有子串,且根到每个节点的路径都是A的子串.如果B(的一部分?)匹配到了SAM上的某个节点,那么这便是AB的公共子串.求出这些点的max(len)即可. 用串B在SAM上逐位匹配,如果匹配,就继续沿着匹配边走: 否则,为了匹配当…
题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; ; char st…
把两个串接到一起求一个SA,然后找最大的sa[i]和sa[i-1]不是一个串的he[i] #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=500005; int n,m,wa[N],wb[N],wv[N],wsu[N],sa[N],rk[N],he[N],bl[N],ans; char s[N],t[N]; bool cmp(int r[],in…
题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在sa[i]的最大值在第二个串里,最小值在第一个串里,说明答案成立 别再把后缀数组敲错了 #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #define ll l…
后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; typedef long long ll; ,mod=; char s[N]; ],Log[N]; void Sort(int* x,int* y,int m) { ; i<m; ++i)c[i]=; ; i<n; ++i)++c[x[i]]; ; i…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10483 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days…
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大值. 如果枚举 A 和 B 的所有的后缀,那么这样做显然效率低下. 由于要计算 A 的后缀和 B 的后缀 的最长公共前缀, 所以先将第二个字符串写在第一个字符串后面,中间用一个没 有出现过的字符隔开, 再求这个新的字符串的后缀数组.观察一下,看看能不能从…
Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 23696   Accepted: 9705 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days:…
The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 1…
还是模板啊,手残&&打成||查错查了1h+TAT #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int N = 2000003; int t1[N], t2[N], c[N]; void st(int *x, int *y, int *sa, int n, int m) { int i;…
用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; + ; char s[maxn]; int n; int sa[maxn], rank[maxn], height[maxn]; int t[maxn], t2[maxn], c[maxn];…
题意: 给你两串字符,要你找出在这两串字符中都出现过的最长子串 解析: 先用个分隔符将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #i…
[题意]给定两个字符串S和T,求最长公共子串.len<=10^5. [算法]后缀自动机 [题解]对字符串S建SAM,然后令串T在S上跑匹配. 这是自动机最原本的功能——匹配,就是串T在SAM(S)上走,不能匹配就沿失配边走,这样得到的是T上以每个字符结尾的子串中与S的最长公共子串,取Max即是答案. 注意失配不要走到0节点处. #include<cstdio> #include<cstring> #include<algorithm> using namespac…
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍. 这题的思路是这样的: 1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界:不存在就缩小上界): 2)主要是对答案的判断(judge函数…