zoukankan      html  css  js  c++  java
  • 【二分图带权匹配】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
    ", Kuhn_Munkras(false));
        }
        return 0;
    }
  • 相关阅读:
    C++ 抽象类二(抽象类的基本语法)
    C++ 抽象类一(多继承与赋值兼容性原则)
    C++ 类的多态五(多态的语法本质分析)
    C++ 类的多态四(虚析构函数的重要性)
    C++ 类的多态三(多态的原理--虚函数指针--子类虚函数指针初始化)
    MEF等Ioc框架引起内存泄露-PartCreationPolicy
    MEF部件的生命周期(PartCreationPolicy)
    wpf prism IRegionManager 和IRegionViewRegistry
    silverlight开发实例(Prism+MVVM+RIA)(二)--创建shell及用户登录
    Prism框架 如何在主程序中合理的弹出子窗体
  • 原文地址:https://www.cnblogs.com/NeilThang/p/9356607.html
Copyright © 2011-2022 走看看