Submission #942155

#TimeUsernameProblemLanguageResultExecution timeMemory
942155vjudge1Palindromic Partitions (CEOI17_palindromic)C++17
0 / 100
8 ms15960 KiB
#include <bits/stdc++.h>
#define endl "\n"
#define int long long

using namespace std;

int q, n, P = 47, cnt = 1;
const int N = 1000000, M = 1000000007;
int p[N], h[N];
string s;

bool check_equal(int a, int b, int l) {
  int ha = (h[a + l] - h[a] + M) % M;
  int hb = (h[b + l] - h[b] + M) % M;
  return 1LL * ha * p[b - a] % M == hb;
}


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

    cin >> q;
    p[0] = 1;
    for (int i = 1; i < N; i++)
    {
        p[i] = p[i - 1] * P % M;
    }
    while(q--)
    {
        cin >> s;
        cnt = 1;
        n = s.size();
        h[0] = 0;
        for (int i = 1; i <= n; i++)
        {
            h[i] = (h[i-1] + (1LL * (s[i-1] - 'a' + 1) * p[i - 1] % M)) % M;
        }
        int l = 0, r = n, k = 1;
        while(l < r && l + k < r - k)
        {
            if(check_equal(l, r - k, k))
            {
                l += k;
                r -= k;
                k = 1;
                cnt += 2;
            }
            else k++;
        }
        cout << cnt << endl;
        memset(h, 0, sizeof(h));

    }







    return 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...