Submission #230645

# Submission time Handle Problem Language Result Execution time Memory
230645 2020-05-10T18:43:39 Z islingr Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
109 ms 22160 KB
#include <iostream>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(x, v) for (auto &x : v)

#define sz(x) int((x).size())

using ll = long long;
constexpr ll p = 5, M = 1e9 + 7, N = 1 << 20;

string s; int n;
ll pp[N], pref[N];

void init() {
	rep(i, 0, n) pref[i + 1] = (pref[i] + pp[i] * (s[i] - 'a' + 1)) % M;
}

ll hsh(int l, int r) { // [l, r]
	int ret = pref[r + 1] - pref[l];
	ret = ret * pp[n - l] % M;
	if (ret < 0) ret += M;
	return ret;
}

int solve() {
	cin >> s; n = sz(s);
	init();
	int cnt = 0;
	int x = 0, y = n - 1;
	while (x <= y) {
		// cerr << x << ' ' << y << '\n';
		int l = x, r = y;
		bool found = false;
		while (l < r) {
			if (hsh(x, l) == hsh(r, y)) {
				cnt += 2;
				found = true;
				break;
			}
			++l, --r;
		}
		if (!found) {
			++cnt;
			break;
		}
		x = l + 1;
		y = r - 1;
	}
	return cnt;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	pp[0] = 1;
	rep(i, 1, N) pp[i] = p * pp[i - 1] % M;

	int t; cin >> t;
	while (t--) cout << solve() << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8576 KB Output is correct
2 Correct 13 ms 8576 KB Output is correct
3 Correct 13 ms 8576 KB Output is correct
4 Correct 13 ms 8576 KB Output is correct
5 Correct 13 ms 8576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8576 KB Output is correct
2 Correct 13 ms 8576 KB Output is correct
3 Correct 13 ms 8576 KB Output is correct
4 Correct 13 ms 8576 KB Output is correct
5 Correct 13 ms 8576 KB Output is correct
6 Correct 13 ms 8576 KB Output is correct
7 Correct 13 ms 8576 KB Output is correct
8 Correct 14 ms 8576 KB Output is correct
9 Correct 14 ms 8576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8576 KB Output is correct
2 Correct 13 ms 8576 KB Output is correct
3 Correct 13 ms 8576 KB Output is correct
4 Correct 13 ms 8576 KB Output is correct
5 Correct 13 ms 8576 KB Output is correct
6 Correct 13 ms 8576 KB Output is correct
7 Correct 13 ms 8576 KB Output is correct
8 Correct 14 ms 8576 KB Output is correct
9 Correct 14 ms 8576 KB Output is correct
10 Correct 14 ms 8832 KB Output is correct
11 Correct 14 ms 8704 KB Output is correct
12 Correct 14 ms 8704 KB Output is correct
13 Correct 14 ms 8704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8576 KB Output is correct
2 Correct 13 ms 8576 KB Output is correct
3 Correct 13 ms 8576 KB Output is correct
4 Correct 13 ms 8576 KB Output is correct
5 Correct 13 ms 8576 KB Output is correct
6 Correct 13 ms 8576 KB Output is correct
7 Correct 13 ms 8576 KB Output is correct
8 Correct 14 ms 8576 KB Output is correct
9 Correct 14 ms 8576 KB Output is correct
10 Correct 14 ms 8832 KB Output is correct
11 Correct 14 ms 8704 KB Output is correct
12 Correct 14 ms 8704 KB Output is correct
13 Correct 14 ms 8704 KB Output is correct
14 Correct 109 ms 22160 KB Output is correct
15 Correct 67 ms 22032 KB Output is correct
16 Correct 103 ms 22036 KB Output is correct
17 Correct 66 ms 17940 KB Output is correct