Submission #1284369

#TimeUsernameProblemLanguageResultExecution timeMemory
1284369hiepsimauhongPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
140 ms35204 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)

#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';

#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pow lkhasf
#define FILE kangaroo

const int N = 1e6 + 5;
const int mod[] = {(int)1e9 + 7, (int)1e9 + 9};
const int base = 27;
const int oo = 1e18;

int n;
string s;
int pow[2][N];

struct Hashing{
        int pre[2][N];

        void build(string &t){
                FOR(i, 1, t.size() - 1){
                        FOR(j, 0, 1){
                                pre[j][i] = (pre[j][i - 1] * base % mod[j] + t[i] - 'a' + 1) % mod[j];
                        }
                }
        }

        ii get(int l, int r){
                int g0 = (pre[0][r] - pre[0][l - 1] * pow[0][r - l + 1] % mod[0] + mod[0]) % mod[0];
                int g1 = (pre[1][r] - pre[1][l - 1] * pow[1][r - l + 1] % mod[1] + mod[1]) % mod[1];

                return {g0, g1};
        }
} H;

void solve(){
        cin >> s;
        n = s.size();
        s = " " + s;
        H.build(s);

        int i = 0, ans = 0;

        while(i <= n / 2){
                int tmp = i;

                FOR(j, i + 1, n / 2){
                        ii lf = H.get(i + 1, j);
                        ii rg = H.get(n - j + 1, n - i);

                        if(lf == rg){
                                ans += 2;
                                i = j;
                                break;
                        }
                }
                if(i == tmp){
                        break;
                }
        }

        if(i < (n + 1) / 2){
                ans++;
        }
        cout << ans << '\n';
}

signed main(){ quickly
        if(fopen("FILE.in", "r")){
                freopen("FILE.in", "r", stdin);
                freopen("FILE.out", "w", stdout);
        }

        int test;
        cin >> test;

        pow[0][0] = pow[1][0] = 1;
        FOR(i, 1, 1e6){
                FOR(j, 0, 1){
                        pow[j][i] = pow[j][i - 1] * base % mod[j];
                }
        }

        while(test--){
                solve();
        }
}

Compilation message (stderr)

palindromic.cpp: In function 'int main()':
palindromic.cpp:86:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |                 freopen("FILE.in", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:87:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |                 freopen("FILE.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...