QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#130716 | #6635. Strange Keyboard | Asif17r | WA | 721ms | 4868kb | C++14 | 6.6kb | 2023-07-24 21:19:14 | 2023-07-24 21:19:16 |
Judging History
answer
#pragma GCC optimize("unroll-loops,O3")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
const int mod = 1e9 + 7;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define endl '\n'
typedef pair<int, int> pii;
typedef vector<int> vi;
#define in(_x) int _x; cin >> _x;
template<typename... T> void out(T&&... args) {((cout << args << " "), ...); cout << endl;}
#define inp(arr,n) vector<int>arr(n); for(auto &_a: arr) cin >> _a;
#define print(arr) for(auto a: arr) cout << a<< " "; cout << endl;
#define pb emplace_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define f first
#define s second
//**************OPTIONAL******************
inline int uceil(int a,int b) {int res = a/b; if(a%b and res >= 0) res++; return res;}
#define set_bit(x, idx) x = x|(1LL<<idx)
int dx[8] = {0,1,0,-1,1,-1,1,-1};
int dy[8] = {-1,0,1,0,1,1,-1,-1};
#define toggle_bit(x, idx) x = x^(1LL<<idx)
#define check_bit(x, idx) min(x&(1LL<<idx),1LL)
#define mxheap priority_queue<int>
#define mnheap priority_queue<int, vector<int>, greater<int>>
#define mxheap2 priority_queue<pair<int,int>>
#define mnheap2 priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //__gnu_cxx::sfmt19937_64 #include <ext/random>
#define rng(x,y) uniform_int_distribution<int>(x,y)(rng)
#ifdef DEBUG
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define pause(x) this_thread::sleep_for(chrono::milliseconds(x));
#define ios
#define dbg(...) {cerr << __LINE__ << " : " ;cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);}
#define ok cerr << __LINE__ << " is done " << endl;
#else
#define pause(x)
#define ios {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
#define dbg(...)
#define ok
#endif
const int inf = 2e9;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
typedef int32_t ull;
struct H {
ull x; H(ull _x=0) : x(_x) {}
H operator+(H o) { return x + o.x + (x + o.x < x); }
H operator-(H o) { return *this + ~o.x; }
H operator*(H o) { auto m = (__uint128_t)x * o.x;
return H((ull)m) + (ull)(m >> 64); }
ull get() const { return x + !~x; }
bool operator==(H o) const { return get() == o.get(); }
bool operator<(H o) const { return get() < o.get(); }
};
static const H C = rng(1e7,1e9); // (order ~ 3e9; random also ok)
struct HashInterval {
vector<H> ha, pw;
HashInterval(string& str) : ha(sz(str)+1), pw(ha) {
pw[0] = 1;
rep(i,0,sz(str))
ha[i+1] = ha[i] * C + str[i],
pw[i+1] = pw[i] * C;
}
H hashInterval(int a, int b) { // hash [a, b)
return ha[b] - ha[a] * pw[b - a];
}
void insert(char a){
ha.pb(ha.back()*C + a);
pw.pb(pw.back()*C);
}
};
vector<int> dummybfs(vector<int>&length, int k){
vector<int>len(k), vis(k, 0);
len[0] = 0;
queue<int>q;
q.push(0);
vis[0] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (auto it : length) {
int x = it;
int nxt = x+cur;
if (nxt > k) {
int t = nxt/k;
nxt%=k;
if (!vis[nxt]) {
len[nxt] = len[cur] + 1 + t;
vis[nxt] = 1;
q.push(nxt);
}
}
else if (!vis[nxt]) {
len[nxt] = len[cur] + 1;
vis[nxt] = 1;
q.push(nxt);
}
}
}
return len;
}
void solvetc(int tt){
in(n) in(k)
vector<string>strs(n);
for(auto &a: strs) cin >> a;
string target; cin >> target;
vector<int>singleStr(k+1, inf);
singleStr[0] = 0;
vector<int>lengths;
dbg(strs);
for(auto a: strs) lengths.pb(a.size());
//~ dbg(lengths);
sort(all(lengths));
lengths.erase(unique(all(lengths)), lengths.end());
//~ dbg(lengths);
//~ for(auto a: strs){
//~ int nw = a.size();
//~ singleStr[nw%k] = min(singleStr[nw%k], (nw/k)+1);
//~ }
vector<int>placementDP = dummybfs(lengths, k);
//~ for(auto &a: placementDP) a++;
//~ return;
gp_hash_table<int,int,custom_hash>dp;
set<int>ase;
for(string a: strs){
HashInterval h(a);
for(int i = 1; i <= a.size(); i++){
int barti = a.size() - i;
int cost = uceil(barti, k);
barti %= k; barti = (k-barti)%k;
if(placementDP[barti] >= inf) continue;
cost += placementDP[barti] + 1;
//~ dbg(0,i,barti);
int hash = h.hashInterval(0,i).get();
if(dp[hash] == 0) {
dp[hash] = inf;
ase.insert(hash);
}
dp[hash] = min(dp[hash], cost+1);
}
}
int mx = target.size();
vector<int>finalDP(mx+2, inf);
finalDP[mx] = 0;
HashInterval keanu(target);
for(int i = mx-1; i >= 0; i--){
for(int j = i+1; j <= mx; j++){
ull now = keanu.hashInterval(i,j).get();
if(ase.find(now) == ase.end()) continue;
now = dp[now];
finalDP[i] = min(finalDP[i], now + finalDP[j] - 1);
dbg(i, j, finalDP[i]);
}
}
if(finalDP[0] >= inf){
cout << -1 << endl;
}else{
cout << finalDP[0] << endl;
}
}
int32_t main()
{
ios ;
#ifdef DEBUG
//~ freopen("in", "r", stdin);
#endif
int nn = 1;
cin >> nn;
rep(i,0, nn) solvetc(i+1);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3556kb
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: 721ms
memory: 4868kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
-1
result:
wrong answer 1st numbers differ - expected: '10', found: '-1'