【BZOJ】1830: [AHOI2008]Y型项链

本文提供了一种解决BZOJ1830问题的有效算法,通过枚举目标字符串作为三个输入字符串之一的前缀,并计算最小操作数。代码采用C++实现,详细展示了如何遍历字符串并计算成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1830


 

直接枚举目标串是什么,目标串一定是三个字符串的某一个前缀(注意可能为空),然后判断一下取个最小值。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<cstring>
 8 using namespace std;
 9 #define maxn 55
10 #define llg long long 
11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
12 llg n,m,tot,ans,len,l[maxn];
13 
14 char s[5][maxn],aim[maxn];
15 
16 llg work(llg x)
17 {
18     llg wz=0;
19     for (llg i=1;i<=len;i++) 
20         if (aim[i]==s[x][i])
21         {
22             wz=i;
23         }
24         else break;
25     if (wz==len) return l[x]-len;
26     return (l[x]-wz)+len-wz;
27 }
28 
29 int main()
30 {
31     yyj("bzoj1830");
32     for (llg i=1;i<=3;i++)
33     {
34         cin>>l[i];
35         cin>>(s[i]+1);
36     }
37     ans=0x7fffffff;
38     for (llg k=1;k<=3;k++)
39         for (llg i=1;i<=1;i++)
40             for (llg j=i-1;j<=l[k];j++)
41             {
42                 for (llg w=0;w<maxn;w++) aim[w]='\0';
43                 tot=0;
44                 len=j-i+1;
45                 for (llg w=1;w<=len;w++) aim[w]=s[k][i+w-1];
46                 for (llg w=1;w<=3;w++) tot+=work(w);
47                 ans=min(ans,tot);
48             }
49     cout<<ans;
50     return 0;
51 }

 

转载于:https://www.cnblogs.com/Dragon-Light/p/6518126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值