# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1274249 | saleh05 | Palindromic Partitions (CEOI17_palindromic) | C++20 | 1 ms | 332 KiB |
#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define ll long long
#define FAST ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int MOD = 1e9 + 7 ;
const int INF = 1e18 + 7;
const int MOD2 = 1e9 + 9 ;
/*------------------------------------------------------------------------------------------------------------------------------*/
int dx[] = { -1 , 1 , 0 , 0 };
int dy[] = { 0 , 0 , -1 , 1 };
void flip(int x , int y , vector<string> &s) {
if (s[x][y] == 'b')s[x][y] = 'w';
else s[x][y] = 'b';
for (int i = 0 ; i < 4 ; i++) {
int nx = x + dx[i] , ny = y + dy[i];
if (nx >= 0 && nx <4 && ny >= 0 && ny <4) {
if (s[nx][ny] == 'b')s[nx][ny] = 'w';
else s[nx][ny] = 'b';
}
}
}
void solve() {
// int n ; cin >> n ;
string s ; cin >> s ;
int n = s.size() ;
vector<int> hash(n + 1), pow(n + 1 );
int p = 109;
pow[0] = 1 ;
for (int i = 1 ; i <= n ; i++) {
hash[i] = (hash[i - 1 ] * p + s[i - 1 ]) % MOD ;
pow[i] =( pow[i - 1] * p) % MOD ;
}
auto compare_s = [&](int l , int r ) -> int {
return ( hash[r + 1 ] - hash[l] * pow[r - l + 1 ] % MOD + MOD ) % MOD ;
};
int l = 0 , r = n - 1 ;
int cnt = 0 ;
while (l <= r ) {
bool found = false ;
if ( s[l] == s[r]) {
if (l == r) {
cnt++; l++ , r--;
}
else {
cnt += 2 ;
l++, r--;
}
continue ;
}
for (int len = 1 ; len < (r - l + 1) / 2 ; len++) {
if (compare_s(l , l + len - 1 ) == compare_s(r - len + 1 , r ) ) {
l+= len ;
r-= len ;
cnt+= 2 ;
found = true;
break;
}
}
if (!found) {
cnt+= 1;
break ;
}
}
cout << cnt << '\n';
}
int32_t main() {
FAST
#ifndef ONLINE_JUDGE
freopen("saleh.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
cin >> t ;
while (t--) {
solve();
}
return 0;
}
Compilation message (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... |