题干:

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 ;
}

【2018山东省赛 - A】Anagram(贪心,费用流,KM算法)相关推荐

  1. 洛谷 - P4014 分配问题(费用流/KM)

    题目链接:点击查看 题目大意:给出n个工人和n个工作,每个人做每一个工作的效率都是不同的,问如何分配能让效率最低/最高 题目分析:最小费用最大流和最大费用最大流的模板题,直接套模板跑答案就行了,没有任 ...

  2. 图论 —— 网络流 —— 费用流 —— MCMF 算法

    [概述] EK 算法是每次用广搜寻找一条最短的增广路,然后沿其增广,而 MCMF 算法是在 EK 算法的基础上,每次用 SPFA 计算图的距离标号,然后沿着可行边进行增广,即将 EK 算法中的 bfs ...

  3. 一种更高效的费用流算法——zkw费用流

    orz原创者zkw%%% 送上zkw神犇的blog原址:传送门 费用流建立在网络最大流的基础上,一张图中最大流有且仅有一个,但是最大流条数往往不止一条,这时候对于我们来说,可能要找出这些最大流中最小( ...

  4. 网络流 费用流 模板 ISAP+SPFA+ZKW

    2020年4月20日重新发布.7年前的文章,几年前CSDN改版的时候变成了私密--重新发一下吧. 关于费用流ZKW算法的讲解:从入门到精通: 最小费用流的"zkw算法" 关于Din ...

  5. 图论-网络流⑦-费用流解题

    图论-网络流⑦-费用流解题 上一篇:图论-网络流⑥-费用流 下一篇:图论-网络流⑧-有上下界的网络流 参考文献: https://www.luogu.com.cn/blog/user9012/solu ...

  6. UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)

    题目链接 http://uoj.ac/contest/47/problem/455 题解 模拟费用流,一个非常神奇的东西. 本题即为WC2019 laofu的讲课中的Problem 8,经典的老鼠进洞 ...

  7. 【贪心+堆/模拟费用流增广】BZOJ4946 [NOI2017]蔬菜

    一道思路很好的题,因为篇幅太长赶时间,以下多数转自这里 [题目] 定义了一种蔬菜为: a i , s i , c i , x i a_i,s_i,c_i,x_i ai​,si​,ci​,xi​,有 n ...

  8. 【贪心 / 线段树模拟费用流增广】BZOJ4977 [Lydsy八月月赛] 跳伞求生

    [题目] 原题地址 有nn个队友和mm个敌人,每个队友有一个攻击力aia_i,每个敌人有攻击力bib_i和价值cic_i.你可以选择若干个队友,每个队友ii分别去怼一个敌人jj,当ai>bja_ ...

  9. 费用流 -- 四川省赛F-Direction Setting [拆边成点+费用流]

    题目链接 题目大意: 就是给你一个nnn个点mmm条边的无向图,每个点有个值是aia_iai​现在你把每条边都赋予一个方向之后这个图就是有向图了,那么设第iii个点的入度是did_idi​,现在设 D ...

最新文章

  1. 选择排序-直接选择排序
  2. 面试常问的29个linux命令
  3. 产品经理第一课上海站圆满结束,下一站你定!
  4. python之.py生成.exe可执行文件
  5. 如何成为数据型产品经理
  6. VS开发中的代码编写小技巧——避免重复代码编写的几种方法
  7. MTK 驱动(85)----RPMB key introduction
  8. 跨域——vue中的axios.post使用json数据传输,出现请求头字段内容类型是不被允许的情况的解决方案
  9. linux ipv4文件,Linux IPV4 IPV6地址批量Ping脚本(工具)
  10. AutoCAD Civil 3D 2015-2020
  11. python tkinter:单位换算小工具完整代码
  12. 最简单的易班打卡脚本
  13. android mvp知乎,安卓日记——MVP重构知乎日报
  14. 兆,字节,位等单位转换
  15. 如何使用iAd在应用程序中展示Banne…
  16. 淘宝补单的一些见解和经验分享,仅供参考
  17. 使用命令行强制注销远程登录用户
  18. mysql怎样发给别人_怎么把数据库发给别人
  19. 【启示录】资源分配体系
  20. 《JavaEE开发技术》课程考试试题(A卷)

热门文章

  1. 第五章数理统计--样本和抽样分布
  2. [Leedcode][JAVA][第560题][和为K的子数组][Hashmap][数组]
  3. [Leedcode][JAVA][第202题][快乐数]
  4. 职业中专计算机基础试讲课,职业中专计算机基础教育分析
  5. 使用ping命令检查路由之解惑
  6. 使用Jedis源码生成Jedis.jar
  7. python文本风格_以写代学:python 良好的代码风格实例解析
  8. python开发视频播放器_Python应用03 使用PyQT制作视频播放器实例
  9. 基于 Android NDK 的学习之旅-----序言
  10. ubuntu12.04samba服务器配置