Submission #346112

#TimeUsernameProblemLanguageResultExecution timeMemory
346112ali_tavakoliPalindromic Partitions (CEOI17_palindromic)C++14
100 / 100
78 ms19080 KiB
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")

const int maxn = 1e6 + 5, base = 29, md = 1e9 + 7;

int t, n;
ll pw[maxn];
string s;

void solve()
{
    cin >> s;
    ll h1 = 0, h2 = 0;
    int a = 0, b = s.size() - 1, p = 0, ans = 0;
    while(a < b)
    {
        h1 = ((h1 * base % md) + s[a ++]) % md;
        h2 = (h2 + pw[p ++] * s[b --] % md) % md;
        if(h1 == h2)
        {
            ans += 2;
            h1 = h2 = p = 0;
        }
    }
    if(h1 != h2|| a == b)
        ans ++;
    cout << ans << '\n';
}

int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    pw[0] = 1;
    for(int i = 1; i < maxn; i++)
        pw[i] = pw[i - 1] * base % md;
    cin >> t;
    while(t --)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...