列表

详情


NC209444. PonkWarshall

描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 63ms, 内存消耗: 4128K, 提交时间: 2020-10-01 15:00:43

#include<bits/stdc++.h>
using namespace std;
int arr[200][200];
int main(){
	string s1,s2;
	char ch[4]={'A','T','C','G'};
	long long num=0;
	cin>>s1>>s2;
	int n=s1.size();
	for(int i=0;i<s1.size();i++){
		if(s1[i]==s2[i]){
			n--;
			continue;
		} 
		arr[s1[i]][s2[i]]++;
	}
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			int ls=min(arr[ch[i]][ch[j]],arr[ch[j]][ch[i]]);
			num+=ls;
			arr[ch[i]][ch[j]]-=ls;
			arr[ch[j]][ch[i]]-=ls;
			n-=2*ls;
		}
	}
//	cout<<n<<endl;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			for(int k=0;k<4;k++){
				int ls=arr[ch[i]][ch[j]];
				ls=min(ls,arr[ch[j]][ch[k]]);
				ls=min(ls,arr[ch[k]][ch[i]]);
				arr[ch[j]][ch[k]]-=ls;
				arr[ch[i]][ch[j]]-=ls;
				arr[ch[k]][ch[i]]-=ls;
				num+=2*ls;
				n-=3*ls;
			}
		}
	}
//	cout<<n<<endl;
	num+=n*3/4;
	cout<<num<<endl;
}

C++11(clang++ 3.9) 解法, 执行用时: 12ms, 内存消耗: 2296K, 提交时间: 2020-10-03 23:59:15

#include<bits/stdc++.h>
using namespace std;
const int nx=1e6+5;
char a[nx],b[nx];
int mp[128],cnt[4][4];
char s[4]={'A','C','G','T'};
int main(){
	scanf("%s%s",a,b);
	int n=strlen(a);
	for(int i=0;i<4;++i)mp[s[i]]=i;
	for(int i=0;i<n;++i)
		if(a[i]!=b[i])++cnt[mp[a[i]]][mp[b[i]]];
	int ans=0,now;
	for(int i=0;i<4;++i)
		for(int j=i;j<4;++j){
			now=min(cnt[i][j],cnt[j][i]);
			cnt[i][j]-=now,cnt[j][i]-=now;
			ans+=now; 
		}
	for(int i=0;i<4;++i)
		for(int j=0;j<4;++j)
			for(int k=0;k<4;++k){
				now=min({cnt[i][j],cnt[j][k],cnt[k][i]});
				cnt[i][j]-=now,cnt[j][k]-=now,cnt[k][i]-=now;
				ans+=2*now;
			}
	for(int i=0;i<4;++i)ans+=3*cnt[i][0];
	printf("%d",ans);
}

pypy3(pypy3.6.1) 解法, 执行用时: 852ms, 内存消耗: 24728K, 提交时间: 2020-10-01 16:49:23

from itertools import permutations
a, b = input(), input()
n = len(a)
cnt = {}
for i in range(n):
    if (a[i], b[i]) in cnt:
        cnt[(a[i], b[i])] += 1
    else:
        cnt[(a[i], b[i])] = 1
ans = 0
for k in range(2, 5):
    for c in permutations("ACGT", k):
        o = tuple(list(c)[1:] + [c[0]])
        minc = min([cnt.get(x, 0) for x in zip(c, o)])
        ans += minc * (k - 1)
        for zi in zip(c, o):
            if zi in cnt:
                cnt[zi] -= minc
print(ans)

上一题