답안 #854114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854114 2023-09-26T07:28:58 Z GoldLight Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
187 ms 760 KB
#include <bits/stdc++.h>
using namespace std;
void fast(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}

const int N=6000;
int a[N+5], b[N+1];
void f(string &s, int *c){
    int j=0;
    for(int i=1; i<s.size(); i++){
        while(j && s[i]!=s[j]) j=c[j-1];
        if(s[i]==s[j]) j++;
        c[i]=j;
    }
}
tuple<int,int,int> solve(string &s, string &t, bool rev){
    int n=s.size(), m=t.size();
    tuple<int,int,int> ans={0, 0, 0};
    string t2=t;
    reverse(t2.begin(), t2.end());
    for(int i=0; i<n; i++){
        string s1=s.substr(0, i), s2=s.substr(i, n-i);
        reverse(s1.begin(), s1.end());
        s1+='#'+t, s2+='#'+t2;
        f(s1, a); f(s2, b);
        for(int j=1; j<=m; j++){
            ans=max(ans, {a[i+j]+b[n+m-i-j], i-a[i+j], rev? m-j-b[n+m-i-j] : j-a[i+j]});
        }
    }
    return ans;
}
int main(){
    fast();
    string s, t; cin>>s>>t;
    auto ans=solve(s, t, 0);
    reverse(t.begin(), t.end());
    ans=max(ans, solve(s, t, 1));
    cout<<get<0>(ans)<<'\n'<<get<1>(ans)<<' '<<get<2>(ans);
}
/*
vector<int> f1(string &s, string &t){
    int n=s.size(), m=t.size(), j=0;
    vector<int> a(n), b(m);
    for(int i=1; i<n; i++){
        while(j>0 && s[i]!=s[j]) j=a[j-1];
        if(s[i]==s[j]) j++;
        a[i]=j;
    }
    j=0;
    for(int i=0; i<m; i++){
        while(j==n || (j>0 && s[j]!=t[i])) j=a[j-1];
        if(s[j]==t[i]) j++;
        b[i]=j;
    }
    return b;
}
vector<int> f2(string &s, string &t){
    int n=s.size(), m=t.size(), j=0;
    vector<int> a(n), b(m);
    for(int i=1; i<n; i++){
        while(j>0 && s[i]!=s[j]) j=a[j-1];
        if(s[i]==s[j]) j++;
        a[i]=j;
    }
    j=0;
    for(int i=m-1; i>=0; i--){
        while(j==n || (j>0 && s[j]!=t[i])) j=a[j-1];
        if(s[j]==t[i]) j++;
        b[i]=j;
    }
    return b;
}
int main(){
    fast();
    string s, t1, t2; cin>>s>>t1;
    t2=t1; reverse(t2.begin(), t2.end());
    int n=s.size(), m=t1.size();
    int ki=1, ka=n, ans, j1, j2;
    while(ki<=ka){
        int k=(ki+ka)/2;
        bool cek=false;
        for(int l=0; l+k-1<n; l++){
            if(cek) break;
            if(k>m){
                cek=true;
                break;
            }
            string s1=s.substr(l, k);
            string s2=s1; reverse(s2.begin(), s2.end());
            auto val1=f1(s1, t1), val2=f2(s2, t1), val3=f1(s1, t2), val4=f2(s2, t2);
            for(int i=0; i+k-1<m; i++){
                if(val1[i+k-1]+val2[i]>=k){
                    ans=k, j1=l, j2=i;
                    cek=true;
                    break;
                }
            }
            for(int i=0; i+k-1<m; i++){
                if(val3[i+k-1]+val4[i]>=k){
                    ans=k, j1=l, j2=m-(i+k-1)-1;
                    cek=true;
                    break;
                }
            }
            // if(ans==k){
            //     cout<<s1<<'\n';
            //     for(int i=0; i<m; i++) cout<<val3[i]<<' ';
            //     cout<<'\n';
            //     for(int i=0; i<m; i++) cout<<val4[i]<<' ';
            //     cout<<'\n';
            // }
        }
        if(cek) ki=k+1;
        else ka=k-1;
    }
    cout<<ans<<'\n';
    cout<<j1<<' '<<j2;
}
*/
//KMP
/*
vector<int> f(string &s){
    int n=s.size(), j=0;
    vector<int> v(n);
    for(int i=1; i<n; i++){
        while(j>0 && s[i]!=s[j]) j=v[j-1];
        if(s[i]==s[j]) j++;
        v[i]=j;
    }
    return v;
}
int main(){
    fast();
    string s, t; cin>>s>>t;
    string temp=t+'0'+s;
    auto lps=f(temp);
    int ans=0;
    for(auto i:lps) ans+=(i==t.size());
    cout<<ans;
}*/

Compilation message

necklace.cpp: In function 'void f(std::string&, int*)':
necklace.cpp:9:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |     for(int i=1; i<s.size(); i++){
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 348 KB Output is correct
2 Correct 133 ms 536 KB Output is correct
3 Correct 187 ms 536 KB Output is correct
4 Correct 139 ms 760 KB Output is correct
5 Correct 91 ms 344 KB Output is correct
6 Correct 99 ms 552 KB Output is correct
7 Correct 122 ms 548 KB Output is correct
8 Correct 143 ms 540 KB Output is correct
9 Correct 147 ms 604 KB Output is correct