【BZOJ 1031】【JSOI 2007】字符加密Cipher

后缀数组模板题,看了一天的后缀数组啊,我怎么这么弱TwT

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 200003;
char s[N];
int t1[N], t2[N], sa[N], c[N];
void st(int *x, int *y, 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(char *a, int n, int m) {
	int i, j, p, *x = t1, *y = t2, *t;
	for(i = 0; i < n; ++i)
		x[i] = s[i], y[i] = i;
	st(x, y, n, m);
	for(j = 1, p = 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, 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++;
	}
}

int main() {
	scanf("%s", s + 1);
	int len = strlen(s + 1);
	for(int i = 1; i <= len; ++i) s[len + i] = s[i];
	s[0] = 0;
	mkhz(s, len * 2 + 1, 256);
	for(int i = 1, cnt = 1; cnt <= len; ++i)
		if (sa[i] <= len)
			printf("%c", s[sa[i] + len - 1]), ++cnt;
	puts("");
	return 0;
}

后缀数组好厉害~~~

NOI 2017 Bless All
posted @ 2016-04-20 17:36  abclzr  阅读(134)  评论(0编辑  收藏  举报