山东省第九届省赛ACM重现赛 A-Anagram

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37618760/article/details/83064092

A-Anagram

链接:https://www.nowcoder.com/acm/contest/123/A
来源:牛客网
 

题目描述

    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

题目解析

          这道题是省赛的A题,题面比较长但是大体意思比较简单。其实就是我们在第一个字符串中依次取字符和第二个字符串中的每个字符进行挨个的比较。选出最小的距离然后依次相加,最后得到一个距离和叫做sum并进行输出。

           给定两个字符串,假设是ELLY与KRIS,E到K是6,L到R是6,当第二个L到I时,L是比I大的,此时L就要绕到Z,从Z到A,再从A开始到I,这样长度就是23,Y到S同理,长度是20;这样找完之后序列长度之和就是6 +6+23+20=55.这是题目中给出的一种解答。但是题目要求我们找字符间最小的长度,我就把第一个字符串中的每一个字符与第二个字符串中的每一个字符比较,每次都找出最短的长度,然后加在一起即可。

题目源代码

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
char str1[55],str2[55];
char s1[55],s2[55];
int vis[100];
const int maxn=1<<30;
int main(){
    while(~scanf("%s %s",str1,str2)){
        memset(vis,0,sizeof(vis));//将访问数组初始化为未访问
        int sum=0;
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        for(int i=0;i<len1;i++)
            s1[i]=str1[i];
        for(int i=0;i<len2;i++)
            s2[i]=str2[i];
        for(int i=0;i<len1;i++){
            int minn=maxn;
            int temp;
            for(int j=0;j<len2;j++){
               if(vis[j]==0){
                //如果未访问过
                    if((s1[i]-'0')-(s2[j]-'0')>0){
                        //字符串1的ascii大于字符串2的ascii
                    if(abs((s1[i]-'0')-(s2[j]-'0')-26)<minn)
					   {
					    	minn=abs((s1[i]-'0')-(s2[j]-'0')-26);
						    temp=j;
					   }

                    }
                    else{
                        if(abs((s1[i]-'0')-(s2[j]-'0'))<minn){
                            minn=abs((s1[i]-'0')-(s2[j]-'0'));
                            temp=j;
                            printf("minn2=%d\n",minn);
                        }
                    }
               }
            }
            vis[temp]=1;
            sum+=minn;

        }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37618760/article/details/83064092
今日推荐