답안 #915992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915992 2024-01-25T05:38:57 Z nasir_bashirov Palindromic Partitions (CEOI17_palindromic) C++11
0 / 100
1 ms 2396 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;
                }
            }
            res += 2, p += best;
        }
        // cout << p << endl;
        if(n - 2 * p > 0) res += 2;
        cout << res - 1 << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -