Submission #915862

#TimeUsernameProblemLanguageResultExecution timeMemory
915862rahidilbayramliPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
287 ms18980 KiB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define f first
#define s second
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll sz = 1e6+5, mod = 1e9+7, P = 47;
ll p[sz], h[sz], n, i;
bool check(ll a, ll b, ll k)
{
    ll h1 = (h[a + k - 1] - h[a - 1] + mod) % mod;
    ll h2 = (h[b + k - 1] - h[b - 1] + mod) % mod;
    return (h1 * 1LL * p[b - a]) % mod == h2;
}
int main()
{
    ll tests = 1;
    cin >> tests;
    while(tests--)
    {
        string s;
        cin >> s;
        n = s.size();
        p[0] = 1;
        for(i = 1; i <= n; i++)
            p[i] = (p[i-1] * P * 1LL) % mod;
        for(i = 1; i <= n; i++)
            h[i] = (h[i-1] + ((s[i - 1] - 'a' + 1) * 1LL * p[i-1])) % mod;
        ll ans = 0, l = 1, r = n, k = 1, curl = -1, curr = -1;
        while(l < r)
        {
            ll a = l - k + 1;
            if(check(a, r, k))
            {
                ans++;
                k = 1;
                curl = l;
                curr = r;
            }
            else
                k++;
            l++;
            r--;
        }
        if(curl + 1 == curr)
            cout << ans * 2 << "\n";
        else
            cout << ans * 2 + 1 << "\n";
        for(i = 0; i <= n; i++)
        {
            h[i] = 0;
            p[i] = 0;
        }
        n = 0;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...