답안 #584315

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
584315 2022-06-27T08:05:44 Z talant117408 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
85 / 85
405 ms 71044 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

string s, t;

char get(int i, int q) {
    if (i < sz(s) - q) return s[q + i];
    else if (i > sz(s)) return t[i - sz(s) - 1];
    return '#';
}

vector <int> z_function(int q) {
    int saizu = sz(s) + sz(t) + 1;
    vector <int> z(saizu);
    for (int i = 1, l = 0, r = 0; i < saizu; i++) {
        if (i <= r)
            z[i] = min (r - i + 1, z[i - l]);
        while (i + z[i] < saizu && get(z[i], q) == get(i + z[i], q))
            z[i]++;
        if (i + z[i] - 1 > r)
            l = i, r = i + z[i] - 1;
    }
    return z;
}

void solve() {
    cin >> s >> t;
    int ans = 0, inds = -1, indt = -1;
    vector <vector <int>> Z(sz(s), vector <int> (sz(t))), reversed_Z(sz(s), vector <int> (sz(t)));
    
    string q = s;
    for (int i = 0; i < sz(s); i++) {
        string tmp = q + '$' + t;
        auto res = z_function(i);
        for (int j = sz(s) + 1; j < sz(s) + sz(t) + 1; j++) 
            Z[i][j - sz(s) - 1] = res[j];
        rotate(q.begin(), q.begin() + 1, q.end());
        q.back() = '#';
    }
    for (int i = 0; i < sz(s); i++) {
        for (int j = 0; j < sz(t); j++) {
            auto r = (j + Z[i][j]) % sz(t);
            if (j + Z[i][j] < sz(t))
                reversed_Z[i][r] = max(reversed_Z[i][r], Z[i][j]);
        }
    }
    for (int i = 0; i < sz(s); i++) {
        for (int j = 0; j < sz(t); j++) {
            if (Z[i][j] == 0) continue;
            int nxt = (i + Z[i][j]) % sz(s);
            if (Z[i][j] + reversed_Z[nxt][j] > ans && Z[i][j] + reversed_Z[nxt][j] <= sz(s)) {
                ans = Z[i][j] + reversed_Z[nxt][j];
                inds = i; indt = (j - reversed_Z[nxt][j] + sz(t)) % sz(t);
                //~ indt = sz(t) - (indt + ans - 1) - 1;
            }
        }
    }
    
    q = s;
    reverse(all(t));
    for (int i = 0; i < sz(s); i++) {
        fill(all(reversed_Z[i]), 0);
        string tmp = q + '$' + t;
        auto res = z_function(i);
        for (int j = sz(s) + 1; j < sz(s) + sz(t) + 1; j++) 
            Z[i][j - sz(s) - 1] = res[j];
        rotate(q.begin(), q.begin() + 1, q.end());
        q.back() = '#';
    }
    for (int i = 0; i < sz(s); i++) {
        for (int j = 0; j < sz(t); j++) {
            auto r = (j + Z[i][j]) % sz(t);
            if (j + Z[i][j] < sz(t))
                reversed_Z[i][r] = max(reversed_Z[i][r], Z[i][j]);
        }
    }
    for (int i = 0; i < sz(s); i++) {
        for (int j = 0; j < sz(t); j++) {
            if (Z[i][j] == 0) continue;
            int nxt = (i + Z[i][j]) % sz(s);
            if (Z[i][j] + reversed_Z[nxt][j] > ans && Z[i][j] + reversed_Z[nxt][j] <= sz(s)) {
                ans = Z[i][j] + reversed_Z[nxt][j];
                inds = i; indt = (j - reversed_Z[nxt][j] + sz(t)) % sz(t);
                indt = sz(t) - (indt + ans - 1) - 1;
            }
        }
    }
    
    cout << ans << endl;
    cout << inds << ' ' << indt << endl;
}

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1492 KB Output is correct
7 Correct 8 ms 1608 KB Output is correct
8 Correct 8 ms 1364 KB Output is correct
9 Correct 8 ms 1492 KB Output is correct
10 Correct 7 ms 1468 KB Output is correct
11 Correct 6 ms 1492 KB Output is correct
12 Correct 7 ms 1512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 1492 KB Output is correct
7 Correct 8 ms 1608 KB Output is correct
8 Correct 8 ms 1364 KB Output is correct
9 Correct 8 ms 1492 KB Output is correct
10 Correct 7 ms 1468 KB Output is correct
11 Correct 6 ms 1492 KB Output is correct
12 Correct 7 ms 1512 KB Output is correct
13 Correct 405 ms 70996 KB Output is correct
14 Correct 353 ms 71044 KB Output is correct
15 Correct 395 ms 66896 KB Output is correct
16 Correct 386 ms 69872 KB Output is correct
17 Correct 303 ms 68480 KB Output is correct
18 Correct 315 ms 70484 KB Output is correct
19 Correct 335 ms 70272 KB Output is correct
20 Correct 353 ms 68440 KB Output is correct
21 Correct 376 ms 69180 KB Output is correct