博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
“浪潮杯”山东省第九届ACM大学生程序设计竞赛(感谢山东财经大学) Anagram
阅读量:3905 次
发布时间:2019-05-23

本文共 1919 字,大约阅读时间需要 6 分钟。

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∣≤50 |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 BACAELLY KRISAAAA ZZZZ

Sample Output

029100

思路:

先排序,因为此题说字母只能通过右移变换来实现变化,因此可以通过将b数组开成2倍,然后遍历即可。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=55;char a[maxn];char b[maxn<<1];int main(){ while(scanf("%s%s",a,b)!=EOF) { int len=strlen(a); int ans=0x3f3f3f3f; sort(a,a+len); sort(b,b+len); for (int i=0;i

 

转载地址:http://ozoen.baihongyu.com/

你可能感兴趣的文章
Oracle中sysdate的时区偏差
查看>>
【每日一算】旋转有序数组
查看>>
【每日一算】两数之和
查看>>
深入理解Mysql索引底层数据结构与算法
查看>>
B+树算法在mysql中能存多少行数据?
查看>>
【vue学习】—条件判断、循环遍历
查看>>
【vue学习】—slot插槽的使用
查看>>
怎样做研究
查看>>
调试串口通用程序的几种技巧
查看>>
GUI 编辑框中读写矩阵
查看>>
matlab成段注释
查看>>
福听阅读器 背景色设置
查看>>
华硕 P5KPL-AM 前面板耳机没有声音
查看>>
labview 局部变量问题
查看>>
labview 循环外部与数组相连时问题
查看>>
哈佛大学凌晨4点半的景象--哈佛图书馆的二十条训言
查看>>
Outlook2010到处通讯录
查看>>
Gmail导入通讯录
查看>>
小米笔记本安装Win 10历程
查看>>
【转】SLAM 论文阅读和分类整理
查看>>