Submission #1170612

#TimeUsernameProblemLanguageResultExecution timeMemory
1170612InvMODPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
158 ms11168 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)(v).size()

const int MOD = 1e9 + 2277, base = 31;

void solve()
{
    string s; cin >> s;

    int n = sz(s); s = " " + s;

    vector<int> hsh(n + 1, 0), pw(n + 1, 1);
    for(int i = 1; i <= n; i++){
        hsh[i] = 1ll * hsh[i - 1] * base % MOD;
        hsh[i] = (hsh[i] + (s[i] - 'a')) % MOD;
        pw[i] = 1ll * pw[i - 1] * base % MOD;
    }

    auto get_hash = [&](int l, int r) -> int{
        return ((hsh[r] - (1ll * hsh[l - 1] * pw[r - l + 1] % MOD)) + MOD) % MOD;
    };

    // (de) (co) (de) | (de) (co) (de)
    // if we want to combine it again we need 3 small partition that should be palindrome

    // prefix + middle + suffix and prefix == suffix
    // so middle should be = middle
    // so we can create 3 smaller partition

    int answer = 0, curLen = 0;
    for(int l = 1, r = n; l <= r; l++, r--){
        ++curLen;
        //cout << l - curLen + 1 <<" " << l <<" " << r <<" " << r + curLen - 1 << "\n";
        if(get_hash(l - curLen + 1, l) == get_hash(r, r + curLen - 1)){
            answer = answer + 2 - (l == r);
            curLen = 0;
        }
    }

    cout << answer + min(curLen, 1) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int tc; cin >> tc;
    while(tc--)
        solve();
    return 0;
}

Compilation message (stderr)

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