Submission #707800

#TimeUsernameProblemLanguageResultExecution timeMemory
707800guagua0407Palindromic Partitions (CEOI17_palindromic)C++17
100 / 100
316 ms28632 KiB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end() 
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=1e6+5;
ll p=27;
ll mod=(1ll<<61)-1;
ll powv[mxn];
ll hashv[mxn];

void pre(){
    powv[0]=1;
    for(int i=1;i<mxn;i++){
        powv[i]=((__int128)powv[i-1]*p)%mod;
    }
}

void genhash(string str){
    for(int i=1;i<=str.length();i++){
        hashv[i]=((__int128)hashv[i-1]*p%mod+str[i-1]-'a'+1)%mod;
    }
}

ll cal(int l,int r){
    ll ans=(hashv[r]-(__int128)hashv[l-1]*powv[r-l+1]%mod)%mod;
    if(ans<0) ans+=mod;
    return ans;
}

void solve(){
    string str;
    cin>>str;
    int n=str.length();
    genhash(str);
    int l=1;
    int ans=0;
    while(l<=n/2){
        int r=l;
        bool tf=false;
        while(r<=n/2){
            if(cal(l,r)==cal(n-l+1-(r-l+1)+1,n-l+1)){
                tf=true;
                break;
            }
            r++;
        }
        if(tf){
            ans++;
            l=r+1;
        }
        else{
            break;
        }
    }
    ans*=2;
    if(l!=n/2+1 or n&1) ans++;
    cout<<ans<<'\n';
}

int main() {_
    //setIO("wayne");
    pre();
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
//maybe its multiset not set

Compilation message (stderr)

palindromic.cpp: In function 'void genhash(std::string)':
palindromic.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for(int i=1;i<=str.length();i++){
      |                 ~^~~~~~~~~~~~~~
palindromic.cpp: In function 'void setIO(std::string)':
palindromic.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...