제출 #1150660

#제출 시각아이디문제언어결과실행 시간메모리
1150660ziadabdouPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
43 ms1508 KiB
#include <bits/stdc++.h>
using namespace std;

#include <tr2/dynamic_bitset>
using dynamic_bitset = tr2::dynamic_bitset<>;
#define db dynamic_bitset


#define ll long long
#define el endl
#define pb push_back
// #define sz(a) (int)a.size()
#define yes cout << "YES"<<el
#define no cout << "NO" << el
#define all(v)   v.begin(), v.end()
#define sp ' '
#define md(a) (((a%mod)+mod)%mod)


const int N = 5e4 + 1;

/*
A Wise Man Once Said: if it works , don't touch it
and a real Man said : " تجري الرياح بما شاء الاله لها, لله نحن وموج البحر والسفن "

but me want say
#define ريح  rating
#define سفن  expert
     " ما عدت أدري كيف الريح اتيه اني غارق حيث لاريح ولا سفن "
 */


const ll mod = 998244353;
const ll M1 = 1000000007;

const ll M = 998244353;

struct Hashing {
private:
    int mod1 = 1e9 + 123, mod2 = 998244353;
    ll base1, base2, h1, h2, inv1, inv2, *pw1, *pw2, len;
    deque<char> d;
    ll power(ll a,ll b,ll m) {
        ll ans = 1;
        while (b > 0) {
            if (b & 1) {
                ans = (ans * a) % m;
            }
            a = (a * a) % m;
            b >>= 1;
        }
        return ans;
    }

public:
    Hashing(int sz,ll x = 131,ll y = 69420) {
        base1 = x;
        base2 = y;
        h1 = h2 = len = 0;
        inv1 = power(x, mod1 - 2, mod1);
        inv2 = power(y, mod2 - 2, mod2);
        pw1 = new ll[sz + 1];
        pw2 = new ll[sz + 1];
        pw1[0] = pw2[0] = 1;
        for (int i = 1; i <= sz; i++) {
            pw1[i] = (x * pw1[i - 1]) % mod1;
            pw2[i] = (y * pw2[i - 1]) % mod2;
        }
    }

    void push_back(char x) {
        x = x - 'a' + 1;
        h1 = (h1 * base1) % mod1;
        h1 = (h1 + x) % mod1;
        h2 = (h2 * base2) % mod2;
        h2 = (h2 + x) % mod2;
        len++;
        d.emplace_back(x);
    }

    void push_front(char x) {
        x = x - 'a' + 1;
        h1 = (h1 + (x * pw1[len]) % mod1) % mod1;
        h2 = (h2 + (x * pw2[len]) % mod2) % mod2;
        len++;
        d.emplace_front(x);
    }

    void pop_back() {
        if (len == 0)return;
        char x = d.back();
        d.pop_back();
        h1 = (h1 - x + mod1) % mod1;
        h1 = (h1 * inv1) % mod1;
        h2 = (h2 - x + mod2) % mod2;
        h2 = (h2 * inv2) % mod2;
        len--;
    }


    void pop_front() {
        if (len == 0)return;
        char x = d.front();
        d.pop_front();
        len--;
        h1 = ((h1 - x * pw1[len] % mod1) + mod1) % mod1;
        h2 = ((h2 - x * pw2[len] % mod2) + mod2) % mod2;
    }

    void clear() {
        h1 = h2 = len = 0;
        d.clear();
    }

    bool operator==(const Hashing &H) const {
        return H.h1 == h1 && H.h2 == h2;
    }

    string GetString() {
        return string(d.begin(), d.end());
    }

    pair<ll, ll> GetHash() {
        return {h1, h2};
    }
};

const int BASE = 265;
ll mul(ll a, ll b, ll m) { return a * 1ll * b % m; }
ll add(ll a, ll b, ll m) {
    return ((a + b) % m + m) % m;
}

ll fp(ll b, ll p, ll m) {
    ll res = 1;
    while (p) {
        if (p & 1) res = mul(res, b, m);
        p >>= 1;
        b = mul(b, b, m);
    }
    return res;
}

ll inv(ll x, ll m) {
    return fp(x, m - 2, m);
}


int gen_base(const int before, const int after) {
    auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::mt19937 mt_rand(seed);
    int base = std::uniform_int_distribution<int>(before + 1, after)(mt_rand);
    return base % 2 == 0 ? base - 1 : base;
}

string s;
ll base=701;
void solve()
{
    cin>>s;
    ll ans=0, h1=0, h2=0;
    ll n=s.size(), t=1;
    for(ll i=0;i<n-i-1;i++)
    {
        h1=md(h1*base+(s[i]-'a'));
        h2=md(h2+(s[n-i-1]-'a')*t);
        t=md(t*base);
        if(h1==h2)
        {
            ans+=2;
            h1=h2=0;
            t=1;
        }
    }
    if(h1!=h2 || n&1) ans++;
    cout<<ans<<"\n";
}


int main() {
    //freopen("filename.in", "r", stdin);
    //freopen("filename.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


    int tt = 1;
    cin >> tt;
    while (tt-- > 0) {
        solve();
    }

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