답안 #168374

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
168374 2019-12-12T20:05:02 Z Thuleanx Periodicity (POI11_okr) C++14
66 / 100
1000 ms 4652 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> extract(string &s) {
	int n = s.length();
	vector<int> pi(n);
	pi[0] = 0;
	for (int i = 1; i < n; i++) {
		int j = pi[i-1];
		while (j > 0 && s[i] != s[j])
			j = pi[j-1];
		if (s[i] == s[j])
			j++;
		pi[i] = j;
	}
	vector<int> borders;
	int j = n;
	while (j>0) {
		borders.push_back(j);
		j = pi[j-1];
	}
	reverse(begin(borders), end(borders));
	return borders;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t; cin>>t;
	stringstream ss;
	while (t--) {
		string s; cin>>s;
		vector<int> p = extract(s); // all borders are here		

		// get seed
		string ans;
		if (p[0] == 1)
			ans.push_back('0');
		else for (int i = 0; i < p[0]; i++)
			ans.push_back('0'+(i==p[0]-1));
		
		for (int i = 1; i < p.size(); i++) {
			if (p[i] - 2*p[i-1] <= 0) {
				ans = ans.substr(0, p[i]-p[i-1]) + ans;
			} else {
				for (int j = 0; j < p[i]-2*p[i-1]; j++)
					ans.push_back('0');
				ans = ans + ans.substr(0,p[i-1]);
				vector<int> border_now = extract(ans); // O(N) but runs only O(\log{n}) time and O(1) amortized
				if (!equal(begin(p), begin(p)+i+1, begin(border_now)))
					ans[p[i]-p[i-1]-1] = '1';
			}
		}
		ss << ans << endl;
	}
	cout << ss.str();

	return 0;
}

Compilation message

okr.cpp: In function 'int main()':
okr.cpp:43:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < p.size(); i++) {
                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Execution timed out 1050 ms 4040 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1077 ms 4652 KB Time limit exceeded