萌新刚学 SA 后缀数组,求调 45pts t=1 不过喵
查看原帖
萌新刚学 SA 后缀数组,求调 45pts t=1 不过喵
852144
Loser_Syx楼主2024/7/20 09:08
#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <random>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define int long long
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
	template <typename T = int>
	inline T read() {
		T s = 0, w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		return s * w;
	}
	template <typename T>
	inline void read(T &s) {
		s = 0;
		int w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		s = s * w;
	}
	template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
		read(x), read(arp...);
	}
	template <typename T>
	inline void write(T x, char ch = '\n') {
		if (x < 0) x = -x, putchar('-');
		static char stk[25];
		int top = 0;
		do {
			stk[top++] = x % 10 + '0', x /= 10;
		} while (x);
		while (top) putchar(stk[--top]);
		putchar(ch);
		return;
	}
	template <typename T>
	inline void smax(T &x, T y) {
		if (x < y) x = y;
	}
	template <typename T>
	inline void smin(T &x, T y) {
		if (x > y) x = y;
	}
	void quit() {
		exit(0);
	}
} using namespace FastIO;
const int N = 1e6 + 19;
int sa[N], rnk[N], h[N], n, k, t, cnt[N], tmp[N]; long long sum[N];
char s[N];
void get_sa() {
	for (int i = 0; i < n; ++i) sa[i] = i;
	sort(sa, sa + n, [&](int x, int y) {
		return s[x] < s[y];
	} );
	for (int i = 0; i < n; ++i) {
		if (i && s[sa[i]] == s[sa[i - 1]]) rnk[sa[i]] = rnk[sa[i - 1]];
		else rnk[sa[i]] = i;
	}
	for (int w = 1; w < n; w <<= 1) {
		for (int i = 0; i < w; ++i) tmp[i] = i + n - w;
		for (int i = 0, j = w; j < n; ++i) if (sa[i] >= w) tmp[j++] = sa[i] - w;
		for (int i = 0; i < n; ++i) cnt[i] = 0;
		for (int i = 0; i < n; ++i) ++cnt[rnk[i]];
		for (int i = 1; i < n; ++i) cnt[i] += cnt[i - 1];
		for (int i = n - 1; ~i; --i) sa[--cnt[rnk[tmp[i]]]] = tmp[i];
		bool ok = 1;
		for (int i = 0; i < n; ++i) {
			if (i && rnk[sa[i - 1]] == rnk[sa[i]] && (sa[i - 1] + w < n ? rnk[sa[i - 1] + w] : -1) == (sa[i] + w < n ? rnk[sa[i] + w] : -1)) {
				ok = 0;
				tmp[sa[i]] = tmp[sa[i - 1]];
			} else tmp[sa[i]] = i;
		}
		for (int i = 0; i < n; ++i) rnk[i] = tmp[i];
		if (ok) break;
	}
	for (int i = 0, k = 0; i < n; ++i) {
		if (!rnk[i]) continue;
		if (k) --k;
		while (s[i + k] == s[sa[rnk[i] - 1] + k]) ++k;
		h[rnk[i]] = k;
	}
}
void solve0() {
	for (int i = 0; i < n; ++i) {
		int c = n - sa[i] - h[i];
		if (k <= c) {
			for (int j = sa[i]; j <= sa[i] + h[i] + k - 1; ++j) putchar(s[j]);
			return ;
		} else k -= c;
	}
	puts("-1");
}
void solve1() {
	sum[0] = n - sa[0];
	for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + n - sa[i];
	if (sum[n - 1] < k) return puts("-1"), void();
	int pl = 0, pr = n - 1;
	for (int i = 0; i < n; ++i) {
		int qwq = pl;
		for (int j = 'a'; j <= 'z'; ++j) {
			int l = qwq, r = pr + 1;
			while (l < r) {
				int mid = (l + r) >> 1;
				if (s[sa[mid] + i] > j) r = mid;
				else l = mid + 1;
			}
			--r;
			long long Syxqwq = sum[r] - (qwq ? sum[qwq - 1] : 0) - 1LL * (r - qwq + 1) * i;
			if (k <= r - qwq + 1) {
				for (int j = sa[qwq]; j <= sa[qwq] + i; ++j) putchar(s[j]);
				return ;
			}
			if (Syxqwq >= k) {
				pl = qwq, pr = r;
				k -= Syxqwq - qwq + 1;
				break;
			}
			qwq = r + 1;
			k -= Syxqwq;
		} if (n - sa[pl] == i+1) ++pl;
	}
}
signed main() {
	scanf("%s", s);
	read(t, k);
	n = strlen(s); get_sa();
	if (t == 0) solve0();
	else solve1();
	return 0;
}
2024/7/20 09:08
加载中...