浪潮杯第九届山东acm程序设计赛A题 Anagram

题目描述

  Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram of B (which means, a rearrangement of B) by changing some of its letters. The only operation the girl can make is to “increase” some (possibly none or all) characters in A. E.g., she can change an ‘A’ to a ‘B’, or a ‘K’ to an ‘L’. She can increase any character any times. E.g., she can increment an ‘A’ three times to get a ‘D’. The increment is cyclic: if she increases a ‘Z’, she gets an ‘A’ again.

For example, she can transform “ELLY” to “KRIS” character by character by shifting ‘E’ to ‘K’ (6 operations), ‘L’ to ‘R’ (again 6 operations), the second ‘L’ to ‘I’ (23 operations, going from ‘Z’ to ‘A’ on the 15-th operation), and finally ‘Y’ to ‘S’ (20 operations, again cyclically going from ‘Z’ to ‘A’ on the 2-nd operation). The total number of operations would be 6 + 6 + 23 + 20 = 55. However, to make “ELLY” an anagram of “KRIS” it would be better to change it to “IRSK” with only 29 operations. You are given the strings A and B. Find the minimal number of operations needed to transform A into some other string X, such that X is an anagram of B.

输入描述:

There will be multiple test cases. For each testcase:

There is two strings A and B in one line.∣A∣=∣B∣≤50. A and B will contain only uppercase letters
from the English alphabet (‘A’-‘Z’).

输出描述:

For each test case, output the minimal number of
operations.

示例1

输入

ABCA BACA

ELLY KRIS

AAAA ZZZZ

输出

0

29

100

题解:

给定两个字符串,每次字符串的字符可以对应相应的字符距离,如‘ELLY’与‘KRIS’之间,‘E’到‘K’的距离为之间相减,ASIIC码,75-69=6,‘L’到‘R’的距离为82-76=6,‘L’到‘I’的距离,因为'I'在‘L’的前面,求‘L’到'I'的距离时需要从‘L’到'Z'然后从'Z'到‘A’,再从'A'到‘I’,相当于得绕一个圈,求得为23,‘Y’到'S'的距离同理为20,那序列距离之和为6+6+23+20=55,但这不是最短的序列和,‘ELLY’和‘IRSK’之间一一对应时距离最短。为29.

思路,两字符串a,b,可使字符串第一个字符与字符串b中的每个字符相比较,选距离最短的那个,表示字符串a中第一个字符与b中的该字符匹配了,然后a中的第二个字符再开始寻找,依次寻找,求得总sum。

 

注意:

若字符串a的第一个字符找到了字符串b中的第二个字符匹配,那么字符串a中的其他字符在寻找匹配字符时不可再寻找b中的第二个字符。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1e9
int main(){
   char a[51],b[51];
   int bb[51];
   int vis[51];
   int sum;
   int k=0;
   int temp;
   while(scanf("%s%s",a,b)!=EOF){
        int len=strlen(a);
        memset(vis,0,sizeof(vis));
        sum=0;
        for(int i=0;i<len;i++){
            int minn=maxn;
            for(int j=0;j<len;j++){
                if(!vis[j]){
                    if(a[i]>b[j]){
                        bb[j]='Z'-a[i]+1+b[j]-'A';
                        if(bb[j]<minn){
                            minn=bb[j];
                            temp=j;
                        }
                    }
                else{
                    bb[j]=b[j]-a[i];
                    if(bb[j]<minn){
                        minn=bb[j];
                        temp=j;
                    }
                }
               }
            }
        sum=sum+minn;
        vis[temp]=1;
    }
    printf("%d\n",sum);
   }
}

猜你喜欢

转载自blog.csdn.net/lijunyan5/article/details/83045214
今日推荐