Submission details
Task:Period
Sender:hugues
Submission time:2016-10-24 00:50:14 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#20.06 sdetails
#30.06 sdetails
#40.06 sdetails
#50.06 sdetails
#60.05 sdetails

Compiler report

input/code.cpp: In function 'void computeZarray(std::string, int*)':
input/code.cpp:10:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < n; ++i) {
                         ^
input/code.cpp:13:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while (R < n && str[R - L] == str[R]) R++;
                        ^
input/code.cpp:21:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 while (R < n && str[R - L] == str[R]) R++;
                            ^

Code

#include <bits/stdc++.h>

using namespace std;


void computeZarray(string str, int *Z) {
    size_t n = str.length();
    int L, R, k;
    L = R = 0;
    for (int i = 1; i < n; ++i) {
        if (i > R) {
            L = R = i;
            while (R < n && str[R - L] == str[R]) R++;
            Z[i] = R - L;
            R--;
        } else {
            k = i - L;
            if (Z[k] < R - i + 1) Z[i] = Z[k];
            else {
                L = i;
                while (R < n && str[R - L] == str[R]) R++;
                Z[i] = R - L;
                R--;
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);

    string s;
    cin >> s;

    if (s.length() == 1) { cout << s << endl; }
    else {
        int Z[s.length()];
        computeZarray(s, Z);

        unsigned long begin = 0, nb_char = 0;
        for (unsigned long i = 1; i < s.length(); i++) {
            if (Z[i] > 0) {
                begin = i;
                break;
            };
        }

        for (unsigned long i = begin + 1; i < s.length(); i++) {
            nb_char++;
            if (Z[i] > 0) {
                break;
            };
        }
        cout << s.substr(begin, nb_char) << endl;
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
a

user output
a

Test 2

Verdict:

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
a

Test 3

Verdict:

input
nabvmrnenabvmrnenabvmrnenabvmr...

correct output
nabvmrne

user output
ne

Test 4

Verdict:

input
fwqrbqnmobvwslpyfrlkrfwluaxyzk...

correct output
fwqrbqnmobvwslpyfrlkrfwluaxyzk...

user output
frlkr

Test 5

Verdict:

input
ohicwwkhdoesqvsyemhdhubpvmqkre...

correct output
ohicwwkhdoesqvsyemhdhubpvmqkre...

user output
oesqvsyemhdhubpvmqkrecuexeskqg...

Test 6

Verdict:

input
gqzzocfzbuvfovbvamyflvcuuajzgu...

correct output
gqzzocfzbuvfovbvamyflvcuuajzgu...

user output
guesdybbmxkpupjmxbpoootqysdaio...