_bzoj1031 [JSOI2007]字符加密Cipher后缀数组

Posted ciao_sora

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了_bzoj1031 [JSOI2007]字符加密Cipher后缀数组相关的知识,希望对你有一定的参考价值。

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1031

保存后缀数组模版。

其实如果数据范围小一点,或者空间限制再大一点,或者甚至只要字母表再小一点就可以使用后缀自动机了,只可惜空间不允许,就用后缀数组!

其实还是不是很理解代码,是否该当成黑盒代码背下来呢?

#include <cstdio>
#include <cstring>
#include <algorithm>

const int maxn = 200005;

int n, mx, sa[maxn], t1[maxn], t2[maxn], c[maxn], *x, *y;
char s[maxn];

int main(void) {
	scanf("%s", s);
	n = strlen(s);
	memcpy(s + n, s, n - 1);
	n = (n << 1) - 1;
	x = t1;
	y = t2;
	mx = 128;
	
	memset(c, 0, sizeof c);
	for (int i = 0; i < n; ++i) {
		++c[x[i] = s[i]];
	}
	for (int i = 1; i < mx; ++i) {
		c[i] += c[i - 1];
	}
	for (int i = n - 1; ~i; --i) {
		sa[--c[x[i]]] = i;
	}
	int p;
	for (int k = 1; k <= n; k <<= 1) {
		p = 0;
		for (int i = n - k; i < n; ++i) {
			y[p++] = i;
		}
		for (int i = 0; i < n; ++i) {
			if (sa[i] >= k) {
				y[p++] = sa[i] - k;
			}
		}
		
		memset(c, 0, sizeof c);
		for (int i = 0; i < n; ++i) {
			++c[x[y[i]]];
		}
		for (int i = 0; i < mx; ++i) {
			c[i] += c[i - 1];
		}
		for (int i = n - 1; ~i; --i) {
			sa[--c[x[y[i]]]] = y[i];
		}
		
		std::swap(x, y);
		p = 1;
		x[sa[0]] = 0;
		for (int i = 1; i < n; ++i) {
			x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]? p - 1: p++;
		}
		if (p >= n) {
			break;
		}
		mx = p;
	}
	
	int tem = (n + 1) >> 1;
	for (int i = 0; i < n; ++i) {
		if (sa[i] < tem) {
			printf("%c", s[sa[i] + tem - 1]);
		}
	}
	return 0;
}

  

以上是关于_bzoj1031 [JSOI2007]字符加密Cipher后缀数组的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 1031: [JSOI2007]字符加密Cipher

BZOJ_1031_[JSOI2007]_字符串加密_(后缀数组)

BZOJ 1031: [JSOI2007]字符加密Cipher

BZOJ1031: [JSOI2007]字符加密Cipher - 后缀数组 -

BZOJ1031: [JSOI2007]字符加密Cipher

[Bzoj]1031: [JSOI2007]字符加密Cipher