山東省第九屆ACM大學生程序設計競賽 A題 Anagram 貪心

Anagram

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

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.

Input

There will be multiple test cases. For each test case:
There is two strings A and B in one line.|A| = |B| \leq 50∣A∣=∣B∣≤50. A and B will contain only uppercase letters from the English alphabet ('A'-'Z').

Output

For each test case, output the minimal number of operations.

Sample Input

ABCA BACA
ELLY KRIS
AAAA ZZZZ

Sample Output

0
29
100

簡單貪心,每次取距離最小的

//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1105;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

int n;
int mp[maxn][maxn];
int used[maxn], mat[maxn];
int fd, ans;
bool Find(int x){
    rep(i, 1, n){
        if(mp[x][i] != 0 && mp[x][i] >= fd && !used[i]){
            used[i] = true;
            if(!mat[i] || Find(mat[i])){
                mat[i] = x;
                return true;
            }
        }
    }
    return false;
}
int Match(){
    ms(mat, 0);
    int cnt = 0;
    rep(i, 1, n){
        ms(used, 0);
        if(Find(i))
            cnt++;
    }
    return cnt;
}

int main()
{
    std::ios::sync_with_stdio(false);
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
    cin >> n;
    int l = INF, r = -INF;
    rep(i, 1, n){
        rep(j, 1, n){
            cin >> mp[i][j];
            l = min(mp[i][j], l);
            r = max(mp[i][j], r);
        }
    }
    fd = 0;
    int cnt = Match();

    while(l <= r){
        int mid = (l+r)/2;
        fd = mid;
        if(Match() == cnt){
            ans = mid;
            l = mid+1;
        }
        else{
            r = mid-1;
        }
    }
    cout <<ans <<endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章