QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#393233#6366. MessagewsyearML 2ms17044kbC++144.5kb2024-04-18 12:58:372024-04-18 12:58:37

Judging History

你现在查看的是最新测评结果

  • [2024-04-18 12:58:37]
  • 评测
  • 测评结果:ML
  • 用时:2ms
  • 内存:17044kb
  • [2024-04-18 12:58:37]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

template <int P>
class mod_int {
  using Z = mod_int;

private:
  static int mo(int x) { return x < 0 ? x + P : x; }

public:
  int x;
  int val() const { return x; }
  mod_int() : x(0) {}
  template <class T>
  mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
  bool operator==(const Z &rhs) const { return x == rhs.x; }
  bool operator!=(const Z &rhs) const { return x != rhs.x; }
  Z operator-() const { return Z(x ? P - x : 0); }
  Z pow(long long k) const {
    Z res = 1, t = *this;
    while (k) {
      if (k & 1) res *= t;
      if (k >>= 1) t *= t;
    }
    return res;
  }
  Z &operator++() {
    x < P - 1 ? ++x : x = 0;
    return *this;
  }
  Z &operator--() {
    x ? --x : x = P - 1;
    return *this;
  }
  Z operator++(int) {
    Z ret = x;
    x < P - 1 ? ++x : x = 0;
    return ret;
  }
  Z operator--(int) {
    Z ret = x;
    x ? --x : x = P - 1;
    return ret;
  }
  Z inv() const { return pow(P - 2); }
  Z &operator+=(const Z &rhs) {
    (x += rhs.x) >= P && (x -= P);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    (x -= rhs.x) < 0 && (x += P);
    return *this;
  }
  Z &operator*=(const Z &rhs) {
    x = 1ULL * x * rhs.x % P;
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                 \
  friend T operator o(const Z &lhs, const Z &rhs) {\
    Z res = lhs;                                   \
    return res o## = rhs;                          \
  }
  setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 1011451423;
using Z = mod_int<P>;

const int maxn = 200010;
const ll inf = 1e18;
const Z base = 13331;

int n, m, a[maxn], vis[maxn][26], pipe[maxn][60], to[maxn][60];
int tot, L[maxn], R[maxn], b[maxn], len, lx[maxn], rx[maxn];
char s[maxn], t[maxn];
ll f[maxn][60], sum, psum[maxn][60];
vector<int> pos;
Z pw[maxn], pre[maxn];

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> (s + 1) >> (t + 1);
  n = strlen(s + 1), m = strlen(t + 1);
  if (m > n) return cout << "-1\n", 0;
  pw[0] = 1;
  rep (i, 1, n) pw[i] = pw[i - 1] * base;
  rep (i, 1, n) cin >> a[i], sum += a[i];
  rep (i, 0, 25) {
    int l = n + 1, r = 0;
    rep (j, 1, m) if (t[j] - 'a' == i) chkmn(l, j), chkmx(r, j);
    if (!r) continue;
    pos.emplace_back(l), pos.emplace_back(r + 1);
  }
  pos.emplace_back(1), pos.emplace_back(m + 1);
  sort(ALL(pos)), pos.erase(unique(ALL(pos)), pos.end());
  rep (i, 0, SZ(pos) - 2) L[++tot] = pos[i], R[tot] = pos[i + 1] - 1;
  rep (i, 0, 25) {
    int l = n + 1, r = 0;
    rep (j, 1, m) if (t[j] - 'a' == i) chkmn(l, j), chkmx(r, j);
    if (!r) continue;
    l = lower_bound(L + 1, L + tot + 1, l) - L;
    r = lower_bound(L + 1, L + tot + 1, r + 1) - L - 1;
    lx[i] = l, rx[i] = r;
    rep (j, l, r) vis[j][i] = 1;
  }
  rep (k, 1, tot) {
    rep (i, 1, n) psum[i][k] = psum[i - 1][k] + (!vis[k][s[i] - 'a'] ? a[i] : 0);
  }
  rep (k, 1, tot) {
    len = 0;
    rep (i, 1, n) if (vis[k][s[i] - 'a']) b[++len] = i;
    rep (i, 1, len) pre[i] = pre[i - 1] * base + (s[b[i]] - 'a' + 1);
    Z ths = 0;
    rep (i, L[k], R[k]) ths = ths * base + (t[i] - 'a' + 1);
    rep (i, 1, len - (R[k] - L[k] + 1) + 1) {
      if ((pre[i + (R[k] - L[k] + 1) - 1] - pre[i - 1] * pw[R[k] - L[k] + 1]) == ths) {
        pipe[b[i]][k] = 1, to[b[i]][k] = b[i + (R[k] - L[k] + 1) - 1];
      }
    }
  }
  rep (i, 0, n) rep (j, 0, tot) f[i][j] = inf;
  f[0][0] = 0;
  rep (i, 0, n - 1) rep (j, 0, tot) {
    if (j == tot) {
      chkmn(f[i + 1][j], f[i][j] + a[i + 1]);
    } else {
      if (j < lx[s[i + 1] - 'a'] || j >= rx[s[i + 1] - 'a']) chkmn(f[i + 1][j], f[i][j] + a[i + 1]);
      if (pipe[i + 1][j + 1]) {
        chkmn(f[to[i + 1][j + 1]][j + 1], f[i][j] + psum[to[i + 1][j + 1]][j + 1] - psum[i][j + 1]);
      }
    }
  }
  if (f[n][tot] == inf) cout << "You better start from scratch man...\n";
  else cout << f[n][tot] << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 16076kb

input:

ababccb
abc
7 2 2 4 3 2 1

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 2ms
memory: 17044kb

input:

babab
baab
2 1 3 2 4

output:

You better start from scratch man...

result:

ok single line: 'You better start from scratch man...'

Test #3:

score: -100
Memory Limit Exceeded

input:

bbaaaabbbbbabaababbaaabbbabaaaababaababaabababbbbabbbbababaabaaabbabaaaabbbabbaababababbabbbabbaababaaaaaabbabbbababbaabbbabbbabbbabaababaaaaaabaaababbbbbbaabaabaaaaaaabbbaaabaabbbababbbbbabbababaababaabbababbaababbbbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbab...

output:

You better start from scratch man...

result: