【二分图带权匹配】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 的墙上做 ...
随机推荐
- 前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)
将: $("input[type=radio][name=optionsContractGroup][value=201]").attr("checked",t ...
- springboot1.5.4 集成cxf完整实例
WebService 服务端 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...
- VM VirtualBox – Cannot register the hard disk
第一打开VirtualBox 文件夹,在地址栏输入cmd 第二, 仔细读下面 VBoxManage.exe internalcommands sethduuid "F:\Virtual ...
- 防止vs编译时自动启动单元测试
Tools → Options → Live Unit Testing Pause 勾选
- day64 django django零碎知识点整理
本文转载自紫金葫芦,哪吒,liwenzhou.cnblog博客地址 简单了解mvc框架和MTV框架, mvc是一种简单的软件架构模式: m----model,模型 v---view,视图 c---co ...
- HTML的5种空格表示
HTML提供了5种空格实体(space entity),它们拥有不同的宽度,非断行空格( )是常规空格的宽度,可运行于所有主流浏览器.其他几种空格( )在不同浏览器中宽度各异. ...
- HDU 1796 How many integers can you find 【容斥】
<题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即 ...
- angular笔记_7
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- UVA 508 Morse Mismatches JAVA
题意:输入字母和数字的编码,输入词典,输入一段编码,求出对应的单词. 思路:来自https://blog.csdn.net/qq_41163933/article/details/82224703 i ...
- Fenng对于管理技术团队的思考
丁香园CTO冯大辉谈技术团队管理 http://www.infoq.com/cn/interviews/fdh-team-manager http://www.geekpark.net/topics/ ...