QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#393224 | #6366. Message | AFewSuns | WA | 3ms | 22728kb | C++17 | 4.1kb | 2024-04-18 12:29:43 | 2024-04-18 12:29:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll int
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
#define bs 29
#define mod 998244853
#define LC x<<1
#define RC x<<1|1
priority_queue<long long> q1,q2;
vector<long long> ins[200020],del[200020];
ll n,N,m,a[200020],suf[200020][26];
ll p[26][200020],cnt[26],nxt[200020][26];
ll fir[33],pos[33],tot=0,pw[200020];
ll tree[800080],siz[800080],ntot[26],ed[26];
long long f[200020],ans=-inf;
bl ck[26];
char s[200020],t[200020];
il void pushup(ll x){
tree[x]=(1ll*tree[LC]*pw[siz[RC]]+tree[RC])%mod;
siz[x]=siz[LC]+siz[RC];
}
il void mdf(ll x,ll v){
tree[x+N-1]=v*(s[x]-'a'+1);
siz[x+N-1]=v;
for(ll i=(x+N-1)>>1;i;i>>=1) pushup(i);
}
int main(){
scanf("%s",s+1);
scanf("%s",t+1);
n=strlen(s+1);
m=strlen(t+1);
fr(i,1,n) a[i]=read();
fr(i,1,n) p[s[i]-'a'][++cnt[s[i]-'a']]=i;
fr(i,0,25) nxt[n][i]=n+1;
pfr(i,n-1,0){
fr(j,0,25) nxt[i][j]=nxt[i+1][j];
nxt[i][s[i+1]-'a']=i+1;
}
pfr(i,m,1){
fr(j,0,25) suf[i][j]=suf[i+1][j];
suf[i][t[i]-'a']++;
fir[t[i]-'a']=i;
}
fr(i,0,25){
if(suf[1][i]>cnt[i]){
write(-1);
return 0;
}
}
fr(i,0,25) if(fir[i]) pos[++tot]=fir[i];
sort(pos+1,pos+tot+1);
pos[tot+1]=m+1;
pw[0]=1;
fr(i,1,n) pw[i]=1ll*pw[i-1]*bs%mod;
fr(i,1,n) f[i]=-inf;
fr(i,1,n) if(s[i]==t[1]) f[i]=0;
N=1<<(__lg(n+5)+1);
fr(i,1,tot){
ck[t[pos[i]]-'a']=1;
ll hsh=0;
long long nsum=0;
fr(j,0,25) ntot[j]=suf[pos[i]][j]-suf[pos[i+1]][j];
fr(j,pos[i],pos[i+1]-1) hsh=(1ll*hsh*bs+(t[j]-'a'+1))%mod;
fr(j,0,25) ed[j]=p[j][ntot[j]];
fr(j,1,2*N) tree[j]=siz[j]=0;
fr(j,0,25) fr(k,1,ntot[j]) mdf(p[j][k],1);
fr(j,0,25) fr(k,1,ntot[j]) nsum+=a[p[j][k]];
fr(x,1,n){
if(s[x]==t[pos[i]]&&tree[1]==hsh){
ll nl=x+1,nr=n+1;
fr(j,0,25){
if(!ck[j]) continue;
if(suf[pos[i+1]][j]){
nl=max(nl,ed[j]+1);
if(ed[j]) nr=min(nr,nxt[ed[j]][j]);
else nr=min(nr,nxt[x][j]);
}
else nl=max(nl,ed[j]+1);
}
if(nl<=nr){
if(i==tot) ans=max(ans,f[x]+nsum);
else{
ins[nl].push_back(f[x]+nsum);
del[nr].push_back(f[x]+nsum);
}
}
}
if(ntot[s[x]-'a']){
if(nxt[ed[s[x]-'a']][s[x]-'a']>n) break;
ed[s[x]-'a']=nxt[ed[s[x]-'a']][s[x]-'a'];
mdf(x,0);
mdf(ed[s[x]-'a'],1);
nsum+=a[ed[s[x]-'a']]-a[x];
}
}
if(i==tot) break;
long long maxx=-inf;
fr(j,1,n){
fr(k,0,(ll)ins[j].size()-1){
q1.push(ins[j][k]);
maxx=max(maxx,ins[j][k]);
}
if(s[j]==t[pos[i+1]]) f[j]=maxx;
fr(k,0,(ll)del[j].size()-1){
q2.push(del[j][k]);
while(!q2.empty()&&q1.top()==q2.top()){
q1.pop();
q2.pop();
}
if(!q1.empty()) maxx=q1.top();
else maxx=-inf;
}
}
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
fr(j,1,n+1) ins[j].clear();
fr(j,1,n+1) del[j].clear();
}
long long all=0;
fr(i,1,n) all+=a[i];
if(ans<0) pf("-1");
else cout<<all-ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22728kb
input:
ababccb abc 7 2 2 4 3 2 1
output:
7
result:
ok single line: '7'
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 18432kb
input:
babab baab 2 1 3 2 4
output:
-1
result:
wrong answer 1st lines differ - expected: 'You better start from scratch man...', found: '-1'