Submission #824050

# Submission time Handle Problem Language Result Execution time Memory
824050 2023-08-13T12:18:33 Z NK_ Palindromic Partitions (CEOI17_palindromic) C++17
100 / 100
275 ms 34928 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;

const int MOD = 1e9 + 7;

using H = array<int, 2>;
H base = {1007, 513};
H makeH(char c) { return {c, c}; }

H operator+(H l, H r) {
	for(int i = 0; i < 2; i++) if ((l[i] += r[i]) >= MOD) l[i] -= MOD;
	return l;
}

H operator-(H l, H r) {
	for(int i = 0; i < 2; i++) if ((l[i] -= r[i]) < 0) l[i] += MOD;
	return l;
}

H operator*(H l, H r) {
	for(int i = 0; i < 2; i++) l[i] = (l[i] * 1LL * r[i]) % MOD;
	return l;
}
 

V<H> pows{{1, 1}};
struct HASH {
	V<H> cum{{}}; string S;
	void add(char c) { cum.pb(cum.back() * base + makeH(c)); }
	void add(string T) { for(auto c : T) add(c); }
	void extend(int len) { while(sz(pows) <= len) pows.pb(pows.back() * base); }
	H hash(int l, int r) { extend(r-l+1); return cum[r+1] - pows[r-l+1] * cum[l]; }
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int T; cin >> T;
	while(T--) {
		string S; cin >> S;
		int N = sz(S);

		HASH h; h.add(S);
		auto check = [&](int l, int r) {
			return h.hash(l, r) == h.hash(N - 1 - r, N - 1 - l);
		};

		// cout << S << endl;

		int ans = 0;
		int l = 0; int M = N / 2;
		for(int r = 0; r < M; r++) {
			if (check(l, r)) ans += 2, l = r + 1;
		}

		int half = (N + 1) / 2;
		if (l < half) ans++;

		cout << ans << nl;
	}

    return 0;
}			

# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 3 ms 736 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 3 ms 700 KB Output is correct
13 Correct 3 ms 628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 3 ms 736 KB Output is correct
11 Correct 2 ms 608 KB Output is correct
12 Correct 3 ms 700 KB Output is correct
13 Correct 3 ms 628 KB Output is correct
14 Correct 265 ms 34928 KB Output is correct
15 Correct 146 ms 28500 KB Output is correct
16 Correct 275 ms 26904 KB Output is correct
17 Correct 154 ms 19048 KB Output is correct