QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135391 | #6635. Strange Keyboard | nameless_story | WA | 2ms | 8208kb | C++20 | 1.3kb | 2023-08-05 14:39:46 | 2023-08-05 14:39:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define N 1200000
const int INF=0x3f3f3f3f;
int c[N][30],mn[N],tot;
void cmin(int &x,int y) { x=min(x,y); }
void solve()
{
int n,k; cin>>n>>k;
for (int i=1; i<=tot; ++i)
{
memset(c[i],0,sizeof c[i]);
mn[i]=INF;
}
tot=1;
vector<string> s;
vector vis(k,INF);
for (int i=1; i<=n; ++i)
{
string st; cin>>st;
s.push_back(st);
cmin(vis[st.size()%k],st.size()/k+1);
}
string ss; cin>>ss;
int m=ss.size();
vector dp(m+1,INF),f(k+1,INF);
f[0]=0;
for (int i=1; i<k; ++i)
{
if (vis[i]==INF) continue;
for (int j=0; j<k; ++j)
{
cmin(f[(i+j)%k],f[j]+vis[i]+(i+j>=k));
}
for (int j=0; j<k; ++j)
{
cmin(f[(i+j)%k],f[j]+vis[i]+(i+j>=k));
}
}
for (auto st:s)
{
int x=1,len=st.size(),step=0;
for (auto w:st)
{
if (!c[x][w]) c[x][w]=++tot;
x=c[x][w];
--len;
cmin(mn[x],f[(k-len%k)%k]+len/k+(len>0));
}
}
dp[0]=0;
for (int i=0; i<ss.size(); ++i)
{
int x=1;
for (int j=i; j<ss.size(); ++j)
{
int w=ss[j];
x=c[x][w];
if (!x) break;
cmin(dp[j+1],dp[i]+mn[x]+1);
// f[i][j+1]=mn[x];
}
}
cout<<(dp[ss.size()]==INF?-1:dp[ss.size()])<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int T; cin>>T;
memset(mn,0x3f,sizeof mn);
while (T--)
{
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8208kb
input:
2 2 3 defgh abc abcde 1 1 a b
output:
4 -1
result:
wrong answer 1st numbers differ - expected: '3', found: '4'