【POJ 3294】Life Forms 不小于k个字符串中的最长子串

一下午和一晚上都在刚这道题,各种错误都集齐了so sad 我的时间啊!!!

后缀数组就先做到这里吧,是在伤不起啊QAQ

出现了各种奇怪的错误,看了标算,然后乱改自己的代码,莫名其妙的改A了,后来发现用字符直接给int赋值会WA,必须一个字符先给另一个字符赋值,后者再给int赋值就能A(什么鬼)。后来加了一个(int)s[n]强制转换就简单地A了,评测时强制转换睡觉了吗?还是我rp太差,得多攒点rp

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110005;

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 *t, *x = t1, *y = t2, i, j, p;
	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);
}

bool vis[105];
char s[N];
int a[N], rank[N], sa[N], h[N], n, k, belong[105], sz, ci = 0, ans[N], ansnum;
bool can(int num) {
	memset(vis, 0, sizeof(vis));
	int cnt = 0, cont = 0;
	for(int i = 2; i <= n; ++i)
		if (h[i] >= num) {
			for(int j = 1; j <= k; ++j) {
				if (sa[i] > belong[j - 1] && sa[i] < belong[j])
					cnt += (int)(!vis[j]), vis[j] = 1;
				if (sa[i - 1] > belong[j - 1] && sa[i - 1] < belong[j])
					cnt += (int)(!vis[j]), vis[j] = 1;
			}
		} else {
			if (cnt > k / 2) ans[++cont] = sa[i - 1];
			cnt = 0;
			memset(vis, 0, sizeof(vis));
		}
	if (cnt > k / 2) ans[++cont] = sa[n - 1];
	if (cont != 0) ansnum = cont;
	return cont;
}
int main() {
	belong[0] = - 1;
	while (scanf("%d", &k), k) {
		n = sz = 1;
		ansnum = 0;
		for(int i = 1; i <= k; ++i) {
			scanf("%s", s + n);
			for(; s[n] != '\0'; ++n) a[n] = (int)s[n];
			a[n] = '#' + i;
			belong[sz++] = n;
			++n;
		}
		--n;
		mkhz(a, sa, n + 1, 400);
		mkh(a, sa, rank, h, n);
		int left = 1, right = n, mid;
		while (left <= right) {
			mid = (left + right) >> 1;
			if (can(mid)) left = mid + 1;
			else right = mid - 1;
		}
		if (ci) puts(""); ++ci;
		if (left == 1) puts("?");
		else {
			for(int i = 1; i <= ansnum; ++i) {
				for(int j = ans[i]; j < ans[i] + left - 1; ++j)
					putchar(s[j]);
				puts("");
			}
		}
	}
	return 0;
}

没了==

posted @ 2016-04-22 22:03  abclzr  阅读(250)  评论(0编辑  收藏  举报