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
水题 字符串匹配
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { string s1,s2; int a[60]; while(cin>>s1>>s2){ int minn = 100,cnt = 0,k = 0,ans = 0; memset(a,0,sizeof(a)); int l = s1.length(); for(int i=0;i<l;i++) { minn = 100; for(int j=0;j<l;j++){ if(a[j] == 0){ if( s1[i] <= s2[j]){ cnt = (s2[j]-'A') - (s1[i]-'A'); } else{ cnt = 26 - ((s1[i]-'A') - (s2[j]-'A')); } if(cnt < minn){ minn = cnt; k = j; } } } ans += minn ; a[k] = 1; } cout<<ans<<endl; } return 0; }