615C - Running Track - CodeForces Solution


dp greedy strings trees *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
// AC2#
using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;

#define fi first
#define se second
#define pb push_back
#define SZ(x) int((x).size())
#define ALL(x) begin(x), end(x)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ENDL '\n'

void buildPi(string& p, vi& pi) {
    pi = vi(SZ(p));
    int k = -2;
    FOR(i, 0, SZ(p)) {
        while (k >= -1 && p[k + 1] != p[i])
            k = (k == -1) ? -2 : pi[k];
        pi[i] = ++k;
    }
}

pi kmp(string& t, string& p) {
    if (p == "")
        return {0, 0};

    vi pi;
    buildPi(p, pi);
    int k = -1;
    FOR(i, 0, SZ(t)) {
        while (k >= -1 && p[k + 1] != t[i])
            k = (k == -1) ? -2 : pi[k];
        k++;
        if (k == SZ(p) - 1) {
            return {i - k, i - k + SZ(p) - 1};
        }
    }
    return {-1, -1};
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    string s, t;
    cin >> s >> t;
    string s_i = s;
    reverse(ALL(s_i));

    vector<bool> in_s(SZ(s), 0);
    FOR(i, 0, SZ(s)) {
        in_s[s[i] - 'a'] = 1;
    }

    FOR(i, 0, SZ(t)) {
        if (!in_s[t[i] - 'a']) {
            cout << -1 << ENDL;
            return 0;
        }
    }

    t += '#';

    int ind = 0;
    vector<pi> coatings;
    while (ind < SZ(t) - 1) {
        string aux = "";
        int last_l, last_r;
        while (1) {
            auto [l1, r1] = kmp(s, aux);
            auto [l2, r2] = kmp(s_i, aux);

            if (l1 != -1) {
                last_l = l1;
                last_r = r1;
            } else if (l2 != -1) {
                last_l = SZ(s) - l2 - 1;
                last_r = SZ(s) - r2 - 1;
            }

            if (l1 == -1 && l2 == -1) {
                ind--;
                coatings.pb({last_l + 1, last_r + 1});
                break;
            }
            aux += t[ind++];
        }
    }

    cout << SZ(coatings) << ENDL;
    for (auto& [u, v] : coatings)
        cout << u << ' ' << v << ENDL;
    return 0;
}


Comments

Submit
0 Comments
More Questions

112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie