Anagram

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
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.

Input
There will be multiple test cases. For each test case:
There is two strings A and B in one line. |A| = |B| \leq 50∣A∣=∣B∣≤50. A and B will contain only uppercase letters from the English alphabet (‘A’-‘Z’).

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

Sample Input
ABCA BACA
ELLY KRIS
AAAA ZZZZ
Sample Output
0
29
100
Hint
Source
“浪潮杯”山东省第九届ACM大学生程序设计竞赛(感谢山东财经大学)

#include<stdio.h>
#include<iostream>
#include <iomanip>
#include <string.h>
using namespace std;
void mySort(char a[])
{
    int i, j;
    int len = strlen(a);
    char t;
    for(i = 0; i < len - 1; i++)
    {
        for(j = 0; j < len - i - 1; j++)
        {
            if(a[j] > a[j+1])
            {
                t = a[j];
                a[j] = a[j+ 1];
                a[j+1] = t;
            }
        }
    }
    //for(i = 0; i < )
}
int main()
{
    char a[102];
    char b[102];
    while(cin >> a >> b)
    {
        mySort(a);
        mySort(b);
        int len = strlen(a);
        char c[105];
        int i = 0;
        int j = 0, k = 0;
        int sum = 0;
        while(j < len)
        {
            if(a[i] <= b[j])
            {
                sum += b[j] - a[i];
                i++;
                j++;
            }
            else
            {
                c[k++] = b[j];
                j++;
            }
        }
        if(j == len)
        {
            j = 0;
            while(j < k)
            {
                sum += c[j]  - a[i]+26;
                j++;
                i++;
            }
        }
        cout << sum << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值