QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#744214#5084. Longest Substringucup-team5062#TL 0ms3628kbC++202.7kb2024-11-13 21:12:132024-11-13 21:12:17

Judging History

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

  • [2024-11-13 21:12:17]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3628kb
  • [2024-11-13 21:12:13]
  • 提交

answer

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;

string to_string(const vector<int>& arr) {
	string res = "[";
	for (int i = 0; i < int(arr.size()); i++) {
		if (i != 0) {
			res += ", ";
		}
		res += to_string(arr[i]);
	}
	res += "]";
	return res;
}

vector<int> solve(int N, const string& S) {
	vector<int> A(N + 1);
	for (int i = 0; i < N; i++) {
		A[i] = 1 << (S[i] - 'a');
	}
	A[N] = 1 << 26;
	int Z = 1;
	vector<int> b(N + 1);
	vector<unsigned short> g(N + 1, 0), created(N + 1), cnt(N + 1), best(N + 1);
	vector<int> ans(N + 1);
	ans[1] = N;
	for (int i = 0; i < N; i++) {
		// cerr << i << ": " << to_string(g) << endl;
		for (int j = 0; j < Z; j++) {
			b[j] = 0;
		}
		for (int j = 0; j <= N - i; j++) {
			b[g[j]] |= A[j + i];
		}
		// cerr << i << ": " << to_string(g) << " " << to_string(cnt) << endl;
		for (int j = 0; j < Z; j++) {
			if (b[j] & (b[j] - 1)) {
				if (created[j] > 0) {
					int base = 0, basecur = 0;
					while (basecur <= N - i) {
						if (g[basecur] == j) {
							basecur += created[j];
							base++;
						} else {
							basecur++;
						}
					}
					int l = created[j], r = i + 1;
					while (r - l > 1) {
						int m = (l + r) / 2;
						int score = 0, cur = 0;
						while (cur <= N - i) {
							if (g[cur] == j) {
								cur += m;
								score++;
							} else {
								cur++;
							}
						}
						if (score == base) {
							l = m;
						} else {
							r = m;
						}
					}
					if (best[cnt[j]] <= base) {
						best[cnt[j]] = base;
						ans[cnt[j]] = l;
					}
				}
				cnt[j] = 0;
				for (int k = 0; k < N - i; k++) {
					if (g[k] == j) {
						int c = __builtin_popcount(b[j] & (A[k + i] - 1));
						int mark = (c ? Z + c - 1 : j);
						g[k] = mark;
						cnt[mark]++;
					}
				}
				int nxtZ = Z + __builtin_popcount(b[j] & ((1 << 26) - 1)) - 1;
				created[j] = i + 1;
				for (int k = Z; k < nxtZ; k++) {
					created[k] = i + 1;
				}
				Z = nxtZ;
			}
		}
		g[N - i] = -1;
	}
	return ans;
}

#include <random>

mt19937_64 mt(1);

void test() {
	int N = 50000;
	string S;
	for (int i = 0; i < N; i++) {
		S += mt() % 1 + 'a';
	}
	vector<int> ans = solve(N, S);
	int sum = 0;
	for (int i = 1; i <= N; i++) {
		sum += ans[i];
	}
	cout << sum << endl;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	string S;
	cin >> S;
	int N = S.size();
	vector<int> ans = solve(N, S);
	for (int i = 1; i <= N; i++) {
		cout << ans[i] << (i != N ? ' ' : '\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: 3492kb

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: 3628kb

input:

a

output:

1

result:

ok single line: '1'

Test #4:

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

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: -100
Time Limit Exceeded

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: