码迷,mamicode.com
首页 > 其他好文 > 详细

【POJ 2774】Long Long Message 最长公共子串

时间:2016-04-21 20:23:51      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

还是模板啊,手残&&打成||查错查了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;
	for(i = 0; i < m; ++i) c[i] = 0;
	for(i = 0; i < n; ++i) ++c[x[y[i]]];
	for(i = 1; i < m; ++i) c[i] += c[i - 1];
	for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
}
void mkhz(int *a, int *sa, int n, int m) {
	int i, j, p, *t, *x = t1, *y = t2;
	for(i = 0; i < n; ++i) x[i] = a[i], y[i] = i;
	st(x, y, sa, n, m);
	for(p = 1, j = 1; p < n; j <<= 1, m = p) {
		for(p = 0, i = n - j; i < n; ++i) y[p++] = i;
		for(i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
		st(x, y, sa, n, m);
		for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)
			x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j] ? p - 1 : p++;
	}
}
void mkh(int *r, int *sa, int *rank, int *h, int n) {
	int i, j, k = 0;
	for(i = 1; i <= n; ++i) rank[sa[i]] = i;
	for(i = 1; i <= n; h[rank[i++]] = k)
		for(k ? --k : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
}

char s[N];
int a[N], sa[N], h[N], rank[N], n;
int main() {
	while(~scanf("%s", s + 1)) {
		int tmp = strlen(s + 1);
		s[tmp + 1] = ‘$‘;
		scanf("%s", s + tmp + 2);
		int n = strlen(s + 1);
		for(int i = 1; i <= n; ++i) a[i] = s[i];
		mkhz(a, sa, n + 1, 130);
		mkh(a, sa, rank, h, n);
		int ans = 0;
		++tmp;
		for(int i = 2; i <= n; ++i)
			if (((tmp < sa[i - 1] && tmp > sa[i]) || (tmp < sa[i] && tmp > sa[i - 1])) && h[i] > ans)
				ans = h[i];
		printf("%d\n", ans);
	}
	return 0;
}

Orz

【POJ 2774】Long Long Message 最长公共子串

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5418374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!