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 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 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 ≤ 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 the minimum number of swaps that transform the first gene version into the second one.样例输入 Copy
CGATA ATAGC
样例输出 Copy
2
这个题就是问你从上面的串变成下面的串,最少操作几次(调换两个)
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<map> using namespace std; typedef long long ll; const int maxn=1e6+100; char a[maxn],b[maxn]; int c[1000][1000]; map<char,int>mp; int main(){ mp['A']=0; mp['T']=1; mp['C']=2; mp['G']=3; scanf("%s",a); scanf("%s",b); int len=strlen(a); for(int i=0;i<len;i++){ if(a[i]!=b[i]) c[mp[a[i]]][mp[b[i]]]++; } int ans=0; for(int i=0;i<=3;i++){ for(int j=i;j<=3;j++){ int t=min(c[i][j],c[j][i]); c[i][j]-=t; c[j][i]-=t; ans+=t; } } for(int i=0;i<=3;i++){ for(int j=0;j<=3;j++){ for(int k=0;k<=3;k++){ int z=min(c[i][j],min(c[j][k],c[k][i])); c[i][j]-=z; c[j][k]-=z; c[k][i]-=z; ans+=2*z; } } } for(int i=0;i<=3;i++){ for(int j=0;j<=3;j++){ for(int k=0;k<=3;k++){ for(int z=0;z<=3;z++){ int p=min(c[i][j],min(c[j][z],min(c[z][k],c[k][i]))); c[i][j]-=p; c[j][z]-=p; c[z][k]-=p; c[k][i]-=p; ans+=3*p; } } } } cout<<ans<<endl; }