【二分图带权匹配】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 的墙上做 ...
 
随机推荐
- 自定义rem
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Codeforces 837F Prefix Sums
			
Prefix Sums 在 n >= 4时候直接暴力. n <= 4的时候二分加矩阵快速幂去check #include<bits/stdc++.h> #define LL l ...
 - js将时间戳转为时间格式
			
时间戳转时间格式 //分钟 let timeM= parseInt(msg/1000/60%60); if(timeM<10){ timeM="0"+timeM; } //秒 ...
 - netty05(netty的一些介绍)
			
netty的一些理论 netty是一个异步事件驱动的网络应用框架(NIO框架),所有IO操作都是异步非阻塞的,NIO是对IO的一个补充 用于开发客户端和服务器的通信(TCP/UDP)长短连接 nett ...
 - F. Shovels Shop   背包DP
			
题意: 商店里有n把铲子 每个铲子有其标价 一个人要买k吧 有m个优惠政策 每个优惠政策有两个元素x,y 表示 正好买x个铲子的时候 这x个铲子中最便宜的y个铲子免单 求用最少的前买到k个铲子 ...
 - 反向传播算法(前向传播、反向传播、链式求导、引入delta)
			
参考链接: 一文搞懂反向传播算法
 - 基于C语言的Socket网络编程搭建简易的Web服务器(socket实现的内部原理)
			
首先编写我们服务器上需要的c文件WebServer.c 涉及到的函数API: int copy(FILE *read_f, FILE * write_f) ----- 文件内容复制的方法 int Do ...
 - RF:RF实现根据乳腺肿瘤特征向量高精度(better)预测肿瘤的是恶性还是良性—Jason niu
			
%RF:RF实现根据乳腺肿瘤特征向量高精度(better)预测肿瘤的是恶性还是良性 load data.mat a = randperm(569); Train = data(a(1:500),:); ...
 - Philosopher’s Walk(递归)
			
In Programming Land, there are several pathways called Philosopher’s Walks for philosophers to have ...
 - 关于pycharm中安装第三方库时报错的解决办法(一)
			
记录自己的生活! 一.事发背景 在pycharm中直接安装第三方库时因为版本问题总是无法安装成功,事情不大,但是很重要. 二.经过 最开始我自己电脑上安装了Python3.6和Python2. ...