Submission details
Task:Repeating substring
Sender:Pohjantahti
Submission time:2018-10-18 16:00:01 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.03 sdetails
#3ACCEPTED0.07 sdetails
#4ACCEPTED0.18 sdetails
#5ACCEPTED0.18 sdetails
#6ACCEPTED0.18 sdetails
#7ACCEPTED0.18 sdetails
#8ACCEPTED0.17 sdetails
#9ACCEPTED0.19 sdetails
#10ACCEPTED0.18 sdetails
#11ACCEPTED0.12 sdetails
#12UNKNOWN--details
#13UNKNOWN--details
#14UNKNOWN--details
#15UNKNOWN--details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:62:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < str.length(); ++i) {
                  ~~^~~~~~~~~~~~~~

Code

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("arch=skylake")

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

using namespace std;
typedef long long ll;

int n;
string str;

ll A = 989734731;
ll B = 1000000007;

ll h[100005];
ll p[100005];

ll ghash(int a, int b) {
	if (a == 0) return h[b];
	ll cres = (h[b]-h[a-1]*p[b-a+1])%B;
	if (cres < 0) cres += B;
	return cres;
}

vector<int> suffix(string s) {
	int N = n+256;
	vector<int> sa(n), ra(n);
	for (int i = 0; i < n; ++i) {
		sa[i] = i;
		ra[i] = (int)s[i];
	}
	for (int k = 0; k < n; k?k*=2:k++) {
		vector<int> nsa(sa), nra(n), cnt(N);
		for (int i = 0; i < n; ++i) nsa[i] = (nsa[i]-k+n)%n;
		for (int i = 0; i < n; ++i) cnt[ra[i]]++;
		for (int i = 1; i < N; ++i) cnt[i] += cnt[i-1];
		for (int i = n-1; i >= 0; --i) sa[--cnt[ra[nsa[i]]]] = nsa[i];
		int r = 0;
		for (int i = 1; i < n; ++i) {
			if (ra[sa[i]] != ra[sa[i-1]]) r++;
			else if (ra[(sa[i]+k)%n] != ra[(sa[i-1]+k)%n]) r++;
			nra[sa[i]] = r;
		}
		ra = nra;
	}
	return sa;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> str;
	n = str.length();

	p[0] = 1;
	h[0] = str[0];

	for (int i = 1; i < str.length(); ++i) {
		p[i] = (A*p[i-1])%B;
		h[i] = (h[i-1]*A + str[i])%B;
	}

	vector<int> sa = suffix(str);
	int mxlen = 0;
	int mxi = -1;
	for (int i = 0; i < n-1; ++i) {
		int a = sa[i];
		int b = sa[i+1];

		int cl = 0;
		for (int jmp = (1<<17); jmp >= 1; jmp /= 2) {
			int ci = cl+jmp-1;
			if (a+ci < n && b+ci < n && ghash(a, a+ci) == ghash(b, b+ci)) cl += jmp;
		}

		if (cl > mxlen) {
			mxlen = cl;
			mxi = sa[i];
		}
		if (cl == n) break;
	}
	if (mxlen == 0) {
		cout << "\n";
		return 0;
	}
	string res = str.substr(mxi, mxlen);
	cout << res << "\n";
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
abcdabcdabcd

correct output
abcdabcd

user output
abcdabcd

Test 2

Verdict: ACCEPTED

input
abcdefghijklmnopqrstuvwxyz

correct output
(empty)

user output
(empty)

Test 3

Verdict: ACCEPTED

input
yypqtdfbwzpfwsmjjagdjpfyqnyspk...

correct output
cqkgus

user output
cqkgus

Test 4

Verdict: ACCEPTED

input
zwnqhornqkcmioyxxtkkwrbkorncjh...

correct output
dywyvkf

user output
dywyvkf

Test 5

Verdict: ACCEPTED

input
fhnnpfcbnpnsigmvmklzvfluwvypyb...

correct output
asjzge

user output
asjzge

Test 6

Verdict: ACCEPTED

input
daqyvtkjopactcbkghijgrpjghmefa...

correct output
sowvwyy

user output
sowvwyy

Test 7

Verdict: ACCEPTED

input
ksdohlhpsupwqhoditrhvbansccnnh...

correct output
devycn

user output
devycn

Test 8

Verdict: ACCEPTED

input
edhikrxqidgjpxnyytsfzylndslhyu...

correct output
brmnvr

user output
brmnvr

Test 9

Verdict: ACCEPTED

input
hutihnlmghdovkmbelctafdqhldiyl...

correct output
bbzhpo

user output
bbzhpo

Test 10

Verdict: ACCEPTED

input
ttyameoijyqaxrkvfqkxgrwigmalxw...

correct output
cbahwtz

user output
cbahwtz

Test 11

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

user output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
Truncated

Test 12

Verdict: UNKNOWN

input
a

correct output
(empty)

user output
(not available)

Test 13

Verdict: UNKNOWN

input
aa

correct output
a

user output
(not available)

Test 14

Verdict: UNKNOWN

input
ab

correct output
(empty)

user output
(not available)

Test 15

Verdict: UNKNOWN

input
zz

correct output
z

user output
(not available)