Submission #725051

# Submission time Handle Problem Language Result Execution time Memory
725051 2023-04-16T14:43:55 Z Rafi22 Necklace (Subtask 4) (BOI19_necklace4) C++14
15 / 15
540 ms 468 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

const int N=3007;

int n,m;
vector<int> prefix_function(string s) {
    int n = (int)s.length();
    vector<int> pi(n);
    for (int i = 1; i < n; i++) {
        int j = pi[i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
    return pi;
}

vector<int>kmp(string s)
{
    int l=sz(s);
    vector<int>p(l,0);
    for(int i=1;i<l;i++)
    {
        int j=p[i-1];
        while(j>0&&s[i]!=s[j]) j=p[j-1];
        if(s[i]==s[j]) j++;
        p[i]=j;
    }
    return p;
}

int ans,x,y;

void solve(string a,string b,bool is)
{
    for(int i=0;i<=n;i++)
    {
        string s="",t="";
        for(int j=i;j<n;j++) s+=a[j];
        for(int j=0;j<m;j++) t+=b[j];
        vector<int>p1=kmp(s+'#'+t);
        s="";
        for(int j=0;j<i;j++) s+=a[j];
        reverse(all(s));
        reverse(all(t));
        vector<int>p2=kmp(s+'#'+t);
        for(int j=0;j<=m;j++)
        {
            int c1=0,c2=0;
            if(i!=n&&j>0) c1=p1[n-i+j];
            if(i!=0&&j!=m-1) c2=p2[i+m-j];
            if(c1+c2>ans)
            {
                ans=c1+c2;
                x=i-c2;
                if(is) y=m-j-c2;
                else y=j-c1;
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string a,b;
    cin>>a>>b;
    n=sz(a),m=sz(b);
    solve(a,b,0);
    reverse(all(b));
    solve(a,b,1);
    cout<<ans<<endl<<x<<" "<<y<<endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 497 ms 468 KB Output is correct
2 Correct 480 ms 420 KB Output is correct
3 Correct 540 ms 420 KB Output is correct
4 Correct 426 ms 412 KB Output is correct
5 Correct 404 ms 412 KB Output is correct
6 Correct 439 ms 416 KB Output is correct
7 Correct 464 ms 416 KB Output is correct
8 Correct 470 ms 416 KB Output is correct
9 Correct 504 ms 420 KB Output is correct