QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#268265#5084. Longest SubstringSTnofarjo#WA 45ms14428kbC++203.3kb2023-11-28 14:31:312023-11-28 14:31:34

Judging History

你现在查看的是最新测评结果

  • [2023-11-28 14:31:34]
  • 评测
  • 测评结果:WA
  • 用时:45ms
  • 内存:14428kb
  • [2023-11-28 14:31:31]
  • 提交

answer

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

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for (int i=(a); i<(b); ++i)
typedef vector<int> vi;

struct SuffixArray {
	vi sa, lcp;
	SuffixArray(string &s, int lim = 256) {
		int n = sz(s) + 1, k = 0, a, b;
		vi x(all(s)+1), y(n), ws(max(n, lim)), rank(n);
		sa = lcp = y, iota(all(sa), 0);
		for (int j=0, p=0; p<n; j=max(1,j*2), lim=p) {
			p = j, iota(all(y), n-j);
			rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j;
			fill(all(ws), 0);
			rep(i,0,n) ws[x[i]]++;
			rep(i,1,lim) ws[i] += ws[i-1];
			for (int i=n; i--;) sa[--ws[x[y[i]]]] = y[i];
			swap(x, y), p = 1, x[sa[0]] = 0;
			rep(i,1,n) a = sa[i-1], b = sa[i], x[b] = (y[a] == y[b] && y[a+j] == y[b+j]) ? p-1 : p++;
		}
		rep(i,1,n) rank[sa[i]] = i;
		for (int i=0, j; i<n-1; lcp[rank[i++]] = k)
			for (k && k--, j=sa[rank[i]-1]; s[i+k] == s[j+k]; k++);
	}
};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	string s;
	cin >> s;
	int n = s.size();

	SuffixArray sa(s);

	// for (int i=1; i<=n; i++) cout << sa.sa[i] << ' ';
	// cout << '\n';
	// for (int i=1; i<=n; i++) cout << sa.lcp[i] << ' ';
	// cout << '\n';

	vector<int> par(n+1), sez(n+1), lb(n+1), rb(n+1), ans(n+1);
	vector<set<int>> vals(n+1);
	vector<map<int, int>> diffs(n+1);
	auto create = [&](int u) -> void {
		par[u] = lb[u] = rb[u] = u; sez[u] = 1; vals[u].insert(sa.sa[u]);
	};
	auto find = [&](auto self, int u) -> int {
		if (u != par[u]) return par[u] = self(self, par[u]);
		return u;
	};
	auto unite_set = [&](int u, int v) -> void {
		auto &val = vals[v];
		auto &diff = diffs[v];
		for (int x : vals[u]) {
			auto it = val.lower_bound(x);
			if (it != val.begin()) {
				auto prv = it;
				prv--;
				int y = *it - *prv;
				diff[y]--;
				if (diff[y] == 0) diff.erase(y);
			}

			val.insert(x);
			it = val.lower_bound(x);
			if (it != val.end()) {
				auto nxt = it;
				nxt++;
				if (nxt != val.end()) {
					int y = *nxt - x;
					diff[y]++;
				}
				if (it != val.begin()) {
					auto prv = it;
					prv--;
					int y = x - *prv;
					diff[y]++;
				}
			}
		}

		vals[u].clear();
		diffs[u].clear();
	};
	auto update_ans = [&](int u, int h) -> void {
		int len = rb[u] - lb[u] + 1;
		// cout << lb[u] << ' ' << rb[u] << ' ' << len << ' ' << h+1 << ' ';
		// cout << (diffs[u].empty() ? 1000 : diffs[u].begin()->first) << '\n';
		if (ans[len] == 0 && (diffs[u].empty() || diffs[u].begin()->first <= h+1)) ans[len] = h+1; 
	};
	auto unite = [&](int u, int v, int h) -> void {
		u = find(find, u); v = find(find, v);
		update_ans(u, h); update_ans(v, h);
		if (sez[u] > sez[v]) swap(u, v);
		par[u] = v; sez[v] += sez[u]; lb[v] = min(lb[u], lb[v]); rb[v] = max(rb[u], rb[v]);
		unite_set(u, v);
	};

	vector<int> pos(n);
	for (int i=1; i<=n; i++) pos[sa.sa[i]] = i;

	vector<int> idx(n+1);
	for (int i=0; i<=n; i++) idx[i] = i;
	sort(idx.begin()+1, idx.end(), [&](int i, int j) {
		return sa.lcp[i] > sa.lcp[j];
	});
	for (int h=n, i=1; h>0; h--) {
		create(pos[n - h]);
		while (i <= n && sa.lcp[idx[i]] >= h) {
			int j = idx[i];
			unite(j, j-1, h);
			i++;
		}
	}
	for (int i=1; i<=n; i++) {
		if (i == par[i]) update_ans(i, 0);
	}

	ans[1] = n;
	for (int i=1; i<=n; i++) cout << ans[i] << ' ';
	cout << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3540kb

input:

ababa

output:

5 2 1 0 0 

result:

ok single line: '5 2 1 0 0 '

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

aaaaaaaa

output:

8 7 6 5 4 3 2 1 

result:

ok single line: '8 7 6 5 4 3 2 1 '

Test #3:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

a

output:

1 

result:

ok single line: '1 '

Test #4:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

abcdefghijklmnopqrstuvwxyz

output:

26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

result:

ok single line: '26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #5:

score: 0
Accepted
time: 27ms
memory: 11896kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 49951 ...

result:

ok single line: '50000 49999 49998 49997 49996 ... 13 12 11 10 9 8 7 6 5 4 3 2 1 '

Test #6:

score: -100
Wrong Answer
time: 45ms
memory: 14428kb

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:

50000 49997 49995 49993 49991 49989 49987 49985 49983 49981 49979 49977 49975 49973 49971 49969 49967 49965 49963 49961 49959 49957 49955 49953 49951 49949 49947 49945 49943 49941 49939 49937 49935 49933 49931 49929 49927 49925 49923 49921 49919 49917 49915 49913 49911 49909 49907 49905 49903 49901 ...

result:

wrong answer 1st lines differ - expected: '50000 49998 49996 49994 49992 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '50000 49997 49995 49993 49991 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '