Task: | Z-array |
Sender: | bubu2006 |
Submission time: | 2024-11-02 11:04:28 +0200 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.09 s | details |
#7 | ACCEPTED | 0.09 s | details |
#8 | ACCEPTED | 0.09 s | details |
#9 | ACCEPTED | 0.09 s | details |
#10 | ACCEPTED | 0.09 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.09 s | details |
#13 | ACCEPTED | 0.09 s | details |
Code
#pragma GCC optimize("O3","unroll-loops") #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt") #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif /** * Author: chilli * License: CC0 * Description: z[i] computes the length of the longest common prefix of s[i:] and s, * except z[0] = 0. (abacaba -> 0010301) * Time: O(n) * Status: stress-tested */ vi Z(const string& S) { vi z(sz(S)); int l = -1, r = -1; rep(i,1,sz(S)) { z[i] = i >= r ? 0 : min(r - i, z[i - l]); while (i + z[i] < sz(S) && S[i + z[i]] == S[z[i]]) z[i]++; if (i + z[i] > r) l = i, r = i + z[i]; } return z; } signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); // RTE if input wrong datatype string s; cin >> s; vi z = Z(s); z[0] = sz(s); for (int i = 0; i < sz(s); i++) cout << z[i] << ' '; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
aaaaaaaaaa |
correct output |
---|
10 9 8 7 6 5 4 3 2 1 |
user output |
---|
10 9 8 7 6 5 4 3 2 1 |
Test 2
Verdict: ACCEPTED
input |
---|
ababababab |
correct output |
---|
10 0 8 0 6 0 4 0 2 0 |
user output |
---|
10 0 8 0 6 0 4 0 2 0 |
Test 3
Verdict: ACCEPTED
input |
---|
aabababaaa |
correct output |
---|
10 1 0 1 0 1 0 2 2 1 |
user output |
---|
10 1 0 1 0 1 0 2 2 1 |
Test 4
Verdict: ACCEPTED
input |
---|
ahtqqkhrrn |
correct output |
---|
10 0 0 0 0 0 0 0 0 0 |
user output |
---|
10 0 0 0 0 0 0 0 0 0 |
Test 5
Verdict: ACCEPTED
input |
---|
mqdozmqdoz |
correct output |
---|
10 0 0 0 0 5 0 0 0 0 |
user output |
---|
10 0 0 0 0 5 0 0 0 0 |
Test 6
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 999999 999998 999997 9... |
user output |
---|
1000000 999999 999998 999997 9... |
Test 7
Verdict: ACCEPTED
input |
---|
ababababababababababababababab... |
correct output |
---|
1000000 0 999998 0 999996 0 99... |
user output |
---|
1000000 0 999998 0 999996 0 99... |
Test 8
Verdict: ACCEPTED
input |
---|
baaaabbabaabbbbaaaabbbababaaba... |
correct output |
---|
1000000 0 0 0 0 1 2 0 3 0 0 1 ... |
user output |
---|
1000000 0 0 0 0 1 2 0 3 0 0 1 ... |
Test 9
Verdict: ACCEPTED
input |
---|
ltpbdybvcpychbsplyonjfmdtsnqwt... |
correct output |
---|
1000000 0 0 0 0 0 0 0 0 0 0 0 ... |
user output |
---|
1000000 0 0 0 0 0 0 0 0 0 0 0 ... |
Test 10
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 99998 99997 99996 9999... |
user output |
---|
1000000 99998 99997 99996 9999... |
Test 11
Verdict: ACCEPTED
input |
---|
abaababaab |
correct output |
---|
10 0 1 3 0 5 0 1 2 0 |
user output |
---|
10 0 1 3 0 5 0 1 2 0 |
Test 12
Verdict: ACCEPTED
input |
---|
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz... |
correct output |
---|
1000000 499999 499998 499997 4... |
user output |
---|
1000000 499999 499998 499997 4... |
Test 13
Verdict: ACCEPTED
input |
---|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... |
correct output |
---|
1000000 147418 147417 147416 1... |
user output |
---|
1000000 147418 147417 147416 1... |