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 }

 

posted @ 2013-10-04 14:18  crazy_apple  阅读(275)  评论(0编辑  收藏  举报