Submission #1005421

# Submission time Handle Problem Language Result Execution time Memory
1005421 2024-06-22T12:24:10 Z anango Necklace (Subtask 1-3) (BOI19_necklace1) C++17
45 / 85
1500 ms 141592 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

int INF = 1LL<<60;

string a;
string b;
int n,m;

vector<vector<int>> match;
vector<vector<int>> smallest;


vector<pair<int,int>> solve() {
    match=vector<vector<int>>(n,vector<int>(m,0));
    smallest=vector<vector<int>>(n+1,vector<int>(m+1,INF));
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int ct=0;
            for (int t=0; t<min(n-i, m-j); t++) {
                if (a[i+t]!=b[j+t]) {
                    break;
                }
                ct++;
            }
            match[i][j] = ct;
        }
    }
    for (int x=0; x<n; x++) {
        for (int t=0; t<m; t++) {
            for (int r=t; r<=t+match[x][t]; r++) {
                smallest[x][r] = min(smallest[x][r],t);
            }
        }
    }
    int ans = 0;
    pair<int,int> xpos,ypos;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int M = match[i][j];
            int x = i+M;
            int tau = smallest[x][j];
            //cout << i << " " << j << " " << M <<" " << x << " " << tau << endl;
            if (tau==INF) {
                continue;
            }
            int subans = j-tau+M;
            if (subans>ans) {
                xpos={i,i+subans-1};
                ypos={tau,j+M-1};
            }
            ans=max(ans,subans);
        }
    }
    //cout << a <<" " << b <<" " << ans <<" " << ans <<" " << xpos.first <<" " << xpos.second <<" " << ypos.first <<" " << ypos.second << endl;
    return {{ans,ans},xpos,ypos};

    
}

signed main() {
    //freopen("input.txt","r", stdin);
    //freopen("output.txt","w",stdout);
    cin >> a;
    cin >> b;
    n=a.size();
    m=b.size();
    vector<pair<int,int>> ans2;
    vector<int> ans(3);
    ans2=solve();
    ans[0] = ans2[0].first;
    ans[1] = ans2[1].first;
    ans[2] = ans2[2].first;
    reverse(b.begin(), b.end());
    ans2 = solve();
    if (ans2[0].first>ans[0]) {
        ans[0] = ans2[0].first;
        ans[1] = ans2[1].first;
        ans[2] = m-ans2[2].second-1;
    }
    reverse(a.begin(), a.end());
    ans2 = solve();
    if (ans2[0].first>ans[0]) {
        ans[0] = ans2[0].first;
        ans[1] = n-ans2[1].second-1;
        ans[2] = m-ans2[2].second-1;
    }
    reverse(b.begin(), b.end());
    ans2 = solve();
    if (ans2[0].first>ans[0]) {
        ans[0] = ans2[0].first;
        ans[1] = n-ans2[1].second-1;
        ans[2] = ans2[2].first;
    }
    cout << ans[0] << endl;
    cout << ans[1] <<" " << ans[2] << endl;


    //want to store for each x and j, the smallest t such that match[x][t]+t = j
    

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 121 ms 4232 KB Output is correct
7 Correct 28 ms 4188 KB Output is correct
8 Correct 13 ms 3512 KB Output is correct
9 Correct 10 ms 3932 KB Output is correct
10 Correct 5 ms 4188 KB Output is correct
11 Correct 5 ms 4008 KB Output is correct
12 Correct 15 ms 3928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 121 ms 4232 KB Output is correct
7 Correct 28 ms 4188 KB Output is correct
8 Correct 13 ms 3512 KB Output is correct
9 Correct 10 ms 3932 KB Output is correct
10 Correct 5 ms 4188 KB Output is correct
11 Correct 5 ms 4008 KB Output is correct
12 Correct 15 ms 3928 KB Output is correct
13 Execution timed out 1573 ms 141592 KB Time limit exceeded
14 Halted 0 ms 0 KB -