H – Ponk Warshall

发布于 2020-10-09  214 次阅读




H - Ponk Warshall

Listening to the rock music permutes your nuclear DNA. This astonishing and unbelievablefact was recently published in the Rock Nature Weekly, one of the top scientifific journals onthe planet. Part of the research was to take DNA samples from volunteers, both before andafter the rock concerts season. The samples were processed and various genes isolated from thesamples. For each person, each gene was isolated twice: The variant before the rock season andthe variant after the season. These two variants were paired and in many cases one variant wasfound 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 prevalenthypothesis suggests that a permutation is composed of a sequence of transpositions, so-calledswaps. A swap is an event (its chemistry is not fully understood yet) in which exactly twonucleobases in the gene exchange their places in the gene. No other nucleobases in the geneare affffected by the swap. The positions of the two swapped nucleobases might be completelyarbitrary.

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

Input Specification
The input contains two text lines. Each line contains a string of N capital letters “A”, “C”, “G”, or “T”, (1≤N≤106). 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 Specification
Output the minimum number of swaps that transform the first gene version into the second one.

Sample Input
CGATA
ATAGC

Sample Output
2

Sample Input 2
CTAGAGTCTA
TACCGTATAG

Sample Output 2
7
题意:给出一开始字符串a(只含ACGT)和目标字符串b,问使a变成b的最小交换次数
思路:可以证明,所有的最小步数方案可以由以下三种方式组成:

  1. 交换一次,达成两个目标,如A->C,C->A;
  2. 交换两次,达成三个目标,如A->C,C->G,G->T;
  3. 交换三次,达成四个目标,如A->T,T-C,C->G,G->A;

运用模拟即可完成本题。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int d[4][4];
int main() {
	map<int,char> mp;
	int cnt=0;
	string s,s1;
	cin>>s>>s1;
	mp['A']=0;
	mp['G']=1;
	mp['C']=2;
	mp['T']=3;
	int n=s.size();
	for(int i=0;i<n;i++)
		if(s[i]!=s1[i])
			d[mp[s[i]]][mp[s1[i]]]++;
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			 int minn=min(d[i][j],d[j][i]);
			 d[i][j]-=minn;
			 d[j][i]-=minn;
			 cnt+=minn;
		}
	}
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			for(int k=0;k<4;k++)
			{
				int minn=min(d[i][j],d[j][k]);
				minn=min(minn,d[k][i]);
				d[i][j]-=minn;
				d[j][k]-=minn;
				d[k][i]-=minn;
				cnt=cnt+2*minn;
			}
	for(int i=0;i<4;i++)
		cnt=cnt+3*d[i][0];
	cout<<cnt<<endl;
}