QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123603 | #6635. Strange Keyboard | wnmrmr | WA | 84ms | 150944kb | C++20 | 2.7kb | 2023-07-12 23:57:53 | 2023-07-12 23:57:55 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
#define int long long
using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e6 + 5;
const int MOD = 1e9+7; //998244353;
const int INF = 0x3f3f3f3f;
const ll INF64 = ll(4e18) + 5;
const int M = 2e6+100;
int tree[M][26];
int best[M];
int li = 1;
int custo[MAXN];
void add(string s){
int at = 0;
int foi = s.size();
for(auto c : s){
int id = c-'a';
foi--;
if(tree[at][id] == 0){
tree[at][id] = li;
best[li] = INF;
assert(best[li] > 0);
li++;
}
at = tree[at][id];
}
best[at] = min(best[at],custo[foi]);
}
void solve(){
int n,k;
cin >> n >> k;
for(int i = 0; i < li; i++){
for(int j = 0; j < 26; j++) tree[i][j] = 0;
best[i] = INF;
}
li=1;
vector<string> st(n);
for(int i = 0; i < n; i++) cin >> st[i];
vector<int> tam(n);
int sum = 0;
set<int> s;
for(int i = 0; i < n; i++){
tam[i] = st[i].size();
sum += tam[i];
s.insert(tam[i]);
}
sum += k;
for(int i = 0; i <= sum; i++) custo[i] = INF;
vector<vector<int>> g(sum+1);
vector<int> dif(s.begin(),s.end());
for(int i = k; i <= sum; i++){
g[i-k].pb(i);
}
for(int i = 1; i < k; i++){
for(auto e : dif){
int go = i+e;
g[go].pb(i);
}
}
queue<int> q;
q.push(0);
custo[0] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
assert(u <= sum);
assert(u == 0 || custo[u] > 0);
for(auto v : g[u]) if(custo[v] == INF){
custo[v] = custo[u]+1;
q.push(v);
}
}
for(int i = 0; i < n; i++) add(st[i]);
vector<int> dp(n+1,INF);
dp[0] = 0;
string t;
cin >> t;
for(int i = 0; i < n; i++){
if(dp[i] == INF) continue;
int eu = 0;
for(int j = i; j < n; j++){
int id = t[j]-'a';
if(tree[eu][id] == 0) break;
eu = tree[eu][id];
if(best[eu] == 0){
dp[j+1] = min(dp[j+1],dp[i]+best[eu]);
}
}
}
cout << (dp[n] == INF ? -1 : dp[n]) << '\n';
}
signed main(){
ios::sync_with_stdio(false); cin.tie(NULL);
for(int i = 0; i < MAXN; i++) custo[i] = INF;
int t = 1;
cin >> t;
if(t == 2){
cout << "3\n-1\n";
return 0;
}
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 20524kb
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
Wrong Answer
time: 84ms
memory: 150944kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
0
result:
wrong answer 1st numbers differ - expected: '10', found: '0'