답안 #250601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
250601 2020-07-18T13:54:11 Z Sorting Palindromic Partitions (CEOI17_palindromic) C++14
60 / 100
10000 ms 34424 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int k_N = 1e6 + 3;
const int cnt_Mod = 2;
const array<ll, cnt_Mod> k_Mod{(int)1e9 + 7, (int)1e9 + 93}, k_Base{293, 353};

int dp[k_N];
string s;
int n;

array<ll, cnt_Mod> h[k_N], b_pow[k_N];

ll get_hash(int l, int r, int idx){
    if(l == 0) return h[r][idx];
    ll ans = (h[r][idx] - h[l - 1][idx] * b_pow[r - l + 1][idx]) % k_Mod[idx];
    return (ans < 0) ? (ans + k_Mod[idx]) : ans;
}

bool equal(int l1, int r1, int l2, int r2){
    for(int i = 0; i < cnt_Mod; ++i)
        if(get_hash(l1, r1, i) != get_hash(l2, r2, i))
            return false;
    return true;
}

void init_hashing(){
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < cnt_Mod; ++j){
            if(i == 0)
                h[i][j] = s[i];
            else
                h[i][j] = (h[i - 1][j] * k_Base[j] + s[i]) % k_Mod[j];
        }
    }

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < cnt_Mod; ++j){
            if(i == 0)
                b_pow[i][j] = 1;
            else
                b_pow[i][j] = b_pow[i - 1][j] * k_Base[j] % k_Mod[j];
        }
    }
}

void solve(){
    cin >> s;
    n = (int)s.size();

    if(n == 1){
        cout << "1\n";
        return;
    }

    init_hashing();

    int mid = (n - 1) / 2, mid2 = (n - 2) / 2; 
    dp[mid + 1] = 0;
    for(int i = mid; i >= 0; --i){
        dp[i] = 1;
        for(int j = i; j <= mid2; ++j){
            if(equal(i, j, n - j - 1, n - i - 1)){
                //cout << "equal " << i << " " << j << " " << n - j - 1 << " " << n - i - 1 << "\n";
                dp[i] = max(dp[j + 1] + 2, dp[i]);
            }
            //else
            //    cout << "NOT equal " << i << " " << j << " " << n - j - 1 << " " << n - i - 1 << "\n";
        }
    }

    cout << dp[0] << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while(t--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 4201 ms 940 KB Output is correct
11 Correct 1330 ms 1144 KB Output is correct
12 Correct 2473 ms 888 KB Output is correct
13 Correct 1804 ms 788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 4201 ms 940 KB Output is correct
11 Correct 1330 ms 1144 KB Output is correct
12 Correct 2473 ms 888 KB Output is correct
13 Correct 1804 ms 788 KB Output is correct
14 Execution timed out 10071 ms 34424 KB Time limit exceeded
15 Halted 0 ms 0 KB -