Submission #859015

#TimeUsernameProblemLanguageResultExecution timeMemory
859015VinhLuuPalindromic Partitions (CEOI17_palindromic)C++17
60 / 100
10030 ms91548 KiB
#pragma GCC optimize("O2") #include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9 + 7; const int Max = 1e6 + 5; const ll dm = 998244353; string s; int dp[Max],n; ll lt[Max],hs[Max],t[Max],pw[Max]; int get(int i,int j){ return (hs[j]%mod + mod - (1LL * hs[i - 1]*lt[j - i + 1])%mod)%mod; } int got(int i,int j){ return (t[j]%dm + dm - (1LL * t[i - 1]*pw[j - i + 1])%dm)%dm; } int dfs(int i){ if(dp[i] != 0) return dp[i]; dp[i] = 1; int l = i, r = n - i + 1; if(l > r) return dp[i] = 0; if(l == r) return dp[i] = 1; int mid = (r - l + 1) / 2; for(int j = 1; j <= mid; j ++){ if(get(l, l + j - 1) == get(r - j + 1, r) && got(l, l + j - 1) == got(r - j + 1, r)){ dp[i] = max(dp[i], 2 + dfs(l + j)); } } return dp[i]; } void solve(){ cin >> s; n = s.size(); s = " " + s; for(int i = 1; i <= n; i ++) dp[i] = 0; for(int i = 1; i <= n; i ++) hs[i] = ((1LL * hs[i - 1] * 26)%mod + (int)(s[i] - 'a'))%mod; for(int i = 1; i <= n; i ++) t[i] = ((1LL * t[i - 1] * 26)%dm + (int)(s[i] - 'a'))%dm; cout << dfs(1) << "\n"; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); lt[0] = 1; pw[0] = 1; for(int i = 1; i <= Max - 5; i ++) lt[i] = (1LL * lt[i-1] * 26)%mod, pw[i] = (1LL * pw[i - 1] * 26)%dm; int tt; cin >> tt; while(tt--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...