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)(1 ≤ N ≤ 10^6)(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.

样例输入1

CGATA
ATAGC

样例输出1

2

样例输入2

CTAGAGTCTA
TACCGTATAG

样例输出2

7

大致题意:

给出两个字符串,并且字符串中只包含四种字母:CTAG,题意里指的四种碱基,当然这不重要,然后要求设计程序求出第一个字符串最少经过多少次两两交换可以变成第二个字符串。

大致思路:

第一种字符串有四种情况:

1:本身就上下对应,比如样例一中就有A是给出的时候就是对应的,这种情况在输入的时候continue即可,不需要考虑他
2:交换一次,消掉两个字母,比如样例一的输出就是这么得来的,交换两次消掉了四个字母。
3:交换两次,消掉三个。
4:交换三次,消掉四个。
样例二的输出就是由第一个字符串进行两次情况三,再进行一次情况四,消掉了全部字母,2+2+3即得7

那么对于这四种情况,处理顺序是不一样的,首先第一种情况是直接跳过的,从第二种开始,显然第二种要优于第三种,第三种要优于第四种。所以处理顺序就是先找出所有的第二种情况,然后找所有的第三种情况,最后剩下的就都是第四种情况。

在储存数据的时候我选择了一个二维数组,numbers[4][4],来表示第一个字符串和第二个字符串每个字母的对应关系。

下面是AC代码:

#include 
using namespace std;
int numbers[4][4];
mapdir;
int FFF(char a,char b,char c)
{
    int ans=0;
    int x=(min(numbers[dir[a]][dir[b]],min(numbers[dir[b]][dir[c]],numbers[dir[c]][dir[a]])));
    int y=(min(numbers[dir[a]][dir[c]],min(numbers[dir[b]][dir[a]],numbers[dir[c]][dir[b]])));
    ans+=2*(x+y);
    numbers[dir[a]][dir[b]]-=x;
    numbers[dir[b]][dir[c]]-=x;
    numbers[dir[c]][dir[a]]-=x;
    numbers[dir[a]][dir[c]]-=y;
    numbers[dir[b]][dir[a]]-=y;
    numbers[dir[c]][dir[b]]-=y;
    return ans;
}
int main()
{
    dir['A']=0;dir['C']=1;dir['G']=2;dir['T']=3;
    char check[4]={'A','C','G','T'};
    for(int i=0;i<4;i++)for(int j=0;j<4;j++)numbers[i][j]=0;
    string A,B;cin>>A>>B;
    int len=A.size();
    int ans=0;
    for(int i=0;i=numbers[j][i])
            {
                numbers[i][j]-=numbers[j][i];
                ans+=numbers[j][i];
                numbers[j][i]=0;
            }
            else
            {
                numbers[j][i]-=numbers[i][j];
                ans+=numbers[i][j];
                numbers[i][j]=0;
            }
        }
    }
    for(int i=0;i<2;i++)
    {
        for(int j=i+1;j<3;j++)
        {
            for(int k=j+1;k<4;k++)
            {
                ans+=FFF(check[i],check[j],check[k]);
            }
        }
    }
    for(int i=1;i<4;i++)
    {
        ans+=3*numbers[0][i];
    }
    cout<

你可能感兴趣的:(个人题解,字符串)