Submission #585544

# Submission time Handle Problem Language Result Execution time Memory
585544 2022-06-29T04:39:44 Z talant117408 Palindromic Partitions (CEOI17_palindromic) C++17
100 / 100
300 ms 27948 KB
#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 time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 508 KB Output is correct
12 Correct 4 ms 560 KB Output is correct
13 Correct 3 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 508 KB Output is correct
12 Correct 4 ms 560 KB Output is correct
13 Correct 3 ms 548 KB Output is correct
14 Correct 300 ms 27948 KB Output is correct
15 Correct 150 ms 22448 KB Output is correct
16 Correct 278 ms 26056 KB Output is correct
17 Correct 153 ms 15176 KB Output is correct