# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1170612 | InvMOD | Palindromic Partitions (CEOI17_palindromic) | C++20 | 158 ms | 11168 KiB |
#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)(v).size()
const int MOD = 1e9 + 2277, base = 31;
void solve()
{
string s; cin >> s;
int n = sz(s); s = " " + s;
vector<int> hsh(n + 1, 0), pw(n + 1, 1);
for(int i = 1; i <= n; i++){
hsh[i] = 1ll * hsh[i - 1] * base % MOD;
hsh[i] = (hsh[i] + (s[i] - 'a')) % MOD;
pw[i] = 1ll * pw[i - 1] * base % MOD;
}
auto get_hash = [&](int l, int r) -> int{
return ((hsh[r] - (1ll * hsh[l - 1] * pw[r - l + 1] % MOD)) + MOD) % MOD;
};
// (de) (co) (de) | (de) (co) (de)
// if we want to combine it again we need 3 small partition that should be palindrome
// prefix + middle + suffix and prefix == suffix
// so middle should be = middle
// so we can create 3 smaller partition
int answer = 0, curLen = 0;
for(int l = 1, r = n; l <= r; l++, r--){
++curLen;
//cout << l - curLen + 1 <<" " << l <<" " << r <<" " << r + curLen - 1 << "\n";
if(get_hash(l - curLen + 1, l) == get_hash(r, r + curLen - 1)){
answer = answer + 2 - (l == r);
curLen = 0;
}
}
cout << answer + min(curLen, 1) << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int tc; cin >> tc;
while(tc--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |