답안 #829049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
829049 2023-08-18T02:59:47 Z 1075508020060209tc Necklace (Subtask 1-3) (BOI19_necklace1) C++14
9 / 85
1500 ms 13720 KB
#pragma GCC optimize("Ofast, no-stack-protector, unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
int LN=50;
int n;int m;
int ar[6010];int br[6010];int rb[6010];
void init(){
string s;
cin>>s;
n=s.size();
s="*"+s;
for(int i=1;i<=n;i++){
    ar[i]=s[i]-'a'+1;
   // ar[i+n]=ar[i];
}
cin>>s;
m=s.size();
s="*"+s;
for(int i=1;i<=m;i++){
    br[i]=s[i]-'a'+1;
    //br[i+m]=br[i];
}
for(int i=1;i<=m;i++){
    rb[i]=br[i];
}
reverse(rb+1,rb+m+1);
}
int psa[200][3010];int psb[200][3010];
int pssa[200][3010];int pssb[200][3010];
int pxa[200][3010];int pxb[200][3010];
int psrb[200][3010];
int pssrb[200][3010];
int pxrb[200][3010];
void precalc(int len){
    for(int i=1;i+len-1<=n;i++){
        int v=ar[i];
        for(int j=2;j<=len;j++){
            v=v*10+ar[i+j-1];
        }
        psa[len][i]=(psa[len][i-1]+v)%mod;
        pssa[len][i]=(pssa[len][i-1]+v*v)%mod;
        pxa[len][i]=pxa[len][i-1]^v;
    }
    for(int i=1;i+len-1<=m;i++){
        int v=br[i];
        for(int j=2;j<=len;j++){
            v=v*10+br[i+j-1];
        }
        psb[len][i]=(psb[len][i-1]+v)%mod;
        pssb[len][i]=(pssb[len][i-1]+v*v)%mod;
        pxb[len][i]=pxb[len][i-1]^v;
    }
    for(int i=1;i+len-1<=m;i++){
        int v=rb[i];
        for(int j=2;j<=len;j++){
            v=v*10+rb[i+j-1];
        }
        psrb[len][i]=(psrb[len][i-1]+v)%mod;
        pssrb[len][i]=(pssrb[len][i-1]+v*v)%mod;
        pxrb[len][i]=pxrb[len][i-1]^v;
    }
}

int cyc(int l,int r,int st,int len){
    int ret=ar[st];
    for(int i=2;i<=len;i++){
        st++;
        if(st==r+1){st=l;}
        ret=ret*10+ar[st];
    }
    return ret;
}
int cycb(int l,int r,int st,int len){
    int ret=br[st];
    for(int i=2;i<=len;i++){
        st++;
        if(st==r+1){st=l;}
        ret=ret*10+br[st];
    }
    return ret;
}
int cycrb(int l,int r,int st,int len){
    int ret=rb[st];
    for(int i=2;i<=len;i++){
        st++;
        if(st==r+1){st=l;}
        ret=ret*10+rb[st];
    }
    return ret;
}

int ansa;int ansb;
int ok(int mi){
map<vector<array<int,3>>,int>mp;
for(int i=1;i<=n-mi+1;i++){
    vector<array<int,3>>vc;
    int l=i;int r=i+mi-1;
    for(int len=1;len<=LN;len++){
        array<int,3>tar;
        tar[0]=0;tar[1]=0;tar[2]=0;
        if(r-len+1>=l){
            tar[0]=(psa[len][r-len+1]-psa[len][l-1]+mod)%mod;
            tar[1]=(pssa[len][r-len+1]-pssa[len][l-1]+mod)%mod;
            tar[2]=pxa[len][r-len+1]^pxa[len][l-1];
        }
        for(int j=max(l,r-len+1+1);j<=r;j++){
            int v=cyc(l,r,j,len);
            tar[0]=(tar[0]+v)%mod;
            tar[1]=(tar[1]+v*v)%mod;
            tar[2]=tar[2]^v;
        }
      //  cout<<i<<" "<<tar[0]<<" "<<tar[1]<<" "<<tar[2]<<endl;
        vc.push_back(tar);
    }
    mp[vc]=i;
}
for(int i=1;i<=m-mi+1;i++){
    vector<array<int,3>>vc;
    int l=i;int r=i+mi-1;
    for(int len=1;len<=LN;len++){
        array<int,3>tar;
        tar[0]=0;tar[1]=0;tar[2]=0;
        if(r-len+1>=l){
            tar[0]=(psb[len][r-len+1]-psb[len][l-1]+mod)%mod;
            tar[1]=(pssb[len][r-len+1]-pssb[len][l-1]+mod)%mod;
            tar[2]=pxb[len][r-len+1]^pxb[len][l-1];
        }
        for(int j=max(l,r-len+1+1);j<=r;j++){
            int v=cycb(l,r,j,len);
            tar[0]=(tar[0]+v)%mod;
            tar[1]=(tar[1]+v*v)%mod;
            tar[2]=tar[2]^v;
        }
        vc.push_back(tar);
    }
    if(mp[vc]>=1){
        ansa=mp[vc];
        ansb=i;
        return 1;
    }
}
for(int i=1;i<=m-mi+1;i++){
    vector<array<int,3>>vc;
    int l=i;int r=i+mi-1;
    for(int len=1;len<=LN;len++){
        array<int,3>tar;
        tar[0]=0;tar[1]=0;tar[2]=0;
        if(r-len+1>=l){
            tar[0]=(psrb[len][r-len+1]-psrb[len][l-1]+mod)%mod;
            tar[1]=(pssrb[len][r-len+1]-pssrb[len][l-1]+mod)%mod;
            tar[2]=pxrb[len][r-len+1]^pxrb[len][l-1];
        }
        for(int j=max(l,r-len+1+1);j<=r;j++){
            int v=cycrb(l,r,j,len);
            tar[0]=(tar[0]+v)%mod;
            tar[1]=(tar[1]+v*v)%mod;
            tar[2]=tar[2]^v;
        }
        vc.push_back(tar);
    }
    if(mp[vc]>=1){
        ansa=mp[vc];
        ansb=m-(i+mi-1)+1;
        return 1;
    }
}
return 0;
}

signed main()
{
    init();
    for(int i=1;i<=LN;i++){
        precalc(i);
    }/*
    for(int i=1;i<=n;i++){
        cout<<ar[i]<<" ";
    }cout<<endl;
*/
  //  ok(1);return 0;

    int l=0;int r=n;
    while(l<r){
        int mi=l+(r-l+1)/2;
        if(mi==0||ok(mi)){
            l=mi;
        }else{
            r=mi-1;
        }
    }
    cout<<l<<endl;
    ok(l);
    cout<<ansa-1<<" "<<ansb-1<<endl;

}

Compilation message

necklace.cpp:1:63: warning: bad option '-f no-stack-protector' to pragma 'optimize' [-Wpragmas]
    1 | #pragma GCC optimize("Ofast, no-stack-protector, unroll-loops")
      |                                                               ^
necklace.cpp:1:63: warning: bad option '-f unroll-loops' to pragma 'optimize' [-Wpragmas]
necklace.cpp:9:11: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
    9 | void init(){
      |           ^
necklace.cpp:9:11: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:36:21: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
   36 | void precalc(int len){
      |                     ^
necklace.cpp:36:21: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:66:35: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
   66 | int cyc(int l,int r,int st,int len){
      |                                   ^
necklace.cpp:66:35: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:75:36: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
   75 | int cycb(int l,int r,int st,int len){
      |                                    ^
necklace.cpp:75:36: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:84:37: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
   84 | int cycrb(int l,int r,int st,int len){
      |                                     ^
necklace.cpp:84:37: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:95:14: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
   95 | int ok(int mi){
      |              ^
necklace.cpp:95:14: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
necklace.cpp:172:13: warning: bad option '-f no-stack-protector' to attribute 'optimize' [-Wattributes]
  172 | signed main()
      |             ^
necklace.cpp:172:13: warning: bad option '-f unroll-loops' to attribute 'optimize' [-Wattributes]
# 결과 실행 시간 메모리 Grader output
1 Partially correct 17 ms 2388 KB Output is partially correct
2 Partially correct 16 ms 2492 KB Output is partially correct
3 Partially correct 43 ms 2584 KB Output is partially correct
4 Partially correct 30 ms 2620 KB Output is partially correct
5 Partially correct 30 ms 2704 KB Output is partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 17 ms 2388 KB Output is partially correct
2 Partially correct 16 ms 2492 KB Output is partially correct
3 Partially correct 43 ms 2584 KB Output is partially correct
4 Partially correct 30 ms 2620 KB Output is partially correct
5 Partially correct 30 ms 2704 KB Output is partially correct
6 Partially correct 74 ms 3632 KB Output is partially correct
7 Partially correct 120 ms 3820 KB Output is partially correct
8 Correct 261 ms 4104 KB Output is correct
9 Partially correct 284 ms 4580 KB Output is partially correct
10 Partially correct 425 ms 4816 KB Output is partially correct
11 Partially correct 352 ms 4848 KB Output is partially correct
12 Partially correct 245 ms 4244 KB Output is partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 17 ms 2388 KB Output is partially correct
2 Partially correct 16 ms 2492 KB Output is partially correct
3 Partially correct 43 ms 2584 KB Output is partially correct
4 Partially correct 30 ms 2620 KB Output is partially correct
5 Partially correct 30 ms 2704 KB Output is partially correct
6 Partially correct 74 ms 3632 KB Output is partially correct
7 Partially correct 120 ms 3820 KB Output is partially correct
8 Correct 261 ms 4104 KB Output is correct
9 Partially correct 284 ms 4580 KB Output is partially correct
10 Partially correct 425 ms 4816 KB Output is partially correct
11 Partially correct 352 ms 4848 KB Output is partially correct
12 Partially correct 245 ms 4244 KB Output is partially correct
13 Partially correct 914 ms 13140 KB Output is partially correct
14 Partially correct 619 ms 12732 KB Output is partially correct
15 Execution timed out 1568 ms 13720 KB Time limit exceeded
16 Halted 0 ms 0 KB -