bnuoj 29375 Two Strings(字符串?)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=29375

【题意】:可以对两字符串进行如下操作:

  1、可以无损耗交换相邻两个字符(可以理解成交换任意字符)

     2、可以改变一个字符 x->y ,花费为 x-y 的绝对值

   求花费最少,将两字符串变成一样

【题解】:

     排序字符串,然后对应相减

【code】:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 char str1[100010],str2[100010];
 9 
10 int abs(int a)
11 {
12     return a<0?-a:a;
13 }
14 
15 int main()
16 {
17     int t,cas=1;
18     scanf("%d",&t);
19     while(t--)
20     {
21         int m;
22         scanf("%d",&m);
23         scanf("%s",str1);
24         scanf("%s",str2);
25         sort(str1,str1+m);
26         sort(str2,str2+m);
27         int i,ans=0;
28         for(i=0;i<m;i++)
29         {
30             ans+=abs(str1[i]-str2[i]);
31         }
32         printf("Case %d: %d\n",cas++,ans);
33     }
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/crazyapple/p/3351472.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值