답안 #648732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648732 2022-10-07T22:47:56 Z Lobo Necklace (Subtask 1-3) (BOI19_necklace1) C++17
25 / 85
1500 ms 6728 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 3030;
const int mod1 = 1e9+7;
const int mod2 = 1e9+9;

string a,b;
int n, m, pw1[maxn], pw2[maxn];
pair<int,int> lra[maxn][maxn], lrb[maxn][maxn];

void solve() {
    cin >> a >> b;
    n = a.size();
    m = b.size();

    pw1[0] = 1;
    pw1[1] = 19079;
    pw2[0] = 1;
    pw2[1] = 1e4+7;
    for(int i = 2; i <= max(n,m); i++) {
        pw1[i] = pw1[i-1]*pw1[1]%mod1;
        pw2[i] = pw2[i-1]*pw2[1]%mod2;
    }

    map<pair<int,int>,int> st;
    for(int i = 0; i < n; i++) {
        int cur1 = 0;
        int cur2 = 0;
        for(int j = i; j < n; j++) {
            cur1 = (cur1+a[j]*pw1[j-i])%mod1;
            cur2 = (cur2+a[j]*pw2[j-i])%mod2;
            lra[i][j] = mp(cur1,cur2);
            st[lra[i][j]] = i;
        }
    }

    for(int i = 0; i < m; i++) {
        int cur1 = 0;
        int cur2 = 0;
        for(int j = i; j < m; j++) {
            cur1 = (cur1+b[j]*pw1[j-i])%mod1;
            cur2 = (cur2+b[j]*pw2[j-i])%mod2;
            lrb[i][j] = mp(cur1,cur2);
        }
    }

    int ans = 0;
    int ansa,ansb;
    for(int i = 0; i < m; i++) {
        for(int j = i; j < m; j++) {
            for(int k = j; k < m; k++) {
                //(j,k) (i,j-1)
                int val1 = lrb[j][k].fr+ lrb[i][j-1].fr*pw1[k-j+1];
                val1%= mod1;
                int val2 = lrb[j][k].sc+ lrb[i][j-1].sc*pw2[k-j+1];
                val2%= mod2;
                if(st.count(mp(val1,val2)) && k-i+1 > ans) {
                    ans = k-i+1;
                    ansa = st[mp(val1,val2)];
                    ansb = i;
                }
            }
        }
    }

    reverse(all(b));
    for(int i = 0; i < m; i++) {
        int cur1 = 0;
        int cur2 = 0;
        for(int j = i; j < m; j++) {
            cur1 = (cur1+b[j]*pw1[j-i])%mod1;
            cur2 = (cur2+b[j]*pw2[j-i])%mod2;
            lrb[i][j] = mp(cur1,cur2);
        }
    }

    for(int i = 0; i < m; i++) {
        for(int j = i; j < m; j++) {
            for(int k = j; k < m; k++) {
                //(j,k) (i,j-1)
                int val1 = lrb[j][k].fr+ lrb[i][j-1].fr*pw1[k-j+1];
                val1%= mod1;
                int val2 = lrb[j][k].sc+ lrb[i][j-1].sc*pw2[k-j+1];
                val2%= mod2;
                if(st.count(mp(val1,val2)) && k-i+1 > ans) {
                    ans = k-i+1;
                    ansa = st[mp(val1,val2)];
                    ansb = m-k-1;
                }
            }
        }
    }

    cout << ans << endl;
    cout << ansa << " " << ansb << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1364 KB Output is correct
2 Correct 28 ms 1364 KB Output is correct
3 Correct 29 ms 1336 KB Output is correct
4 Correct 22 ms 1464 KB Output is correct
5 Correct 35 ms 1564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1364 KB Output is correct
2 Correct 28 ms 1364 KB Output is correct
3 Correct 29 ms 1336 KB Output is correct
4 Correct 22 ms 1464 KB Output is correct
5 Correct 35 ms 1564 KB Output is correct
6 Execution timed out 1591 ms 6728 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1364 KB Output is correct
2 Correct 28 ms 1364 KB Output is correct
3 Correct 29 ms 1336 KB Output is correct
4 Correct 22 ms 1464 KB Output is correct
5 Correct 35 ms 1564 KB Output is correct
6 Execution timed out 1591 ms 6728 KB Time limit exceeded
7 Halted 0 ms 0 KB -