Submission #916043

# Submission time Handle Problem Language Result Execution time Memory
916043 2024-01-25T07:34:27 Z nasir_bashirov Palindromic Partitions (CEOI17_palindromic) C++11
100 / 100
1866 ms 35900 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
using namespace std;

#define db long double
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define fastio\
    ios_base::sync_with_stdio(0);\
    cin.tie(0);\
    cout.tie(0)\

#define int long long

const int sz = 1e6 + 5;
const int mod = 1e9 + 7;
string s;
int t, n, pre[sz], pw[sz], inv[sz];
map<pii, int> memo;

int sum(int a, int b){
    return (a + b) % mod;
}

int mult(int a, int b){
    return (a % mod) * (b % mod) % mod;
}

int sub(int a, int b){
    return (a - b + mod) % mod;
}

int binpow(int a, int b){
    if(b == 0)  return 1;
    if(b & 1)   return (a % mod * binpow(a, b - 1)) % mod;
    return binpow(a * a % mod, b / 2) % mod;
}

int get_hash(int l, int r){
    return mult(sub(pre[r], pre[l - 1]), inv[l - 1]);
}

signed main(){
    fastio;
    cin >> t;
    while(t--){
        cin >> s;
        n = s.size();
        s = ' ' + s;
        pw[0] = 1, inv[0] = 1;
        for(int i = 1; i <= n; i++){
            pw[i] = mult(pw[i - 1], 29);
            inv[i] = binpow(pw[i], mod - 2);
            pre[i] = sum(pre[i - 1], mult(s[i] - 'a' + 1, pw[i]));
        }
        int res = 0, p = 0;
        while(p < n / 2){
            int best = n - 2 * p;
            for(int i = 1; i <= n - 2 * p; i++){
                if(get_hash(p + 1, p + i) == get_hash(n - p - i + 1, n - p)){
                    best = i;
                    break;
                }
            }
            if(best == n - 2 * p)  res--;
            res += 2, p += best;
        }
        // cout << p << endl;
        if(n - 2 * p > 0) res++;
        cout << res << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 20 ms 2908 KB Output is correct
11 Correct 11 ms 2652 KB Output is correct
12 Correct 19 ms 2652 KB Output is correct
13 Correct 16 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 20 ms 2908 KB Output is correct
11 Correct 11 ms 2652 KB Output is correct
12 Correct 19 ms 2652 KB Output is correct
13 Correct 16 ms 2652 KB Output is correct
14 Correct 1866 ms 34584 KB Output is correct
15 Correct 987 ms 30820 KB Output is correct
16 Correct 1757 ms 35900 KB Output is correct
17 Correct 978 ms 21972 KB Output is correct