Submission #832302

# Submission time Handle Problem Language Result Execution time Memory
832302 2023-08-21T08:41:03 Z yeediot Necklace (Subtask 4) (BOI19_necklace4) C++14
15 / 15
155 ms 488 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
vector<int> kmp(string s){
    vector<int>res(sz(s));
    for(int i=1,j=0;i<sz(s);i++){
        while(j and s[i]!=s[j])j=res[j-1];
        if(s[i]==s[j])j++;
        res[i]=j;
    }
    return res;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    string s,t;
    cin>>s>>t;
    int n=sz(s);
    int m=sz(t);
    vi ans(3,0);
    for(int i=0;i<n;i++){
        string s1=s.substr(0,i),s2=s.substr(i,sz(s)-i);
        reverse(all(s1));
        string t1=t,t2=t;
        reverse(all(t2));
        vector<int>p1=kmp(s1+"#"+t1);
        vector<int>p2=kmp(s2+"#"+t2);
        for(int j=0;j<m;j++){
            int x=p1[i+j+1]+p2[n-i+m-j-1];
            if(x>=ans[0]){
                ans[0]=x;
                ans[1]=i-p1[i+j+1];
                ans[2]=j+1-p1[i+j+1];
                //cout<<i<<' ';
            }
        }
    }
    reverse(all(t));
    for(int i=0;i<n;i++){
        string s1=s.substr(0,i),s2=s.substr(i,sz(s)-i);
        reverse(all(s1));
        string t1=t,t2=t;
        reverse(all(t2));
        vector<int>p1=kmp(s1+'#'+t1);
        vector<int>p2=kmp(s2+'#'+t2);
        for(int j=0;j<m;j++){
            int x=p1[i+j+1]+p2[n-i+m-j-1];
            if(x>=ans[0]){
                ans[0]=x;
                ans[1]=i-p1[i+j+1];
                ans[2]=m-j-1-p2[n-i+m-j-1];
            }
        }
    }
    cout<<ans[0]<<'\n'<<ans[1]<<' '<<ans[2]<<'\n';
}
 /*
 input:
 
 */

# Verdict Execution time Memory Grader output
1 Correct 144 ms 468 KB Output is correct
2 Correct 117 ms 488 KB Output is correct
3 Correct 155 ms 484 KB Output is correct
4 Correct 121 ms 476 KB Output is correct
5 Correct 83 ms 484 KB Output is correct
6 Correct 86 ms 472 KB Output is correct
7 Correct 105 ms 480 KB Output is correct
8 Correct 119 ms 476 KB Output is correct
9 Correct 125 ms 440 KB Output is correct