zoukankan      html  css  js  c++  java
  • Anagram

    问题 A: Anagram

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 89  解决: 53
    [提交][状态][讨论版][命题人:admin]

    题目描述

    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 test case:
    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.

    样例输入

    ABCA BACA
    ELLY KRIS
    AAAA ZZZZ
    

    样例输出

    0
    29
    100

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        char str1[55];
        char str2[55];
        while(scanf("%s",str1)!=EOF)
        {
            scanf("%s",str2);
            int len =strlen(str1);
            int a[26]= {0};
            int b[26]= {0};
            for(int i=0; i<len; i++)
            {
                a[str1[i]-'A']++;
                b[str2[i]-'A']++;
            }
            for(int i=0; i<26; i++)
            {
                int t = min(a[i],b[i]);
                a[i]-=t;
                b[i]-=t;
            }
            long long int ans = 0;
            for(int i=0; i<26; i++)
            {
                while(a[i])
                {
                    for(int j=i+1;i!=j%26; j++)
                    {
                        if(b[j%26]!=0)
                        {
                            if(i<=j%26)
                            {
                                ans+=j%26-i;
                               // cout<<j%26-i<<endl;
                            }
                            else
                            {
                                ans+=26-i+j%26;
                               // cout<<26-i+j%26<<endl;
                            }
                            b[j%26]--;
                            break;
                        }
    
                    }
                    a[i]--;
                }
            }
            printf("%lld
    ",ans);
        }
    }

    注意最优解为无序的替换

     
  • 相关阅读:
    Commons.net FTPClient 上传文件
    C盘空间不够,清除VS下的 Font Cache
    Redis 密码设置
    Window bat expdp 定时任务逻辑备份 定时删除N天前的旧文件
    Windows下修改Oracle默认的端口1521
    Intellij idea 乱码问题(英文操作系统)
    给VMware下的Linux扩展磁盘空间(以CentOS6.3为例)转
    TortoiseSVN and TortoiseGit 版本控制图标不见了
    R语言中字符串的拼接操作
    SparkR:数据科学家的新利器
  • 原文地址:https://www.cnblogs.com/hao-tian/p/9080578.html
Copyright © 2011-2022 走看看