답안 #1116573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1116573 2024-11-21T20:23:07 Z PagodePaiva Palindromic Partitions (CEOI17_palindromic) C++17
60 / 100
1099 ms 16612 KB
#include<bits/stdc++.h>
#define int long long
#define fr first
#define sc second

using namespace std;

const int N = 100010;
const int p = 1e9+7;
const int q = 1e9+9;
const int b = 127;
int pot[N][2];
int potinv[N][2];

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

struct hs{
    pair <int, int> pref[N];
    void build(string s){
        pref[0] = {0, 0};
        for(int i = 0;i < s.size();i++){
            pref[i+1].fr = (pref[i].fr + ((s[i]-'a'+1)*pot[i][0])%p)%p;
            pref[i+1].sc = (pref[i].sc+((s[i]-'a'+1)*pot[i][1])%q)%q;
        }
    }
    pair <int, int> query(int l, int r){
        l++;
        r++;
        return {(((pref[r].first+p-pref[l-1].first)%p)*potinv[l-1][0])%p, (((pref[r].second+q-pref[l-1].second)%q)*potinv[l-1][1])%q};
    }
    bool get(int l1, int r1, int l2, int r2){
        if(query(l1, r1) == query(l2, r2)) return true;
        return false;
    }
} h;
int res[N];

void solve(){
    string s;
    cin >> s;
    h.build(s);
    int n = s.size();
    if(n == 1){
        cout << 1 << '\n';
        return;
    }
    int st = (n%2 == 0 ? (n-1)/2 : n/2);
    res[st+1] = 0;
    res[st] = (n%2 == 1 ? 1 : (s[st] == s[st+1] ? 2 : 1));
    for(int i = st-1;i >= 0;i--){
        int r = (n%2 == 1 ? st+st-i : st+st-i+1);
        //cout << i << ' ' << r << '\n';
        res[i] = 1;
        for(int j = i;j <= st;j++){
          //  cout << i << ' ' << j << ' ' << r-(j-i) << ' ' << r << ' ' << h.get(i, j, r-(j-i), r) << '\n';
            if(h.get(i, j, r-(j-i), r)){
                res[i] = max(res[i], res[j+1]+2);
            }
        }
    }
    cout << res[0] << '\n';
}

int32_t main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;
    pot[0][0] = pot[0][1] = 1;
    potinv[0][0] = potinv[0][1] = 1;
    for(int i = 1;i < N;i++){
        pot[i][0] = (pot[i-1][0]*b)%p;
        pot[i][1] = (pot[i-1][1]*b)%q;
        potinv[i][0] = binpow(pot[i][0], p-2, p);
        potinv[i][1] = binpow(pot[i][1], q-2, q);
    }
    while(t--){
        solve();
    }
    return 0;
}

Compilation message

palindromic.cpp: In member function 'void hs::build(std::string)':
palindromic.cpp:28:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |         for(int i = 0;i < s.size();i++){
      |                       ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 5704 KB Output is correct
2 Correct 47 ms 5708 KB Output is correct
3 Correct 48 ms 5612 KB Output is correct
4 Correct 47 ms 5704 KB Output is correct
5 Correct 46 ms 5704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 5704 KB Output is correct
2 Correct 47 ms 5708 KB Output is correct
3 Correct 48 ms 5612 KB Output is correct
4 Correct 47 ms 5704 KB Output is correct
5 Correct 46 ms 5704 KB Output is correct
6 Correct 47 ms 5704 KB Output is correct
7 Correct 47 ms 5584 KB Output is correct
8 Correct 46 ms 5704 KB Output is correct
9 Correct 46 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 5704 KB Output is correct
2 Correct 47 ms 5708 KB Output is correct
3 Correct 48 ms 5612 KB Output is correct
4 Correct 47 ms 5704 KB Output is correct
5 Correct 46 ms 5704 KB Output is correct
6 Correct 47 ms 5704 KB Output is correct
7 Correct 47 ms 5584 KB Output is correct
8 Correct 46 ms 5704 KB Output is correct
9 Correct 46 ms 5624 KB Output is correct
10 Correct 1099 ms 5960 KB Output is correct
11 Correct 393 ms 5816 KB Output is correct
12 Correct 709 ms 5952 KB Output is correct
13 Correct 510 ms 5856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 5704 KB Output is correct
2 Correct 47 ms 5708 KB Output is correct
3 Correct 48 ms 5612 KB Output is correct
4 Correct 47 ms 5704 KB Output is correct
5 Correct 46 ms 5704 KB Output is correct
6 Correct 47 ms 5704 KB Output is correct
7 Correct 47 ms 5584 KB Output is correct
8 Correct 46 ms 5704 KB Output is correct
9 Correct 46 ms 5624 KB Output is correct
10 Correct 1099 ms 5960 KB Output is correct
11 Correct 393 ms 5816 KB Output is correct
12 Correct 709 ms 5952 KB Output is correct
13 Correct 510 ms 5856 KB Output is correct
14 Runtime error 58 ms 16612 KB Execution killed with signal 11
15 Halted 0 ms 0 KB -