Submission #1129934

#TimeUsernameProblemLanguageResultExecution timeMemory
1129934rayan_bdPalindromic Partitions (CEOI17_palindromic)C++20
60 / 100
10091 ms2528 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; 


#define getar(ar,n) for(ll i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define fi first
#define se second

const int mxN = 1e5 + 5;
const ll MOD1 = 1e9 + 9;
const ll MOD2 = 998244353;
const ll INF = 1e9;
const ld EPS = 1e-9;

ll dp1[mxN],dp2[mxN],inv1[mxN],inv2[mxN];

struct sub_hash
{
	ll pw(ll n,ll k,ll m){
		ll ans=1;
		while(k>0){
			if(k&1) ans=(ans*n)%m;
			n=(n*n)%m;
			k>>=1;
		}
		return ans;
	}

	pair<ll,ll> qry(ll i,ll j){
		ll f=(dp1[j+1]-dp1[i]+MOD1)%MOD1*inv1[i]%MOD1;
		ll s=(dp2[j+1]-dp2[i]+MOD2)%MOD2*inv2[i]%MOD2;
		return {f,s};
	}

	void init(string inp_string){
		ll p=31,p1=1,p2=1,n=inp_string.size();
		for(ll i=0;i<n;++i){
			dp1[i+1]=(dp1[i]+(inp_string[i]-'a'+1)*p1%MOD1)%MOD1;
			dp2[i+1]=(dp2[i]+(inp_string[i]-'a'+1)*p2%MOD2)%MOD2;
			p1=p1*p%MOD1;
			p2=p2*p%MOD2;
		}
		inv1[n-1]=pw(pw(p,n-1,MOD1),MOD1-2,MOD1);
		inv2[n-1]=pw(pw(p,n-1,MOD2),MOD2-2,MOD2);
		for(int i=n-2;i>=0;--i){
        	inv1[i]=inv1[i+1]*p%MOD1;
        	inv2[i]=inv2[i+1]*p%MOD2;
    	}
	}
};

string sub_str(string m,ll i,ll j){
	string ans="";
	while(i<=j) ans.pb(m[i++]);
	return ans;
}

void solve(ll tc) {
    string str;cin>>str;
    ll n=str.size();
   /* sub_hash hsh; 
    hsh.init(str);*/
    ll fst=0,fen=0,est=n-1,een=n-1,ans=0;
    while(fen<est){
    	/*    	pair<ll,ll> q1=hsh.qry(fst,fen);
    	pair<ll,ll> q2=hsh.qry(est,een);*/
        string q1 = sub_str(str,fst,fen);
        string q2 = sub_str(str,est,een);
    	if(q1==q2){
    		ans+=2;
    		fst=++fen,een=--est;
    	}else{
    		++fen,--est;
    	}
    }
    if(fst>een) show(ans);
    else show(ans+1);
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll tc;//=1;
    cin>>tc;
    for (ll t = 1; t <= tc; t++) {
        solve(t);
    }
    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...