답안 #725050

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725050 2023-04-16T14:43:15 Z Rafi22 Necklace (Subtask 1-3) (BOI19_necklace1) C++14
85 / 85
515 ms 548 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 364 KB Output is correct
7 Correct 9 ms 340 KB Output is correct
8 Correct 10 ms 320 KB Output is correct
9 Correct 10 ms 340 KB Output is correct
10 Correct 9 ms 340 KB Output is correct
11 Correct 8 ms 360 KB Output is correct
12 Correct 9 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 364 KB Output is correct
7 Correct 9 ms 340 KB Output is correct
8 Correct 10 ms 320 KB Output is correct
9 Correct 10 ms 340 KB Output is correct
10 Correct 9 ms 340 KB Output is correct
11 Correct 8 ms 360 KB Output is correct
12 Correct 9 ms 316 KB Output is correct
13 Correct 486 ms 420 KB Output is correct
14 Correct 466 ms 416 KB Output is correct
15 Correct 515 ms 468 KB Output is correct
16 Correct 494 ms 548 KB Output is correct
17 Correct 411 ms 408 KB Output is correct
18 Correct 431 ms 460 KB Output is correct
19 Correct 449 ms 464 KB Output is correct
20 Correct 464 ms 424 KB Output is correct
21 Correct 471 ms 420 KB Output is correct