题干:

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 BACA
ELLY KRIS
AAAA ZZZZ

Sample Output

0
29
100

Hint

解题报告:

  因为数据量小,直接跑KM就可以了。(当然好像这题也可以贪心解)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
#include <iomanip>
using namespace std;
const int MAX = 2e5 + 5;
const int inf = 0x3f3f3f3f;
struct node {
	int to,c,w,ne;
} e[50005<<2];
int n,m;
int N;
int head[MAX],d[MAX],vis[MAX],tot,p[MAX];
void add(int u,int v,int c,int cost=0) {
	e[++tot].to = v;e[tot].c = c;e[tot].w = cost;e[tot].ne = head[u];head[u] = tot;
	e[++tot].to = u;e[tot].c = 0; e[tot].w = -cost;e[tot].ne = head[v];head[v] = tot;
}
bool bfs(int s,int t) {
	for(int i = 0; i<=N; i++) d[i]=inf,vis[i]=0;
	d[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty()) {
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u]; ~i; i=e[i].ne) {
			int j=e[i].to;
			if(e[i].c&&d[j]>d[u]+e[i].w) {
				d[j]=d[u]+e[i].w;
				p[j]=i;
				if(!vis[j])vis[j]=1,q.push(j);
			}
		}
	}
	return d[t]<inf;
}
int MCMF(int s,int t,int &flow) {
	ll ans=0;
	while(bfs(s,t)) {
		int x=t,f=inf;
		while(x!=s) {
			f = min(f,e[p[x]].c),x=e[p[x]^1].to;
		}
		flow += f;
		ans+=1LL*d[t]*f;
		x=t;
		while(x!=s) {
			e[p[x]].c-=f,e[p[x]^1].c+=f;
			x=e[p[x]^1].to;
		}
	}
	return ans;
}
int db[55][55];
char s1[MAX],s2[MAX];
int main() 
{
	for(int i = 'A'; i<='Z'; i++) {
		for(int j = 'A'; j<='Z'; j++) {
			int x=i-'A'+1,y=j-'A'+1;
			db[x][y] = x<=y?y-x:y-x+26;
		}
	}
	while(~scanf("%s%s",s1+1,s2+1)) {
		tot=1;
		memset(head,-1,sizeof(head));
		n = strlen(s1+1);
		N=2*n+12;
		int st=2*n+1,ed=2*n+2;
		for(int i = 1; i<=n; i++) add(st,i,1);
		for(int i = 1; i<=n; i++) add(n+i,ed,1);
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				add(i,n+j,1,db[s1[i]-'A'+1][s2[j]-'A'+1]);
			}
		}
		int ans2=0;
		int ans=MCMF(st,ed,ans2);
		printf("%d\n",ans);
	}
	return 0 ;
}