SP1811 LCS - Longest Common Substring

\(\color{#0066ff}{ 题目描述 }\)

输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串。如果没有公共子串则输出0 。

\(\color{#0066ff}{输入格式}\)

两个字符串

\(\color{#0066ff}{输出格式}\)

一个整数,为 所求答案

\(\color{#0066ff}{输入样例}\)

alsdfkjfjkdsal
fdjskalajfkdsla

\(\color{#0066ff}{输出样例}\)

3

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{ 题解 }\)

对第一个串建立后缀自动机

用第二个串在自动机上跑匹配

匹配一下就取max

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
	char ch; int x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
const int maxn = 6e5 + 5;
struct SAM {
protected:
	struct node {
		node *ch[26], *fa;
		int len, siz;
		node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
			memset(ch, 0, sizeof ch);
		}
	};
	node *root, *tail, *lst;
	node pool[maxn];
	void extend(int c) {
		node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
		for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
		if(!v) o->fa = root;
		else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
		else {
			node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
			std::copy(d->ch, d->ch + 26, n->ch);
			n->fa = d->fa, d->fa = o->fa = n;
			for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
		}
		lst = o;
	}
	void clr() {
		tail = pool;
		root = lst = new(tail++) node();
	}
public:
	SAM() { clr(); }
	void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
	int match(char *s) {
		int ans = 0;
		node *o = root;
		int len = 0;
		for(char *p = s; *p; p++) {
			int pos = *p - 'a';
			if(o->ch[pos]) o = o->ch[pos], len++;
			else {
				while(o && !o->ch[pos]) o = o->fa;
				if(!o) o = root, len = 0;
				else len = o->len + 1, o = o->ch[pos];
			}
			ans = std::max(ans, len);
		}
		return ans;
	}
}sam;
char s[maxn];
int main() {
	scanf("%s", s);
	sam.ins(s);
	scanf("%s", s);
	printf("%d\n", sam.match(s));
	return 0;
}
posted @ 2019-01-10 18:50  olinr  阅读(150)  评论(0编辑  收藏  举报