比赛 |
EYOI与SBOI开学欢乐赛10th |
评测结果 |
AAAWWAWWWW |
题目名称 |
01串 |
最终得分 |
40 |
用户昵称 |
yrtiop |
运行时间 |
0.186 s |
代码语言 |
C++ |
内存使用 |
78.77 MiB |
提交时间 |
2022-10-10 21:31:16 |
显示代码纯文本
#include <bits/stdc++.h>
typedef long long ll;
const int maxn = 5e3 + 5;
char s[maxn],t[maxn];
int n,x,y,lst[maxn],sum[maxn],nxt[maxn];
ll dp[maxn][maxn];
ll dfs(int p,int q) {
if(p > q)return 0;
if(dp[p][q])return dp[p][q];
if(s[p] == t[p]||s[q] == t[q])return 0;
if(1ll * (q - p) * x > 1ll * y)return dp[p][q] = std::min(dfs(nxt[p] , lst[q]) + 1ll * y , dfs(p , lst[lst[q]]) + std::min(1ll * (q - lst[q]) * x , 1ll * y));
return dp[p][q] = dfs(p , lst[lst[q]]) + 1ll * (q - lst[q]) * x;
}
void solve() {
int res = 0,p,q;
for(int i = 1;i <= n;++ i) {
if(t[i - 1] != s[i - 1])lst[i] = i - 1;
else lst[i] = lst[i - 1];
sum[i] = sum[i - 1] + (s[i] != t[i]);
}
if(sum[n] == 0) {
puts("0");
return ;
}
nxt[n + 1] = n + 1;
for(int i = n;i;-- i) {
if(s[i + 1] != t[i + 1])nxt[i] = i + 1;
else nxt[i] = nxt[i + 1];
}
p = (s[1] != t[1]) ? 1 : nxt[1];
q = (s[n] != t[n]) ? n : lst[n];
printf("%lld\n",dfs(p , q));
return ;
}
int main() {
freopen("zerone.in","r",stdin);
freopen("zerone.out","w",stdout);
scanf("%d %d %d",&n,&x,&y);
scanf("%s %s",s + 1,t + 1);
int tot = 0,res = 0;
for(int i = 1;i <= n;++ i)
tot += (s[i] ^= '0');
for(int i = 1;i <= n;++ i)
res += (t[i] ^= '0');
if((tot - res) & 1) {
puts("-1");
return 0;
}
if(y > x) {
solve();
return 0;
}
res = 0;
for(int i = 1;i <= n;++ i)
res += s[i] ^ t[i];
if(res == 2) {
int p,q;
for(int i = 1;i <= n;++ i) {
if(s[i] != t[i]) {
p = i;
break ;
}
}
for(int i = n;i;-- i) {
if(s[i] != t[i]) {
q = i;
break ;
}
}
if(p == q + 1)printf("%d\n",std::min(x , y << 1));
else printf("%d\n",y);
return 0;
}
printf("%lld\n",1ll * (res >> 1ll) * y);
return 0;
}