QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#113906#6635. Strange KeyboardITMO_pengzoo#TL 395ms4752kbC++202.8kb2023-06-20 00:40:302023-06-20 00:40:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-20 00:40:33]
  • 评测
  • 测评结果:TL
  • 用时:395ms
  • 内存:4752kb
  • [2023-06-20 00:40:30]
  • 提交

answer

//  Nikita Golikov, 2023

#include <bits/stdc++.h>

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#ifdef GOLIKOV
  #include "/Users/golikovnik/contests/debug.h"
#else
  #define debug(...) ;
#endif

template <class A, class B>
bool smin(A& x, B&& y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template <class A, class B>
bool smax(A& x, B&& y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solveTest() {
  int n, k;
  cin >> n >> k;
  vector<string> a(n);
  for (auto& s : a) {
    cin >> s;
  }
  string t; cin >> t;
  ll const INF = ll(1e18);
  vector<ll> forRem(k, INF);
  for (auto& s : a) {
    smin(forRem[int(s.size()) % k], int(s.size()));
  }
  vector<int> vals;
  for (int i = 0; i < k; ++i) {
    if (forRem[i] != INF) {
      vals.push_back(i);
    }
  }
  vector<ll> dp(k, INF);
  dp[0] = 0;
  vector<bool> used(k);
  for (int it = 0; it < k; ++it) {
    int p = -1;
    for (int r = 0; r < k; ++r) {
      if (!used[r] && (p < 0 || dp[r] < dp[p])) {
        p = r;
      }
    }
    assert(p >= 0);
    used[p] = true;
    for (int t : vals) {
      smin(dp[(t + p) % k], dp[p] + forRem[t] + k);
    }
  }
  struct node {
    int nxt[26]{};
    ll bestScore = INF;
  };
  vector<node> tree;
  tree.emplace_back();
  for (auto& s : a) {
    int cur = 0;
    int len = 0;
    for (char ch : s) {
      ch -= 'a';
      if (!tree[cur].nxt[ch]) {
        tree[cur].nxt[ch] = int(tree.size());
        tree.emplace_back();
      }
      cur = tree[cur].nxt[ch];
      len++;
      int left = int(s.size()) - len;
      auto val = dp[(k - left % k) % k];
      if (val != INF) {
        assert((val + left) % k == 0);
        smin(tree[cur].bestScore, (val + left) / k);
      }
    }
  }
  int m = int(t.size());
  vector<ll> dpAns(m + 1, INF);
  dpAns[0] = 0;

  for (int p = 0; p < m; ++p) {
    int cur = 0;
    for (int q = p; q < m; ++q) {
      int here = t[q] - 'a';
      if (!tree[cur].nxt[here]) {
        break;
      }
      cur = tree[cur].nxt[here];
      smin(dpAns[q + 1], dpAns[p] + 1 + tree[cur].bestScore);
    }
  }

  cout << (dpAns[m] == INF ? -1 : dpAns[m]) << '\n';
}

int main() {
#ifdef GOLIKOV
  assert(freopen("in", "rt", stdin));
  auto _clock_start = chrono::high_resolution_clock::now();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int tests_;
  cin >> tests_;
  for (int tt_ = 1; tt_ <= tests_; ++tt_) {
    solveTest();
  }

#ifdef GOLIKOV
  cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
      chrono::high_resolution_clock::now()
          - _clock_start).count() << "ms." << endl;
#endif
  return 0;
}

詳細信息

Test #1:

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

input:

2
2 3
defgh
abc
abcde
1 1
a
b

output:

3
-1

result:

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

Test #2:

score: 0
Accepted
time: 53ms
memory: 4752kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 395ms
memory: 3932kb

input:

10
446 4905
afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica
afigcjhcgagabbiccehjcjajigghgbj...

output:

3
2
2
11
6
5
1
1
1
1

result:

ok 10 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

100
140 4879
baabaababbababbaabababaaababbbabbbbbbabbababbbabbbbabbbbbbaabbbbbbbbabaabbbaabaabbbaabbabaabaabbbabbbababbbaabbabaaaaabbaaabbbabb
baa
baabaababbababbaabababaaababbbabbbbbbabbab
baabaababbababbaabababaaabab
baabaababbababbaabababaaababbbabbb
baabaababbababbaabababaaababbbabbbbbbabbababbb...

output:


result: