Submission #1125471

#TimeUsernameProblemLanguageResultExecution timeMemory
1125471ducanh0811Palindromic Partitions (CEOI17_palindromic)C++20
100 / 100
145 ms35184 KiB
//
#include <bits/stdc++.h>
#define         int             long long
#define         fi              first
#define         se              second
#define         pii             pair<int,int>
#define         pb              push_back
#define         eb              emplace_back
#define         MASK(i)         (1ll << (i))
#define         FOR(i,a,b)      for (int i = (a); i <= (b); ++i)
#define         REV(i,a,b)      for (int i = (a); i >= (b); --i)
using namespace std;

const int MOD[] = {1000000007, 998244353};
const int base = 727;

int n;
string s;
#define MAXN 1000005
int p[2][MAXN];
int f[MAXN];
int g[MAXN];

pair<int,int> get(int l, int r){
    int f1 = ((f[r] - f[l - 1] * p[0][r - l + 1]) % MOD[0] + MOD[0]) % MOD[0];
    int f2 = ((g[r] - g[l - 1] * p[1][r - l + 1]) % MOD[1] + MOD[1]) % MOD[1];
    return make_pair(f1, f2);
}

bool bang(pair<int,int> A, pair<int,int> B){
    return (A.first == B.first && A.second == B.second);
}

void solve(){
    cin >> s;
    n = s.size();
    s = '#' + s;
    FOR(i,1,n){
        f[i] = (f[i - 1] * base + s[i] - 'a' + 1) % MOD[0];
        g[i] = (g[i - 1] * base + s[i] - 'a' + 1) % MOD[1];
    }
    int res = 1;
    int last_left = 1;
    int last_right = n;
    for (int i = 1, j = n; i < j;){
        if (bang(get(last_left, i), get(j, last_right))){
            res += 2;
            last_left = i + 1;
            last_right = j - 1;
        }
        i++;
        j--;
    }
    if (last_left - last_right == 1) res--;
    cout << res << '\n';
}

#define task ""
int32_t main(){
    if (fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    p[0][0] = 1;
    p[1][0] = 1;
    FOR(i,1,1000000){
        p[0][i] = (p[0][i - 1] * base) % MOD[0];
        p[1][i] = (p[1][i - 1] * base) % MOD[1];
    }
    int test; cin >> test; while (test--)
    solve();
    return 0;
}

Compilation message (stderr)

palindromic.cpp: In function 'int32_t main()':
palindromic.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         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...