后缀数组板子

#include <bits/stdc++.h>
using namespace std;


inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();


//后缀数组
const int maxn = 1e6 + 7;
int sa[maxn], rk[maxn << 1], oldrk[maxn << 1], cnt[maxn], id[maxn], key1[maxn],c[maxn],x[maxn],y[maxn];
int n, m;
int height[maxn];
char s[maxn];

//sa数组 即后缀数组
class SuffixArray {
public:
	static void getnlogn2() {
		n = strlen(s + 1);
		for (int i = 1; i <= n; i++)sa[i] = i, rk[i] = s[i];
		for (int w = 1; w < n; w <<= 1) {
			std::sort(sa + 1, sa + n + 1, [&](int x, int y) {
				return rk[x] == rk[y] ? rk[x + w] < rk[y + w] : rk[x] < rk[y];
				});
			memmove(oldrk + 1, rk + 1, sizeof(int) * n);
			for (int i = 1, p = 0; i <= n; i++) {
				if (oldrk[sa[i]] == oldrk[sa[i - 1]] && oldrk[sa[i] + w] == oldrk[sa[i - 1] + w]) {
					rk[sa[i]] = p;
				}
				else {
					rk[sa[i]] = ++p;
				}
			}
		}
	}
public:
	static void _getnlogn() {
		int w, p, i;
		m = 127;
		n = strlen(s + 1);
		for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
		for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
		for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i;
		memcpy(oldrk + 1, rk + 1, n * sizeof(int));
		for (p = 0, i = 1; i <= n; ++i) {
			if (oldrk[sa[i]] == oldrk[sa[i - 1]]) {
				rk[sa[i]] = p;
			}
			else {
				rk[sa[i]] = ++p;
			}
		}

		for (w = 1; w < n; w <<= 1, m = n) {
			// 对第二关键字:id[i] + w进行计数排序
			memset(cnt, 0, sizeof(cnt));
			memcpy(id + 1, sa + 1,
				n * sizeof(int));  // id保存一份儿sa的拷贝,实质上就相当于oldsa
			for (i = 1; i <= n; ++i) ++cnt[rk[id[i] + w]];
			for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
			for (i = n; i >= 1; --i) sa[cnt[rk[id[i] + w]]--] = id[i];

			// 对第一关键字:id[i]进行计数排序
			memset(cnt, 0, sizeof(cnt));
			memcpy(id + 1, sa + 1, n * sizeof(int));
			for (i = 1; i <= n; ++i) ++cnt[rk[id[i]]];
			for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
			for (i = n; i >= 1; --i) sa[cnt[rk[id[i]]]--] = id[i];

			memcpy(oldrk + 1, rk + 1, n * sizeof(int));
			for (p = 0, i = 1; i <= n; ++i) {
				if (oldrk[sa[i]] == oldrk[sa[i - 1]] &&
					oldrk[sa[i] + w] == oldrk[sa[i - 1] + w]) {
					rk[sa[i]] = p;
				}
				else {
					rk[sa[i]] = ++p;
				}
			}
		}
	}

	static bool cmp(int x, int y, int w) {
		return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w];
	}
	static void getnlogn() {
		int w, p, i;
		n = strlen(s + 1);
		m = 127;
		for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
		for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
		for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i;

		for (w = 1;; w <<= 1, m = p) {  // m=p 就是优化计数排序值域
			for (p = 0, i = n; i > n - w; --i) id[++p] = i;
			for (i = 1; i <= n; ++i)
				if (sa[i] > w) id[++p] = sa[i] - w;

			memset(cnt, 0, sizeof(cnt));
			for (i = 1; i <= n; ++i) ++cnt[key1[i] = rk[id[i]]];
			// 注意这里px[i] != i,因为rk没有更新,是上一轮的排名数组

			for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
			for (i = n; i >= 1; --i) sa[cnt[key1[i]]--] = id[i];
			memcpy(oldrk + 1, rk + 1, n * sizeof(int));
			for (p = 0, i = 1; i <= n; ++i)
				rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p;
			if (p == n) {
				break;
			}
		}
	}
public:
	static void get_SA()
	{
		n = strlen(s + 1);
		m = 127;
		for (int i = 1; i <= n; ++i) ++c[x[i] = s[i]];
		//c数组是桶 
		//x[i]是第i个元素的第一关键字 
		for (int i = 2; i <= m; ++i) c[i] += c[i - 1];
		//做c的前缀和,我们就可以得出每个关键字最多是在第几名 
		for (int i = n; i >= 1; --i) sa[c[x[i]]--] = i;
		for (int k = 1; k <= n; k <<= 1)
		{
			int num = 0;
			for (int i = n - k + 1; i <= n; ++i) y[++num] = i;
			//y[i]表示第二关键字排名为i的数,第一关键字的位置 
			//第n-k+1到第n位是没有第二关键字的 所以排名在最前面 
			for (int i = 1; i <= n; ++i) if (sa[i] > k) y[++num] = sa[i] - k;
			//排名为i的数 在数组中是否在第k位以后
			//如果满足(sa[i]>k) 那么它可以作为别人的第二关键字,就把它的第一关键字的位置添加进y就行了
			//所以i枚举的是第二关键字的排名,第二关键字靠前的先入队 
			for (int i = 1; i <= m; ++i) c[i] = 0;
			//初始化c桶 
			for (int i = 1; i <= n; ++i) ++c[x[i]];
			//因为上一次循环已经算出了这次的第一关键字 所以直接加就行了 
			for (int i = 2; i <= m; ++i) c[i] += c[i - 1];//第一关键字排名为1~i的数有多少个 
			for (int i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
			//因为y的顺序是按照第二关键字的顺序来排的 
			//第二关键字靠后的,在同一个第一关键字桶中排名越靠后 
			//基数排序 
			swap(x, y);
			//这里不用想太多,因为要生成新的x时要用到旧的,就把旧的复制下来,没别的意思 
			x[sa[1]] = 1; num = 1;
			for (int i = 2; i <= n; ++i)
				x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
			//因为sa[i]已经排好序了,所以可以按排名枚举,生成下一次的第一关键字 
			if (num == n) break;
			m = num;
			//这里就不用那个122了,因为都有新的编号了 
		}
	}
public:
	//height[i] = lcp(s[i],s[i - 1]);
	//height[rk[i]] >= height[rk[i - 1]] - 1
	static void getHeight() {
		int i, k;
		for (int i = 1; i <= n; i++)rk[sa[i]] = i;
		for (i = 1, k = 0; i <= n; ++i) {
			if (rk[i] == 0) continue;
			if (k) --k;
			while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k;
			height[rk[i]] = k;
		}
	}
};
#include <cstdio>
#include <cstring>
#include <algorithm>
#define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb))
#define G(x) ((x) < tb ? (x) * 3 + 1 : ((x) - tb) * 3 + 2)
using namespace std;
const int N = 3 * (1e5 + 5);
int wa[N], wb[N], ws[N], wv[N], sa[N];
int rak[N], height[N], cal[N], n;
char s[N], ans[N], s1[N];
int cnt[N];
int c0(int* r, int a, int b) {
	return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];
}
int c12(int k, int* r, int a, int b) {
	if (k == 2)
		return r[a] < r[b] || r[a] == r[b] && c12(1, r, a + 1, b + 1);
	return r[a] < r[b] || r[a] == r[b] && wv[a + 1] < wv[b + 1];
}
void Rsort(int* r, int* a, int* b, int n, int m) {
	for (int i = 0; i < n; i++) wv[i] = r[a[i]];
	for (int i = 0; i < m; i++) ws[i] = 0;
	for (int i = 0; i < n; i++) ws[wv[i]]++;
	for (int i = 1; i < m; i++) ws[i] += ws[i - 1];
	for (int i = n - 1; i >= 0; i--) b[--ws[wv[i]]] = a[i];
}
void dc3(int* r, int* sa, int n, int m) {
	int i, j, * rn = r + n, * san = sa + n, ta = 0, tb = (n + 1) / 3, tbc = 0, p;
	r[n] = r[n + 1] = 0;
	for (i = 0; i < n; i++) if (i % 3 != 0) wa[tbc++] = i;
	Rsort(r + 2, wa, wb, tbc, m);
	Rsort(r + 1, wb, wa, tbc, m);
	Rsort(r, wa, wb, tbc, m);
	for (p = 1, rn[F(wb[0])] = 0, i = 1; i < tbc; i++)
		rn[F(wb[i])] = c0(r, wb[i - 1], wb[i]) ? p - 1 : p++;
	if (p < tbc) dc3(rn, san, tbc, p);
	else for (i = 0; i < tbc; i++) san[rn[i]] = i;
	for (i = 0; i < tbc; i++) if (san[i] < tb) wb[ta++] = san[i] * 3;
	if (n % 3 == 1) wb[ta++] = n - 1;
	Rsort(r, wb, wa, ta, m);
	for (i = 0; i < tbc; i++) wv[wb[i] = G(san[i])] = i;
	for (i = 0, j = 0, p = 0; i < ta && j < tbc; p++)
		sa[p] = c12(wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
	for (; i < ta; p++) sa[p] = wa[i++];
	for (; j < tbc; p++) sa[p] = wb[j++];
	//for(int i=n;i;i--) rak[i]=rak[i-1];
	//for(int i=n;i;i--) sa[i]++;
}
void calheight(int* r, int* sa, int n) {
	int i, j, k = 0;
	for (i = 1; i <= n; i++) rak[sa[i]] = i;
	for (i = 0; i < n; height[rak[i++]] = k)
		for (k ? k-- : 0, j = sa[rak[i] - 1]; r[i + k] == r[j + k]; k++);
	for (int i = n; i; i--) rak[i] = rak[i - 1];
	for (int i = n; i; i--) sa[i]++;
}
int main() {
	while (scanf("%s", s + 1) && s[1] != '#') {
		n = strlen(s + 1);
		for (int i = 1; i <= n; i++)
			cal[i] = s[i] - 'a' + 1;
		cal[n + 1] = 0;
		dc3(cal + 1, sa, n + 1, 30);
		calheight(cal + 1, sa, n);
		for (int i = 1; i <= n; i++) {
			printf("%d ", height[i]);
		}puts("");
	}
	return 0;
}
class SuffixArray {
public:
	SuffixArray(int n) {
		init(n);
	}
	void init(int n) {
		x.assign(n,0);y.assign(n, 0);c.assign(n, 0);
		sa.assign(n, 0); rank.assign(n, 0); height.assign(n, 0);
	}

	void RSort(int k) {
		fill(c.begin(), c.end(), 0);
		for (int i = 1; i <= n; i++)y[i] = sa[i];
		for (int i = 1; i <= n; i++)c[x[y[i] + k]]++;
		for (int i = 1; i <= m; i++)c[i] += c[i - 1];
		for (int i = n; i; i--) sa[c[x[y[i] + k]]--] = y[i];
	}

	void getSa(const char* s,int _n) {
		m = 127; n = _n;
		int i, k;
		for (i = 1; i <= n; i++) c[x[i] = s[i]]++;	//统计后缀i的第一个字符
		for (i = 1; i <= m; i++)c[i] += c[i - 1];	//累计个数
		for (i = n; i ; i--) sa[ c[x[i]]--] = i;//排名为i的后缀编号

		for (k = 1; k <= n; k <<= 1) {
			RSort(k);
			RSort(0);
			for (i = 1; i <= n; i++)y[i] = x[i];
			for (m = 0, i = 1; i <= n; i++) {
				if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) x[sa[i]] = m;
				else x[sa[i]] = ++m;
			}
			if (m == n)break;
		}
	}
	void getHeight(const char* s) {
		int i, k;
		for (int i = 1; i <= n; i++)rank[sa[i]] = i;
		for (i = 1, k = 0; i <= n; ++i) {
			if (rank[i] == 0) continue;
			if (k) --k;
			while (s[i + k] == s[sa[rank[i] - 1] + k]) ++k;
			height[rank[i]] = k;
		}
	}

	void out() {
		for (int i = 1; i <= n; i++) {
			printf("%d ", sa[i]);
		}
	}
public:
	int m;
	int n;
	vector<int> x, y, c;
	vector<int> sa;		//排名为i的后缀编号
	vector<int> rank;	//后缀i的排名
	vector<int> height; //LCP(sa[i],sa[i-1])
};

const int maxn = 1e6 + 7;
SuffixArray sa(maxn << 1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值