求两串的最长公共子串。
用分隔符连接2个字符串,并且跨字符串的height才对答案有贡献。
回忆一下当时的程序。。倍增算法 O(nlogn)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;i++)
#define MAXN 200050
int n, num[MAXN], z[MAXN];
int sa[MAXN], rank[MAXN], height[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], wd[MAXN];
char s[MAXN];
void sort(int *sa, int *x, int *bucket, int *y, int n, int m) {
int i;
rep(i,0,m) bucket[i] = 0;
rep(i,0,n) bucket[x[i]]++;
rep(i,1,m) bucket[i] += bucket[i-1];
for(i = n-1; i >= 0; i --) sa[--bucket[x[i]]] = y[i];
}
void da(char *r, int n, int m) {
int i, j, p, *x = wa, *y = wb, *t;
rep(i,0,n) x[i] = r[i], z[i] = i;
sort(sa, x, wd, z, n, m);
for(j = 1, p = 1; p < n; j *= 2, m = p) {
p = 0;
rep(i,n-j,n) y[p ++] = i;
rep(i,0,n) if(sa[i] >= j) y[p ++] = sa[i] - j;
rep(i,0,n) wv[i] = x[y[i]];
sort(sa, wv, wd, y, n, m);
t=x,x=y,y=t;p=1;x[sa[0]]=0;
rep(i,1,n) x[sa[i]] = y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]? p - 1: p ++;
}
}
void calHeight(char *r, int n) {
int i, j, k = 0;
for(i = 1; i <= n; i ++) rank[sa[i]] = i;
rep(i,0,n) {
if (k) k--;
j = sa[rank[i]-1];
while(r[i+k]==r[j+k]) k++;
height[rank[i]] = k;
}
}
int main() {
scanf("%s", s);
int n1 = strlen(s), ans=0;
s[n1]='$';
scanf("%s",s+n1+1);
int n = strlen(s);
da(s,n+1,200);
calHeight(s,n);
for(int i=2;i<=n;i++) {
if(sa[i-1]<n1&&sa[i]>n1||sa[i-1]>n1&&sa[i]<n1)
ans=max(ans,height[i]);
}
printf("%d", ans);
return 0;
}
后缀自动机很优越啊,不管是时间还是代码复杂度都很低,一下就进Rk100了。。注意后缀自动机的状态数最大是2n-1。。。。
构建SAM,另一个串在SAM上跑,无法匹配就走失败指针。
O(n) 复杂度。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200005;
int ch[N][26], ma[N], fa[N], last, cnt, len, rt;
char s1[N], s2[N];
void extend(char c) {
int np = ++cnt, p = last; last = np; ma[np] = ++len;
while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
if (!p) fa[np] = rt;
else {
int q = ch[p][c];
if (ma[p] + 1 == ma[q]) fa[np] = q;
else {
int nq = ++cnt; ma[nq] = ma[p] + 1;
memcpy(ch[nq], ch[q], sizeof ch[q]);
fa[nq] = fa[q]; fa[q] = fa[np] = nq;
while (ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
}
void construct(char *s) {
int l = strlen(s);
last = cnt = rt = 1; len = 0;
for (int i = 0; i <= 2 * l; ++i) {
memset(ch[i], 0, sizeof ch[i]);
ma[i] = fa[i] = 0;
}
for (; *s; ++s) extend(*s - 'a');
}
int find(char *s) {
int len = 0x3f3f3f3f, p = rt, res = 0;
for (; *s; ++s) {
char c = *s - 'a';
while (p && !ch[p][c]) p = fa[p];
if (!p) p = rt, len = 0;
else len = min(len, ma[p]) + 1, p = ch[p][c];
res = max(res, len);
}
return res;
}
int main() {
while (scanf("%s", s1) != EOF) {
scanf("%s", s2);
construct(s1);
printf("%d\n", find(s2));
}
return 0;
}
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 16 shours on train to his hometown), he decided only to send SMS with his mother.
The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:
You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.
Why ask you to write a program? There are four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :(
Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
A single line with a single integer number – what is the maximum length of the original text written by the little cat.
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
27