CSES - Aalto Competitive Programming 2024 - wk9 - Homework - Results
Submission details
Task:Z-array
Sender:ilyas.ben
Submission time:2024-11-04 09:56:16 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.15 sdetails
#7ACCEPTED0.14 sdetails
#8ACCEPTED0.14 sdetails
#9ACCEPTED0.14 sdetails
#10ACCEPTED0.14 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.15 sdetails
#13ACCEPTED0.14 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:44:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for (long long i = 0; i < Z.size(); i++) {
      |                           ~~^~~~~~~~~~
input/code.cpp:46:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         if (i < Z.size() - 1) {
      |             ~~^~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<long long> calculateZ(const string &s) {
    long long n = s.length();
    vector<long long> Z(n);
    long long l = 0, r = 0; 

    for (long long i = 1; i < n; i++) {
        if (i > r) {
            l = r = i;
            while (r < n && s[r] == s[r - l]) {
                r++;
            }
            Z[i] = r - l;
            r--;
        } else {
            long long k = i - l; 
            if (Z[k] < r - i + 1) {
                Z[i] = Z[k];
            } else {
                l = i;
                while (r < n && s[r] == s[r - l]) {
                    r++;
                }
                Z[i] = r - l;
                r--;
            }
        }
    }
    Z[0] = n;
    return Z;
}

int main() {
    string s;
    cin >> s;

    vector<long long> Z = calculateZ(s);

    for (long long i = 0; i < Z.size(); i++) {
        cout << Z[i];
        if (i < Z.size() - 1) {
            cout << " "; 
        }
    }
    cout << endl;

    return 0;
}

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...
Truncated

Test 7

Verdict: ACCEPTED

input
ababababababababababababababab...

correct output
1000000 0 999998 0 999996 0 99...

user output
1000000 0 999998 0 999996 0 99...
Truncated

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 ...
Truncated

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 ...
Truncated

Test 10

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1000000 99998 99997 99996 9999...

user output
1000000 99998 99997 99996 9999...
Truncated

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...
Truncated

Test 13

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1000000 147418 147417 147416 1...

user output
1000000 147418 147417 147416 1...
Truncated