QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#433086#6635. Strange Keyboarducup-team3215TL 1ms3660kbC++202.1kb2024-06-08 00:18:132024-06-08 00:18:13

Judging History

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

  • [2024-06-08 00:18:13]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3660kb
  • [2024-06-08 00:18:13]
  • 提交

answer

#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

constexpr int A = 26, V = 26e6 + 1, K = 5e3;

int nx[V], cost[V], used, ks[K];
int64_t ans[K + 1];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  for (int tc = (cin >> tc, tc); tc--; ) {
    fill_n(cost, used, 0);
    fill_n(nx, used, 0);
    used = 1;
    fill(ks + 1, end(ks), 1e9);
    fill(ans + 1, end(ans), 1e18);
    int n, k; cin >> n >> k;
    vector<string> v(n);
    for (auto& s: v) cin >> s;
    {
      vector<int> big;
      vector<char> small(100, 0);
      for (auto& s: v) if (s.size() >= small.size()) big.push_back(s.size()); else small[s.size()] = 1;
      sort(big.begin(), big.end());
      big.erase(unique(big.begin(), big.end()), big.end());
      vector<int> q{0}, nq;
      int dist = 1;
      while (q.size()) {
        ++dist;
        for (auto& t: q) {
          if (t >= k) {
            if (t - k >= k) nq.push_back(t - k);
            else if (ks[t - k] == 1e9) nq.push_back(t - k), ks[t - k] = dist;
            continue;
          }
          for (int x = 0; x < small.size(); ++x) if (small[x]) {
            if (t + x >= k) nq.push_back(t + x);
            else if (ks[t + x] == 1e9) nq.push_back(t + x), ks[t + x] = dist;
          }
          for (auto x: big) nq.push_back(t + x);
        }
        swap(q = {}, nq);
      }
      reverse(ks + 1, ks + k);
    }
    for (auto& s: v) {
      int v = 0;
      for (int rem = s.size(); auto& c: s) {
        if (!nx[v]) nx[v] = used, used += A;
        v = nx[v] + c - 'a';
        --rem;
        if (ks[rem % k] != 1e9) cost[v] = min(cost[v] ?: 1 << 30, ks[rem % k] + rem / k + 1);
      }
    }
    string s; cin >> s;
    for (int i = 0; i < s.size(); ++i) {
      int v = 0;
      for (int j = i; j < s.size(); ++j) {
        if (!nx[v]) break;
        v = nx[v] + s[j] - 'a';
        if (!cost[v]) break;
        ans[j + 1] = min(ans[j + 1], ans[i] + cost[v]);
      }
    }
    cout << (ans[s.size()] < 1e18? ans[s.size()]: -1) << '\n';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3660kb

input:

2
2 3
defgh
abc
abcde
1 1
a
b

output:

3
-1

result:

ok 2 number(s): "3 -1"

Test #2:

score: -100
Time Limit Exceeded

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:


result: