제출 #1342652

#제출 시각아이디문제언어결과실행 시간메모리
1342652dex111222333444555Palindromic Partitions (CEOI17_palindromic)C++20
100 / 100
97 ms11768 KiB
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define lli pair<long long, int>
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))

using namespace std;

const int MAXN = 1e6 + 6, infINT = 1e9 + 1321, mod = 1e9 + 7, base = 256;
const long long inf = 1e18 + 1231;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}

void add(int &a, const int &b){
    a += b;
    if (a >= mod) a -= mod;
}

void sub(int &a, const int &b){
    a -= b;
    if (a < 0) a += mod;
}

int mul(const int &a, const int &b){
    return 1LL * a * 1LL * b % mod;
}

int bin_pow(int a, int k){
    int res = 1;
    while(k){
        if (k & 1) res = mul(res, a);
        k >>= 1; a = mul(a, a);
    }
    return res;
}

int lenS, hashS[MAXN];
int powB[MAXN];
string s;

void init(){
    powB[0] = 1;
    for(int i = 1; i < MAXN; i++){
        powB[i] = mul(powB[i - 1], base);
    }
}

void input(){
    cin >> s;
    lenS = siz(s);
    s = '_' + s;
}

int getHash(const int &L, const int &R){
    int value = 0;
    add(value, hashS[R]);
    sub(value, mul(hashS[L - 1], powB[R - L + 1]));
    return value;
}

void solve(){
    hashS[0] = 0;
    for(int i = 1; i <= lenS; i++){
        hashS[i] = mul(hashS[i - 1], base);
        add(hashS[i], s[i] - 'a' + 1);
    }
    int l = 1, r = lenS, res = 0;
    while(l < r){
        int ll = l, rr = r;
        while(ll < rr){
            if (getHash(l, ll) == getHash(rr, r)){
                res += 2;
                break;
            }
            ll++; rr--;
        }
        if (ll >= rr){
            break;
        }
        l = ll + 1, r = rr - 1;
    }
    if (l <= r) res++;
    cout << res << '\n';
    cerr << res << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    init();
    int t = 1;
    cin >> t;
    while(t--){
        input();
        solve();
    }
    cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
}

컴파일 시 표준 에러 (stderr) 메시지

palindromic.cpp: In function 'int main()':
palindromic.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...