답안 #688552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
688552 2023-01-27T16:47:08 Z stanislavpolyn Necklace (Subtask 1-3) (BOI19_necklace1) C++17
25 / 85
1500 ms 5256 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()

using namespace std;

mt19937_64 rng(228);

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) {
    return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
    return a < b ? a = b, 1 : 0;
}

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int M = 3005 * 3005;

string a, b;

ve<int> ord;

int szTrie = 1;
int to[M][26];

bool ask(string& s, bool print = 0) {

    if (print) {
        fr (i, 0, sz(b) - sz(s)) {
            if (b.substr(i, sz(s)) == s) {
                if (print) {
                    cout << i << "\n";
                }
                return 1;
            }
        }
    } else {
        int v = 1;
        fr (i, 0, sz(s) - 1) {
            if (!to[v][s[i] - 'a']) {
                return 0;
            }
            v = to[v][s[i] - 'a'];
        }
        return 1;
    }
    return 0;
}

bool rev;

bool possible(int len, bool print = 0) {
    fr (i, 0, sz(a) - len) {
        fr (shift, 0, len - 1){
            string s = a.substr(i + shift, len - shift) + a.substr(i, shift);

            if (ask(s)) {
                if (print) {
                    if (!rev) {
                        cout << i << " ";
                    } else {
                        cout << sz(a) - 1 - (i + len - 1) << " ";
                    }
                    ask(s, 1);
                }

                return 1;
            }
        }
    }
    return 0;
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    cin >> a >> b;

    fr (i, 0, sz(b) - 1) {
        int v = 1;
        fr (j, i, sz(b) - 1) {
            if (!to[v][b[j] - 'a']) {
                to[v][b[j] - 'a'] = ++szTrie;
            }
            v = to[v][b[j] - 'a'];
        }
    }

//    {
//        fr (i, 0, sz(b) - 1) {
//            ord.pb(i);
//        }
//        sort(all(ord), [](int i, int j) {
//            while (i < sz(b) && j < sz(b) && b[i] == b[j]) {
//                i++;
//                j++;
//            }
//            if (i == sz(b) && j < sz(b)) {
//                return 1;
//            }
//            if (i < sz(b) && j < sz(b) && b[i] < b[j]) {
//                return 1;
//            }
//            return 0;
//        });
//    }


//
//    int l = 0;
//    int r = min(sz(a), sz(b));
//    while (l < r) {
//        int mid = l + ((r - l + 1) >> 1);
//
//        if (possible(mid)) {
//            l = mid;
//        } else {
//            r = mid - 1;
//        }
//    }

    int ans = -1;
    rf (i, sz(a), 1) {
        if (possible(i)) {
            ans = i;
            break;
        }
    }

    rev = 1;
    reverse(all(a));
    rf (i, sz(a), ans + 1) {
        if (possible(i)) {
            cout << i << "\n";
            possible(i, 1);
            return 0;
        }
    }

    rev = 0;
    reverse(all(a));

    cout << ans << "\n";
    possible(ans, 1);

//    cout << l << "\n";
//
//    possible(l, 1);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 6 ms 480 KB Output is correct
3 Correct 20 ms 724 KB Output is correct
4 Correct 54 ms 612 KB Output is correct
5 Correct 39 ms 804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 6 ms 480 KB Output is correct
3 Correct 20 ms 724 KB Output is correct
4 Correct 54 ms 612 KB Output is correct
5 Correct 39 ms 804 KB Output is correct
6 Correct 1336 ms 2772 KB Output is correct
7 Correct 1323 ms 4048 KB Output is correct
8 Execution timed out 1559 ms 5256 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 6 ms 480 KB Output is correct
3 Correct 20 ms 724 KB Output is correct
4 Correct 54 ms 612 KB Output is correct
5 Correct 39 ms 804 KB Output is correct
6 Correct 1336 ms 2772 KB Output is correct
7 Correct 1323 ms 4048 KB Output is correct
8 Execution timed out 1559 ms 5256 KB Time limit exceeded
9 Halted 0 ms 0 KB -