QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217657 | #6366. Message | liqingyang | WA | 1ms | 5756kb | C++14 | 2.3kb | 2023-10-17 08:24:30 | 2023-10-17 08:24:31 |
Judging History
answer
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int Task,T,n,m,a[200010],p[26],cnt[26],Cnt[26];
char s[200010],t[200010];
vector<int> S[26];
vector<long long> A[26];
long long ans,V[26];
void dfs(int ps,int pt,long long val)
{
if(val>=ans)
{
return;
}
if(pt>m)
{
ans=val;
return;
}
int minn=1e9;
for(int i=0;i<26;i++)
{
if(Cnt[i]&&Cnt[i]<cnt[i])
{
minn=min(minn,S[i][p[i]]);
}
}
int c=t[pt]-'a';
if(!(Cnt[c]++))
{
int P=upper_bound(S[c].begin(),S[c].end(),ps)-S[c].begin();
for(int i=P;i+cnt[c]<=S[c].size()&&S[c][i]<minn;i++)
{
p[c]=i;
dfs(S[c][p[c]++],pt+1,val-V[c]+A[c][i]+A[c].back()-A[c][i+cnt[c]]);
}
Cnt[c]--,p[c]=0;
return;
}
if(S[c][p[c]]>minn)
{
Cnt[c]--;
return;
}
dfs(S[c][p[c]++],pt+1,val);
Cnt[c]--,p[c]--;
}
long long f[200010][110];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>Task>>T;
while(T--)
{
cin>>n>>m>>s+1>>t+1;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=m;i++)
{
cnt[t[i]-'a']++;
}
if(m<=100)
{
memset(f,0x3f,sizeof(f)),f[0][0]=0;
for(int i=1;i<=n;i++)
{
memset(Cnt,0,sizeof(Cnt));
for(int j=0;j<=m;j++)
{
if(j)
{
Cnt[t[j]-'a']++;
}
long long v=f[i-1][j];
if(!Cnt[s[i]-'a']||Cnt[s[i]-'a']==cnt[s[i]-'a'])
{
f[i][j]=min(f[i][j],v+a[i]);
}
if(s[i]==t[j+1])
{
f[i][j+1]=min(f[i][j+1],v);
}
}
}
cout<<(f[n][m]<1e18?f[n][m]:-1)<<endl;
continue;
}
for(int i=0;i<26;i++)
{
S[i].clear(),A[i].clear();
A[i].push_back(0);
}
for(int i=1;i<=n;i++)
{
S[s[i]-'a'].emplace_back(i);
A[s[i]-'a'].emplace_back(A[s[i]-'a'].back()+a[i]);
}
bool flag=1;
long long sum=0;
for(int i=0;i<26;i++)
{
if(cnt[i])
{
V[i]=1e18;
if(S[i].size()<cnt[i])
{
flag=0;
break;
}
for(int j=0;j+cnt[i]<=S[i].size();j++)
{
V[i]=min(V[i],A[i][j]+A[i].back()-A[i][j+cnt[i]]);
}
sum+=V[i];
}
}
if(!flag)
{
cout<<-1<<endl;
continue;
}
ans=1e18,dfs(0,1,sum);
for(int i=0;i<26;i++)
{
if(!cnt[i])
{
ans+=A[i].back();
}
}
cout<<(ans<1e18?ans:-1)<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5756kb
input:
ababccb abc 7 2 2 4 3 2 1
output:
result:
wrong answer 1st lines differ - expected: '7', found: ''