QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#393213 | #6366. Message | CharlieVinnie | WA | 2ms | 15988kb | C++17 | 6.7kb | 2024-04-18 12:04:23 | 2024-04-18 12:04:24 |
Judging History
answer
#include "bits/stdc++.h"
#undef DEBUG
#ifdef DEBUG
#include "PrettyDebug.hpp"
#else
#define debug(...) [](){}()
#define debuga(...) [](){}()
#endif
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; typedef long long ll;
constexpr int N=2e5+5;
char S[N],T[N],a[N],b[N]; int n,m,acnt,bcnt,posa[N],nxt[N],to[N][30],Xsum[N],Ysum[N],Xpos[N],Ypos[N],vis[26],V,cmp[30][30]; ll w[N];
int ban[N],beg[N]; ll val[N],f[N],tmp[N];
inline void ckmin(int& x,int y) { x=min(x,y); }
inline void ckmax(int& x,int y) { x=max(x,y); }
signed main(){
atexit([](){cerr<<"Time = "<<clock()<<" ms"<<endl;});
ios::sync_with_stdio(0); cin.tie(0);
cin>>(S+1)>>(T+1); n=strlen(S+1); m=strlen(T+1); For(i,1,n) cin>>w[i];
if(n<m) return cout<<"-1\n",0;
For(i,1,m) if(!vis[T[i]-'a']) vis[T[i]-'a']=++V;
debug(vis);
For(x,0,25) if(vis[x]) For(y,x+1,25) if(vis[y]) {
acnt=bcnt=0;
For(i,1,n) if(S[i]=='a'+x||S[i]=='a'+y) a[++acnt]=S[i],posa[acnt]=i;
For(i,1,m) if(T[i]=='a'+x||T[i]=='a'+y) b[++bcnt]=T[i];
int Xcnt=0,Ycnt=0;
For(i,1,acnt){
if(a[i]=='a'+x){
Xpos[Xsum[i]=++Xcnt]=i; Ysum[i]=Ycnt;
}
else{
Ypos[Ysum[i]=++Ycnt]=i; Xsum[i]=Xcnt;
}
}
int L=1; while(b[L+1]==b[L]) L++;
int R=bcnt; while(b[R-1]==b[R]) R--;
L++; R--;
if(L>R){
int u=(b[1]=='a'+x?x:y);
int v=(b[bcnt]=='a'+x?x:y);
assert(u!=v);
cmp[vis[u]][vis[v]]=1;
cmp[vis[v]][vis[u]]=-1;
For(i,1,acnt){
if(a[i]=='a'+v){
if(v==y){
if(Ysum[i]<bcnt-R) continue;
int t=Ypos[Ysum[i]-(bcnt-R)+1];
if(Xsum[t-1]<R) continue;
int o=Xpos[Xsum[t-1]];
to[posa[i]][vis[x]]=posa[o];
}
else{
if(Xsum[i]<bcnt-R) continue;
int t=Xpos[Xsum[i]-(bcnt-R)+1];
if(Ysum[t-1]<R) continue;
int o=Ypos[Ysum[t-1]];
to[posa[i]][vis[y]]=posa[o];
}
}
}
continue;
}
bool oth=0; For(i,L,R) if(b[i]!=b[L]) { oth=1; break; }
if(!oth){
if(b[L]=='a'+x){
cmp[vis[y]][vis[x]]=-1;
}
else{
cmp[vis[x]][vis[y]]=-1;
}
}
nxt[L]=L-1;
for(int i=L+1,j=L-1;i<=R;i++){
while(j>=L&&b[j+1]!=b[i]) j=nxt[j];
if(b[j+1]==b[i]) j++;
nxt[i]=j;
}
for(int i=1,j=L-1;i<=acnt;i++){
while(j>=L&&b[j+1]!=a[i]) j=nxt[j];
if(b[j+1]==a[i]) j++;
if(j==R){
j=nxt[j];
if(a[i]=='a'+x){
if( (b[1]=='a'+x?Xsum[i-(R-L+1)]:Ysum[i-(R-L+1)])>=L-1 && Ysum[acnt]-Ysum[i]>=bcnt-R ){
int u = Ypos[Ysum[i]+(bcnt-R)]; to[posa[u]][vis[x]]=posa[i]; to[posa[i]][vis[y]]=posa[u];
}
}
else{
if( (b[1]=='a'+x?Xsum[i-(R-L+1)]:Ysum[i-(R-L+1)])>=L-1 && Xsum[acnt]-Xsum[i]>=bcnt-R ){
int u = Xpos[Xsum[i]+(bcnt-R)]; to[posa[u]][vis[y]]=posa[i]; to[posa[i]][vis[x]]=posa[u];
}
}
}
}
}
// For(u,1,V){
// int lst=0;
// For(i,1,n) if(vis[S[i]-'a']==u) {
// if(lst==0) { lst=i; continue; }
// For(v,1,V) if(cmp[u][v]==1) to[i][v]=max(to[i][v],to[lst][v]);
// lst=i;
// }
// lst=0;
// Rev(i,n,1) if(vis[S[i]-'a']==u) {
// if(lst==0) { lst=i; continue; }
// For(v,1,V) if(cmp[u][v]==-1) to[i][v]=min(to[i][v],to[lst][v]);
// lst=i;
// }
// }
For(i,1,V) debuga(cmp[i],1,V);
For(u,1,n){
For(i,1,V) if(vis[S[u]-'a']!=i) debug(u,i,to[u][i]);
}
For(x,1,V){
int L=0,R=0;
For(i,1,m) if(vis[T[i]-'a']==x) R=i;
Rev(i,m,1) if(vis[T[i]-'a']==x) L=i;
For(i,L,R-1) ban[i]=1;
int cntB=0; For(i,1,m) if(vis[T[i]-'a']==x) cntB++;
static int pos[N]; int pcnt=0; ll qwq=0;
For(i,1,n) if(vis[S[i]-'a']==x){
qwq+=w[i]; pos[++pcnt]=i;
if(pcnt>=cntB) qwq-=w[pos[pcnt-cntB]],val[i]=qwq,beg[i]=pos[pcnt-cntB+1];
else val[i]=-1e18;
}
}
debuga(val,1,n);
f[0]=0; For(i,1,n) f[i]=-1e18;
int has[30]={0};
auto work=[&](){
For(i,0,n) tmp[i]=f[i],f[i]=-1e18;
For(i,1,n) tmp[i]=max(tmp[i],tmp[i-1]);
int qwq=0;
For(u,1,V) if(has[u]){
int ok=1;
For(v,1,V) if(has[v]&&cmp[u][v]!=0) { ok=0; break; }
if(ok) { qwq=u; break; }
}
debug(qwq);
assert(qwq);
For(i,1,n) if(vis[S[i]-'a']==qwq) {
int pos[30]={0};
function<bool(int,int)> dfs=[&](int u,int o){
if(pos[u]) return pos[u]==o?true:false;
else pos[u]=o;
For(v,1,V) if(v!=u&&cmp[u][v]==0){
if(to[o][v]==0) return false;
if(!dfs(v,to[o][v])) return false;
}
return true;
};
if(!dfs(qwq,i)) continue;
For(u,1,V) if(has[u]) assert(pos[u]);
int OK=1; For(u,1,V) if(has[u]) For(v,1,V) if(has[v]){
if(cmp[u][v]==1&&pos[u]>to[pos[v]][u]) { OK=0; goto OUT; }
}
OUT: if(!OK) continue;
int mx=0,mn=1e9; For(u,1,V) if(has[u]) mx=max(mx,pos[u]),mn=min(mn,beg[pos[u]]);
if(mn==0) continue;
ll tt=0; For(u,1,V) { tt+=val[pos[u]]; if(tt<-1e18) break; }
f[mx]=max(f[mx],tmp[mn-1]+tt);
}
};
debuga(ban,1,m);
For(i,1,m){
has[vis[T[i]-'a']]=1;
if(!ban[i]){
work(); memset(has,0,sizeof(has));
debug(i); debuga(f,0,n);
}
}
ll ans=-1e18; For(i,1,n) ans=max(ans,f[i]);
if(ans<0) cout<<"-1\n";
else{
ans=-ans; For(i,1,n) ans+=w[i];
cout<<ans<<'\n';
}
return 0;
}
// CONTINUE, NON-STOPPING, FOR CHARLIEY
// START TYPING IF YOU DON'T KNOW WHAT TO DO
// STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
// Started Coding On: April 18 Thu, 07 : 48 : 39
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 13952kb
input:
ababccb abc 7 2 2 4 3 2 1
output:
7
result:
ok single line: '7'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 15988kb
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'