Submission #1367826

#TimeUsernameProblemLanguageResultExecution timeMemory
1367826pieterbotPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
183 ms17116 KiB
#include <bits/stdc++.h>
#include <iomanip>

using namespace std;
using ll = long long ;
using pii = pair <int,int> ;
using pll = pair<ll,ll> ;
using vi = vector <int> ; 
using vll = vector < ll > ;
using tiii = tuple<int,int,int> ; 
using ld = long double ;
using big = __int128 ;

#define bra(x) "[" << (x) << "] "
#define ndl '\n' ;
#define all(x) (x).begin() , (x).end() 
#define sz(x) (int)(x).size() 
#define nd second 
#define st first
#define vvll vector<vll> 
#define vvi vector<vi>

#define dbg_vec(x) cout << #x << " : " ; for(auto cc : (x) ) cout << cc << ' ' ; cout << endl 
#define DEBU
#ifdef DEBUG 
#define dbg(x) cout << #x << " = " << x << endl 
#else 
#define dbg(x) 
#endif


int N , M ;
string S ;

struct StringHash {
    string lS ; // localS
    ll base = 314159 , mod = (1LL<<61) - 1 ;
    vll H , pot ;
    ll mul( ll a , ll b ){
        return ( (big)a * b ) % mod ;
    }
    void init( ){
        // hashtable
        H.assign(N+7,0) ;
        pot.assign(N+7,1) ;
        for(int i=0;i<N;i++){
            pot[i+1] = mul(pot[i],base) ;
            H[i+1] = mul(H[i],base) + S[i] ;
        }
    }
    ll get_hash( ll l , ll r ){
        l++ , r++ ;
        if( l > r) return 0 ;
        ll res = ( H[r] - mul(H[l-1],pot[r-l+1]) + mod ) % mod  ;
        return res ;
    }
    bool are_eq( ll l , ll r , ll len ){
        ll r1 = get_hash(l,l+len-1) ; 
        ll r2 = get_hash(r,r+len-1) ;
        return r1 == r2 ;
    }
} ;
void solve(){
    cin >> S ; 
    N = sz(S) ;
    StringHash H ; H.init() ;
    int lptr = 0 , rptr = N-1 ;
    int len = 1 , tans = 1 ;
    while(lptr + len -1 < rptr - len +1){
        if( H.are_eq(lptr,rptr-len+1,len) ){
            lptr += len , rptr -= len ;
            len = 1 ;
            tans += 2 ;
        }
        else{
            len ++ ;
        }
    }
    if( lptr -1 == rptr ) tans -- ;
    
    cout << tans << ndl ;
}
int main(){
    ios_base::sync_with_stdio(false) ; cin.tie(0) ;
    int t ; cin >> t ; 
    while( t-- ) solve() ;
    return 0 ;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...