Submission details
Task:Period
Sender:omantere
Submission time:2016-10-22 13:48:23 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#10.22 sdetails
#20.17 sdetails
#30.17 sdetails
#40.18 sdetails
#50.18 sdetails
#60.21 sdetails

Code

#include <bits/stdc++.h>

#define _ ios_base::sync_with_stdio(0);cin.tie();
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define vpii vector<pii>
#define vvi vector< vector<int> >
#define si set<int>
#define mi map<string, int>

using namespace std;

int main() { _
    string s;
    string f;
    cin >> f;
    s.reserve(2*s.size());
    s = f + f;
    int L = 0;
    int R = 0;
    int n = s.size();
    int z[n];
    if(n == 1) {
        cout << s[0] << endl;
        return 0;
    }
    for(int i = 0; i < n; i++) z[i] = 0;
    int p = 0; 
    for (int i = 1; i < n; i++) {
        if (i > R) {
            L = R = i;
            while (R < n && s[R-L] == s[R]) R++;
            z[i] = R-L; R--;

        } else {
            int k = i-L;
            if (z[k] < R-i+1) z[i] = z[k];
            else {
                L = i;
                while (R < n && s[R-L] == s[R]) R++;
                z[i] = R-L; R--;
            }
        }
        if(z[i] >= p) {
            p = z[i];
        }
        else if(z[i] != 0) {
            p -= z[i];
            break;
        }
    }    
    for(int i = 0; i < n; i++) cout << z[i] << " ";
    cout << endl;
    cout << s.substr(0, p) << endl;

    return 0;
}

Test details

Test 1

Verdict:

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
a

user output
0 1999999 1999998 0 0 0 0 0 0 ...

Test 2

Verdict:

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
0 999998 999997 0 0 0 0 0 0 0 ...

Test 3

Verdict:

input
nabvmrnenabvmrnenabvmrnenabvmr...

correct output
nabvmrne

user output
0 0 0 0 0 0 1 0 1999992 0 0 0 ...

Test 4

Verdict:

input
fwqrbqnmobvwslpyfrlkrfwluaxyzk...

correct output
fwqrbqnmobvwslpyfrlkrfwluaxyzk...

user output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

Test 5

Verdict:

input
ohicwwkhdoesqvsyemhdhubpvmqkre...

correct output
ohicwwkhdoesqvsyemhdhubpvmqkre...

user output
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ...

Test 6

Verdict:

input
gqzzocfzbuvfovbvamyflvcuuajzgu...

correct output
gqzzocfzbuvfovbvamyflvcuuajzgu...

user output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...