제출 #585544

#제출 시각아이디문제언어결과실행 시간메모리
585544talant117408Palindromic Partitions (CEOI17_palindromic)C++17
100 / 100
300 ms27948 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #define pb push_back #define mp make_pair #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define lb lower_bound #define ub upper_bound #define sz(v) int((v).size()) #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' const int mod = 1e9 + 7; ll get(int i, int len, vector <ll> &pw, vector <ll> &hash) { auto res = hash[i + len - 1]; if (i) res = ((res - hash[i - 1]) % mod + mod) % mod; return res; } void solve() { string s; cin >> s; int n = sz(s); const int P = 37; vector <ll> pw(n), hash(n); pw[0] = 1; for (int i = 1; i < n; i++) { pw[i] = ((pw[i - 1] * P) % mod + mod) % mod; } for (int i = 0; i < n; i++) { hash[i] = (((s[i] - 'a' + 1) * pw[i]) % mod + mod) % mod; if (i) hash[i] = ((hash[i] + hash[i - 1]) % mod + mod) % mod; } int l = 0, r = n - 1; int ans = 0; for (int step = 0; step < n - step - 1; step++) { int l1 = step; int r1 = n - step - 1; auto ans1 = ((get(l, l1 - l + 1, pw, hash) * pw[r1 - l]) % mod + mod) % mod; auto ans2 = get(r1, r - r1 + 1, pw, hash); //cout << l << ' ' << l1 << ' ' << r1 << ' ' << r << endl; //cout << ans1 << ' ' << ans2 << endl; //cout << s.substr(l, l1 - l + 1) << ' ' << s.substr(r1, r - r1 + 1) << endl; if (ans1 == ans2) { ans += 2; l = l1 + 1; r = r1 - 1; } } if (l <= r) ans++; cout << ans << endl; } int main() { do_not_disturb int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...