Submission #6997670


Source Code Expand

Copy
#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x) x
#define rep(...) MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c) c.begin(),c.end()
#define mp make_pair
#define write(x) cout<<(x)<<'\n'
using namespace std; typedef long long ll; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
template<class T1, class T2>bool chmax(T1& a, const T2& b) { return a < b ? a = b, 1 : 0; }
template<class T1, class T2>bool chmin(T1& a, const T2& b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

string S, T;
vector<int> Schar_cnt(26);
vv<int> next_cindex;

int main() {
    cin >> S >> T;

    rep(i, S.size()) {
        Schar_cnt[S[i] - 'a']++;
    }

    bool ok = true;
    rep(i, T.size()) {
        if (Schar_cnt[T[i] - 'a'] == 0) {
            ok = false;
        }
    }

    if (!ok) {
        write(-1);
        return 0;
    }

    next_cindex = vvec(S.size(), 26, -1);
    vector<int> ci(26, -1);
    for (int i = S.size() - 1; i >= 0; --i) {
        ci[S[i] - 'a'] = i;

        for (int c = 0; c < 26; ++c) {
            next_cindex[i][c] = ci[c];
        }
    }

    ll ans = 0;
    int cur = 0;
    for (int i = 0; i < T.size(); ++i) {
        if (next_cindex[cur][T[i] - 'a'] >= 0) {
            ans += next_cindex[cur][T[i] - 'a'] - cur + 1;
            cur = next_cindex[cur][T[i] - 'a'] + 1;
        }
        else {
            ans += S.size() - cur;
            cur = 0;
            ans += next_cindex[cur][T[i] - 'a'] - cur + 1;
            cur = next_cindex[cur][T[i] - 'a'] + 1;
        }

        if (cur == S.size()) {
            cur = 0;
        }
    }

    write(ans);
}

Submission Info

Submission Time
Task E - Strings of Impurity
User myun
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2069 Byte
Status AC
Exec Time 19 ms
Memory 13776 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 45
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 1 ms 256 KB
b09 AC 1 ms 256 KB
b10 AC 16 ms 13776 KB
b11 AC 15 ms 13776 KB
b12 AC 15 ms 13776 KB
b13 AC 15 ms 13776 KB
b14 AC 16 ms 13776 KB
b15 AC 15 ms 13776 KB
b16 AC 2 ms 592 KB
b17 AC 19 ms 13776 KB
b18 AC 19 ms 13776 KB
b19 AC 2 ms 592 KB
b20 AC 15 ms 13776 KB
b21 AC 15 ms 13776 KB
b22 AC 15 ms 13776 KB
b23 AC 15 ms 13776 KB
b24 AC 15 ms 13776 KB
b25 AC 18 ms 13776 KB
b26 AC 17 ms 13776 KB
b27 AC 17 ms 13776 KB
b28 AC 16 ms 13776 KB
b29 AC 15 ms 13776 KB
b30 AC 16 ms 13776 KB
b31 AC 2 ms 592 KB
b32 AC 2 ms 512 KB
b33 AC 2 ms 512 KB
b34 AC 13 ms 13648 KB
b35 AC 2 ms 512 KB
b36 AC 15 ms 13776 KB
b37 AC 15 ms 13776 KB
b38 AC 15 ms 13776 KB
b39 AC 14 ms 13776 KB
b40 AC 16 ms 13776 KB
b41 AC 14 ms 13776 KB
b42 AC 2 ms 592 KB
b43 AC 2 ms 592 KB
b44 AC 15 ms 13776 KB
b45 AC 15 ms 13776 KB