【二分图带权匹配】Anagram @山东省第九届省赛 A
题目描述
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
左串看作左部节点,右串看作右部节点,权值就是按题意转化的代价
求最小权匹配。(KM算法,权值取反即可)
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int weight[maxn][maxn];
int lx[maxn], ly[maxn];
bool vx[maxn], vy[maxn];
int match[maxn];
int n, delta;
int trans(char a,char b){
if(a<=b)return b-a;
return 26-(a-b);
}
bool search_path(int u) {
vx[u] = true;
for(int v = 1; v <= n; v++)
if(!vy[v]) {
if(lx[u] + ly[v] == weight[u][v]) {
vy[v] = true;
if(!match[v] || search_path(match[v])) {
match[v] = u;
return true;
}
} else
delta = min(delta, lx[u] + ly[v] - weight[u][v]);
}
return false;
}
int Kuhn_Munkras(bool max_weight) {
memset(match,0,sizeof match);
if(!max_weight)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
weight[i][j] = -weight[i][j];
for(int i = 1; i <= n; i++) {
ly[i] = 0;
lx[i] = -0x7fffffff;
for(int j = 1; j <= n; j++)
lx[i] = max(lx[i], weight[i][j]);
}
for(int u = 1; u <= n; u++)
while(true) {
memset(vx, 0, sizeof vx);
memset(vy, 0, sizeof vy);
delta = 0x7fffffff;
if(search_path(u))
break;
for(int i = 1; i <= n; i++) {
if(vx[i])
lx[i] -= delta;
if(vy[i])
ly[i] += delta;
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
ans += weight[match[i]][i];
if(!max_weight)
ans = -ans;
return ans;
}
int main() {
// IN_LB();
char stringa[maxn], stringb[maxn];
while(scanf("%s%s", stringa, stringb) != EOF) {
int len = strlen(stringa);
n = len;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
weight[i][j] = trans(stringa[i-1],stringb[j-1]);
}
printf("%d\n", Kuhn_Munkras(false));
}
return 0;
}
【二分图带权匹配】Anagram @山东省第九届省赛 A的更多相关文章
- 二分图带权匹配、最佳匹配与KM算法
---------------------以上转自ByVoid神牛博客,并有所省略. [二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和 ...
- 二分图带权匹配 KM算法与费用流模型建立
[二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- [poj3565] Ants (二分图带权匹配)
传送门 Description 年轻自然主义者比尔在学校研究蚂蚁. 他的蚂蚁以苹果树上苹果为食. 每个蚁群都需要自己的苹果树来养活自己. 比尔有一张坐标为 n 个蚁群和 n 棵苹果树的地图. 他知道蚂 ...
- 二分图带权匹配-Kuhn-Munkres算法模板 [二分图带权匹配]
尴尬...理解不太好T T #include<cstdio> #include<cstring> #include<iostream> #include<al ...
- 【二分图最大匹配】Bullet @山东省第九届省赛 B
时间限制: 6 Sec 内存限制: 128 MB 题目描述 In GGO, a world dominated by gun and steel, players are fighting for t ...
- 【容斥】Four-tuples @山东省第九届省赛 F
时间限制: 10 Sec 内存限制: 128 MB 题目描述 Given l1,r1,l2,r2,l3,r3,l4,r4, please count the number of four-tuples ...
- hdu 1569 &1565 (二分图带权最大独立集 - 最小割应用)
要选出一些点,这些点之间没有相邻边且要求权值之和最大,求这个权值 分析:二分图带权最大独立集. 用最大流最小割定理求解.其建图思路是:将所有格点编号,奇数视作X部,偶数视作Y部,建立源点S和汇点T, ...
- nyoj1273 河南省第九届省赛_"宣传墙"、状压DP+矩阵幂加速
宣传墙 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 ALPHA 小镇风景美丽,道路整齐,干净,到此旅游的游客特别多.CBA 镇长准备在一条道路南 面 4*N 的墙上做 ...
随机推荐
- C# 之 HttpResponse 类
Response 对象,派生自HttpResponse 类,该类封装来自 ASP.NET 操作的 HTTP 响应信息.存在于System.Web命名空间下. 注:MIME(Multipurpose I ...
- 【AtCoder】AGC013
AGC013 A - Sorted Arrays 直接分就行 #include <bits/stdc++.h> #define fi first #define se second #de ...
- 在启用了“编辑并继续”时,修改包含 lambda 表达式的“method”将会阻止调试会话继续进行
将所有的引用的“复制到本地”属性都设置成false就可以了
- PUTTY工具的使用
Putty工具包简单使用 一.Putty简介 Putty是一款远程登录工具,用它可以非常方便的登录到Linux服务器上进行各种操作(命令行方式).Putty完全免费,而且无需安 装(双击即可运行),支 ...
- 删除了原有的offset之后再次启动会报错park Streaming from Kafka has error numRecords must not ...
笔者使用Spark streaming读取Kakfa中的数据,做进一步处理,用到了KafkaUtil的createDirectStream()方法:该方法不会自动保存topic parti ...
- Codeforces 653F Paper task SA
Paper task 如果不要求本质不同直接st表二分找出最右端, 然后计数就好了. 要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦. #include<bits/stdc++.h& ...
- Codeforces 746F Music in Car
Music in Car 用两个Set维护一下尺取的过程. #include<bits/stdc++.h> #define LL long long #define fi first #d ...
- PHP远程下载图片,微信头像存到本地,本地图片转base64
方法一(推荐): function download_remote_pic($url){ $header = [ 'User-Agent: Mozilla/5.0 (Windows NT 6.1; W ...
- 使用aws中国的s3时,制订bucket poicy时注意注意……
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Pub ...
- 033 Url中特殊字符的处理
在url跳转页面的时候,参数值中的#不见了,一直没有处理,今天有空看了一下,后来发现后台的过滤器之类的都没有处理,就比较奇怪了,原来是特殊字符的问题. 一:Url中的特殊字符 1.说明 这里还是需要做 ...