【二分图带权匹配】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 的墙上做 ...
随机推荐
- Android开发——使用Jword生成本地word文档
本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下. 为什么使用Jword呢?因为IText .Freemark在安卓平台上压 ...
- Linux批量清空当前目录中的日志文件
在Linux中,有时需要批量清空当前目录中的日志文件,同时还要保留日志文件. 其实一行shell命令就可以搞定,一起来看看吧. 在当前目录下,键入如下命令: for i in `find . -nam ...
- 集群LVS
集群分为LB负载均衡集群,HA高可用集群,LB高应用集群. 前两种比较常见 LB是更加注重性能处理速度,而HA注重是服务器的在线时间. HA集群一般设有主重,当主服务器当掉时候,重服务器进行工作,此时 ...
- BZOJ3796 Mushroom追妹纸 字符串 SA KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/9253173.html 题目传送门 - BZOJ3796 题意 找一个串 $w$ 满足: 1.$w$ 是 $s_ ...
- 02. Pandas 1|数据结构Series、Dataframe
1."一维数组"Series Pandas数据结构Series:基本概念及创建 s.index . s.values # Series 数据结构 # Series 是带有标签的一 ...
- 使用loadrunner录制脚本的思路和注意要点
基本思路如下图: 注意要点有如下几点: 1.性能测试往往需要准备大批量的数据,大批量数据的生成方法有很多种,常见的有: (1)编写SQL语句来插入数据 (2)使用DataFactory等专业的数据生成 ...
- ActiveMQ在windows下启动失败解决方案
activemq.xml文件中的 <transportConnectors> <!-- DOS protection, limit concurrent connections to ...
- Bi-shoe and Phi-shoe (欧拉函数)
题目描述: 题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一 ...
- Java Lambda基础——Function, Consumer, Predicate, Supplier, 及FunctionalInterface接口
这几个接口经常与Lambda结合使用,网上当然也有很多介绍,不过有些过于繁琐,有些又偏简单,秉着实用主义精神,今天这里折中一下,把介绍的内容分为两部分,第一部分相当于TLDR,总结几个"口诀 ...
- superset链接本地mysql数据库
刚安装好superset的时候大家都知道是用的其自动生成的sqllite数据库,如果我们想让器链接到自己数据库,给大家分享一下我的方法,以mysql为例: 1.安装好数据库mysql: $ sudo ...