QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331850 | #6366. Message | wullaaa | ML | 47ms | 175932kb | C++14 | 2.1kb | 2024-02-18 20:30:05 | 2024-02-18 20:30:06 |
Judging History
answer
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
const int N=2e5+5,P=113;
const ll INF=1e18;
int n,m,w[N],sz[70],id[70][N],pos[70][N];
char s[N],t[N],ss[70][N];
int le[26],ri[26],tot=1,l[70],r[70];
bool vis[26],alp[70][26];
ull pw[N],h[70][N];
ull get(int l,int r,int k){ return h[k][r]-h[k][l-1]*pw[r-l+1]; }
ll dp[N][70],sum[70][N];
void Min(ll &x,ll y){ x=x<y?x:y; }
int main(){
scanf("%s\n%s",s+1,t+1),n=strlen(s+1),m=strlen(t+1);
pw[0]=1; for(int i=1;i<=n;++i) scanf("%d",&w[i]),pw[i]=pw[i-1]*P;
for(int i=1;i<=m;++i) (!le[t[i]-'a'])&&(le[t[i]-'a']=i),ri[t[i]-'a']=i;
for(int i=1;i<=m;++i){
if(le[t[i]-'a']==i) vis[t[i]-'a']=true,l[++tot]=i;
if(ri[t[i]-'a']==i) r[tot]=i,memcpy(alp[tot],vis,sizeof(vis)),vis[t[i]-'a']=false,r[tot]=i,++tot,l[tot]=i+1,r[tot]=i,memcpy(alp[tot],vis,sizeof(vis));
h[0][i]=h[0][i-1]*P+t[i];
}
for(int i=1;i<=tot;++i){
for(int j=1;j<=n;++j){
sum[i][j]=sum[i][j-1];
if(alp[i][s[j]-'a']) ss[i][++sz[i]]=s[j],pos[i][sz[i]]=j,id[i][j]=sz[i];
else id[i][j]=sz[i]+1,sum[i][j]+=w[j];
}
for(int j=1;j<=sz[i];++j) h[i][j]=h[i][j-1]*P+ss[i][j];
}
for(int i=0;i<=n;++i) for(int j=0;j<=tot;++j) dp[i][j]=INF;
dp[0][0]=dp[0][1]=0;
for(int i=0;i<=n;++i)
for(int j=0;j<=tot;++j) if(dp[i][j]<INF){
// cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
if(r[j]-l[j]+1==0&&!alp[j][i+1]) Min(dp[i+1][j],dp[i][j]+w[i+1]);
if(r[j+1]-l[j+1]+1==0){
if(!alp[j+1][i+1]) Min(dp[i+1][j+1],dp[i][j]+w[i+1]);
Min(dp[i][j+1],dp[i][j]);
}else{
int st=id[j+1][i+1],ed=id[j+1][i+1]+r[j+1]-l[j+1];
if(ed<=sz[j+1]&&get(st,ed,j+1)==get(l[j+1],r[j+1],0)) Min(dp[pos[j+1][ed]][j+1],dp[i][j]+sum[j+1][pos[j+1][ed]]-sum[j+1][i]);
}
}
ll Sum=0;
if(dp[n][tot]==INF) puts("You better start from scratch man...");
else printf("%lld\n",dp[n][tot]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24340kb
input:
ababccb abc 7 2 2 4 3 2 1
output:
7
result:
ok single line: '7'
Test #2:
score: 0
Accepted
time: 0ms
memory: 24360kb
input:
babab baab 2 1 3 2 4
output:
You better start from scratch man...
result:
ok single line: 'You better start from scratch man...'
Test #3:
score: 0
Accepted
time: 20ms
memory: 149472kb
input:
bbaaaabbbbbabaababbaaabbbabaaaababaababaabababbbbabbbbababaabaaabbabaaaabbbabbaababababbabbbabbaababaaaaaabbabbbababbaabbbabbbabbbabaababaaaaaabaaababbbbbbaabaabaaaaaaabbbaaabaabbbababbbbbabbababaababaabbababbaababbbbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbab...
output:
You better start from scratch man...
result:
ok single line: 'You better start from scratch man...'
Test #4:
score: 0
Accepted
time: 47ms
memory: 175932kb
input:
bdabcfbdfcffebebcabbadacbbaeeaffbdedeedfabefdfdbddcecdaaddafdfbbdceccedebcecdfbcfbaafcefeecffdabfaacbeeecfeffaaafaefdcdaaeaeecfafcdadbfbccbdecacfeabdbfcacafebdcfbfbebacbffaecbfbcedccabbdecabaebbbdbcfbaeadfcadfadfaebaddbebfcbefdabdcefbbdaaaabcefedabcabcafedcfadedfdcbbccbffdcfdfcfcdfcfbbdabdbbeecafecc...
output:
You better start from scratch man...
result:
ok single line: 'You better start from scratch man...'
Test #5:
score: -100
Memory Limit Exceeded
input:
soibsuydrizsuvymezuyrewmgwpnzxgyggpzjkdzooisgzbkfqjzkfcklluotqpwganvksoqtzixkfkrtqobdnregwgkxjwzsruvhztscxjyqlhfytomzhxiglxemdhkjnskrsqngojffogrkbygmdgzfwrlhwhhngqpjpepqgynsdybhpuaqhgjroijqofiwnxgxdmhofwsjnmwitruiesefzmabcfsyzrrruidewjowfkwwwqhztsmmtdnejlqhkmbpmknlxijnmzbtqviburbqwufipqsrqplcelovsxz...
output:
You better start from scratch man...