Submission #859477

#TimeUsernameProblemLanguageResultExecution timeMemory
859477hoanganh_siuuuuuuuuPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
111 ms42680 KiB
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for(int i = a; i <= b; i++)
#define fr(i, a, b) for(int i = a; i >= b; i--)
#define pii pair <int, int>
#define fi first
#define se second
#define pb push_back
#define in insert
#define vvec vector<vector<int>>

using namespace std;

const int N = 1e6 + 5;
const int base = 311;
const int mod = 1e9 + 7;
const int mod2 = 1e6 + 3;

string s;
int n, m, t;
int Pow[N], Hash[N];
int Pow2[N], Hash2[N];

int get(int i, int j)
{
    return (Hash[j] - Hash[i - 1] * Pow[j - i + 1] + mod * mod) % mod;
}

int get2(int i, int j)
{
    return (Hash2[j] - Hash2[i - 1] * Pow2[j - i + 1] + mod2 * mod2) % mod2;
}

signed main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> t;
    Pow[0] = Pow2[0] = 1;
    f(i ,1, mod2 - 2)
    {
        Pow[i] = (Pow[i - 1] * base) % mod;
        Pow2[i] = (Pow2[i - 1] * base) % mod2;
    }
    while(t--)
    {
        cin >> s;
        n = s.size();
        s = "!" + s;
        f(i, 1, n)
        {
             Hash[i] = (Hash[i - 1] * base + s[i] - 'a' + 1) % mod;
             Hash2[i] = (Hash2[i - 1] * base + s[i] - 'a' + 1) % mod2;
        }
        int l = 0, ok = 0, cnt = 0;
        f(i, 1, n / 2)
        {
            if(s[i] == s[n - i + 1] && l == 0)
            {
                cnt += 2;
                if(i == n / 2) ok = 1;
                //cout << i << " " << n - i + 1 << "\n";
            }
            else
            {
                if(!l) l = i;
                int j = n - l + 1;
                int k = n - i + 1;
                int left = get(l, i);
                int left2 = get2(l, i);
                int right = get(k, j);
                int right2 = get2(k, j);
                //cout << l << " " << i << " " << k << " " << j << "\n";
                //cout << left << " " << left2 << " " << right << " " << right2 << "\n";
                if(left == right && right2 == left2)
                {
                    if(i == n / 2) ok = 1;
                    l = 0;
                    //cout << "cc\n";
                    cnt += 2;
                }
            }
        }
        if(!ok || n % 2 == 1) cnt++;
        cout << cnt << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...