제출 #1303373

#제출 시각아이디문제언어결과실행 시간메모리
1303373txq2109Palindromic Partitions (CEOI17_palindromic)C++20
0 / 100
0 ms336 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pu push
#define pb push_back
#define el cout<<"\n";
#define endl "\n"
#define M 1000006
#define ii pair<int,int>

using namespace std;
const int base=311,mod=1e9+7;
vector<int> h1,h2,pw;

int get_h(vector<int> h,int l,int r)
{
    int res=(h[r+1]-h[l]*pw[r-l+1])%mod;
    if(res<0) res+=mod;
    return res;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    #define FILE "SIXCUP"
    if(fopen(FILE".inp","r")) {
        freopen(FILE".inp","r",stdin);
        freopen(FILE".out","w",stdout);
    }
    int t; cin>>t;
    while(t--) {
        string s;
        int ans;
        cin>>s;
        h1.assign(s.size()+1,0);
        pw.assign(s.size()+1,1);

        for(int i=1;i<=s.size();i++) {
            pw[i]=pw[i-1]*base%mod;
            h1[i]=(h1[i-1]*base+s[i-1])%mod;
        }
        int l,r;
        int lastl,lastr;

        if(s.size()%2!=0) {
            bool solve=0;
            for(int mid=1;mid<=s.size();mid+=2) {
                ans=1;
                l=s.size()/2-mid;
                r=s.size()/2+mid;
                lastl=l; lastr=r;
                while(l>=0&&r<=s.size()-1) {
                    if(get_h(h1,l,lastl)==get_h(h1,lastr,r)) {
                        //cout<<l<<" "<<r<<endl;
                        if((get_h(h1,0,l-1)==get_h(h1,r+1,s.size()-1))||(l==0&&r==s.size()-1)) {
                            ans+=2;
                            //cout<<l<<" "<<r<<endl;
                            lastl=l-1; lastr=r+1;
                        }
                    }
                    l--; r++;
                }
                if(lastl==-1&&lastr==s.size()) solve=1;
                if(solve) {
                    cout<<ans<<endl;
                    break;
                }
            }
            if(!solve) cout<<1<<endl;
        }
        else {
            ans=0;
            l=s.size()/2-1;
            r=s.size()/2;
            lastl=l; lastr=r;
            while(l>=0&&r<s.size()) {
                if(get_h(h1,l,lastl)==get_h(h1,lastr,r)) {
                    if((get_h(h1,0,l-1)==get_h(h1,r+1,s.size()-1))||(l==0&&r==s.size()-1)) {
                        ans+=2;
                        lastl=l-1; lastr=r+1;
                    }
                }
                l--; r++;
            }
            if(ans==0) ans=1;
            cout<<ans<<endl;
        }
    }
    return 0;
}

//txq - ctq

컴파일 시 표준 에러 (stderr) 메시지

palindromic.cpp: In function 'int main()':
palindromic.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen(FILE".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen(FILE".out","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...