zoukankan      html  css  js  c++  java
  • HUST 1605 Gene recombination(广搜,位运算)

    题目描述
    As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recombination. It is well known that a gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T.
    Enigma has gotten a gene, such as “ATCC”. And he wants to reorder this gene to make a new one, like “CTCA”. He can use two types of operations: (1) exchange the first two letters, or (2) move the first letter to the end of the gene. For example, “ATCC” can be changed to “TCCA” by operation (2), and then “TCCA” can be changed to “CTCA” by operation (1). Your task is to make a program to help Enigma to find out the minimum number of operations to reorder the gene.
    输入
    The input contains several test cases. The first line of a test case contains one integer N indicating the length of the gene (1<=N<=12). The second line contains a string indicating the initial gene. The third line contains another string which Enigma wants.
    Note that the two strings have the same number for each kind of letter.
    输出
    For each test case, output the minimum number of operations.
    样例输入
    4
    ATCC
    CTCA
    4
    ATCG
    GCTA
    4
    ATCG
    TAGC
    样例输出
    2
    4
    6

    求最短的路径,所以可以用广搜,如果你单纯用字符串处理,和map无疑会超时。因为只有四个字符,所以我们可以进行四进制压缩,两个操作均可以用位运算操作来完成
    这里推荐一篇博客关于位运算
    http://blog.csdn.net/Dacc123/article/details/50974579

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <map>
    #include <queue>
    
    using namespace std;
    char a[15];
    char b[15];
    map<char,int> m;
    map<int,int>M;
    int c,d,e;
    int n;
    queue<int> Q;
    int x,y;
    int ans;
    void BFS(int x)
    {
        c=((1<<(n+n))-1)^15;
        d=3;e=12;
        Q.push(x);
        int term=0,temp=0;
        while(!Q.empty())
        {
            int num=Q.front();
            Q.pop();
            if(!(num^y))
            {
                ans=M[num];
                return;
            }
            term=(num&c)|((num&d)<<2)|((num&e)>>2);
            temp=(num>>2)|((num&d)<<(n+n-2));
            int sum=M[num]+1;
            if(!M[term])
            {
                M[term]=sum;
                Q.push(term);
            }
            if(!M[temp])
            {
                M[temp]=sum;
                Q.push(temp);
            }
    
        }
    }
    int main()
    {
        m['A']=0;m['T']=1;m['C']=2;m['G']=3;
        while(scanf("%d",&n)!=EOF)
        {
            scanf("%s%s",a,b);
            x=0;y=0;
            for(int i=n-1;i>=0;i--)
            {
                x=(x<<2)|m[a[i]];
                y=(y<<2)|m[b[i]];
            }
            M.clear();
            M[x]=0;
            while(!Q.empty())
                Q.pop();
            BFS(x);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    005.Kickstart部署多系统
    004.Kickstart部署之FTP架构
    003.Kickstart部署之HTTP架构
    C#并发编程之异步编程(二)
    设计模式之策略者模式
    设计模式之职责链模式
    C#并发编程之异步编程(一)
    C#并发编程之概述
    微服务探索与实践—总述
    设计模式之模板方法模式
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228782.html
Copyright © 2011-2022 走看看