Ponk Warshall(思维 + 暴力)

链接:https://ac.nowcoder.com/acm/contest/7817/H
来源:牛客网
 

题目描述

Listening to the rock music permutes your nuclear DNA. This astonishing and unbelievable fact was recently published in the Rock Nature Weekly, one of the top scientific journals on the planet. Part of the research was to take DNA samples from volunteers, both before and after the rock concerts season. The samples were processed and various genes isolated from the samples. For each person, each gene was isolated twice: The variant before the rock season and the variant after the season. These two variants were paired and in many cases one variant was found to be some permutation of the other variant in the pair. 

The next step in the research is to determine how the permutations happen. The prevalent hypothesis suggests that a permutation is composed of a sequence of transpositions, so-called swaps. A swap is an event (its chemistry is not fully understood yet) in which exactly two nucleobases in the gene exchange their places in the gene. No other nucleobases in the gene are affected by the swap. The positions of the two swapped nucleobases might be completely arbitrary. 

To predict and observe the movement of the molecules in the permutation process, the re- searchers need to know the theoretical minimum number of swaps which can produce a par- ticular permutation of nucleobases in a gene. We remind you that the nuclear DNA gene is a sequence of nucleobases cytosine, guanine, adenine, and thymine, which are coded as C, G, A, and T, respectively.

输入描述:

The input contains two text lines. Each line contains a string of N capital letters “A”, “C”, “G”,or “T”, (1 ≤ N ≤ 10 6 ). 

The two strings represent one pair of a particular gene versions. 

The first line represents the gene before the rock season, the second line represents the same gene from the same person after the rock season. 

The number of occurrences of each nucleobase is the same in both strings.

输出描述:

Output the minimum number of swaps that transform the first gene version into the second one.

示例1

输入

CGATA
ATAGC

输出

2

示例2

输入

CTAGAGTCTA
TACCGTATAG

输出

7

题意:每次操作可以交换字符串中的两个字母,问a串最少几次操作能变成b串

思路:

只有16种碱基组合,存储每种组合的对数。

首先交换AT TA这种,花费1

其次交换AT TC CA这种,花费2

最后交换AT TC CG GA这种,花费3

因为只有4种碱基,所以一对碱基组合最多交换3次。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 +7;
int cnt[4][4];

int main() {
    map<char, int>mp;
    mp['A'] = 0;
    mp['T'] = 1;
    mp['C'] = 2;
    mp['G'] = 3;
    string a, b;
    cin >> a >> b;
    int n = a.size();
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < n; ++i) {
        if(a[i] == b[i]) continue;
        cnt[mp[a[i]]][mp[b[i]]]++;
    }
    int ans = 0;
    for(int i = 0; i < 4; ++i) {
        for(int j = 0; j < 4; ++j) {
            int minn = min(cnt[i][j], cnt[j][i]);
            cnt[i][j] -= minn;
            cnt[j][i] -= minn;
            ans += minn;
        }
    }
    for(int i = 0; i < 4; ++i) {
        for(int j = 0; j < 4; ++j) {
            for(int k = 0; k < 4; ++k) {
                int x = min({cnt[i][j], cnt[j][k], cnt[k][i]});
                ans += 2 * x;
                cnt[i][j] -= x;
                cnt[j][k] -= x;
                cnt[k][i] -= x;
            }
        }
    }
    for(int i = 0; i < 4; ++i) {
        ans += 3 * cnt[i][0];
    }
    printf("%d\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/108898244