Submission #446513

#TimeUsernameProblemLanguageResultExecution timeMemory
446513JasperLPalindromic Partitions (CEOI17_palindromic)C++14
100 / 100
413 ms27816 KiB
#include <iostream> #include <vector> #include <cmath> using namespace std; #define maxn 1000005 typedef long long ll; ll mod = 1e9 + 7; ll p = 29; string s; int n; vector<int> ret; ll h[maxn]; // hash values ll pw[maxn]; bool match(int i, int j, int len) { // if [i ... i+len) == [j ... j+len) ll H1 = h[i+len-1] - pw[len] * h[i-1]; H1 %= mod; if (H1 < 0) H1 += mod; ll H2 = h[j+len-1] - pw[len] * h[j-1]; H2 %= mod; if (H2 < 0) H2 += mod; return H1 == H2; } void solve() { n = s.length(); s = '$' + s; for (int i = 1; i <= n; i++) { h[i] = h[i-1] * p + (s[i]-'a'); h[i] %= mod; } int l = 1, r = n; int ans = 0; for (int i = 1, j = n; i < j; i++, j--) { if (match(l,j,i-l+1)) { ans += 2; l = i+1; r = j-1; continue; } } if (l <= r) ans++; ret.push_back(ans); } int main() { int t; cin >> t; pw[0] = 1; for (int i = 1; i < maxn; i++) pw[i] = (pw[i-1] * p)%mod; for (int i = 0; i < t; i++) { cin >> s; solve(); } for (int z : ret) cout << z << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...