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;
    }
    
    
  • 相关阅读:
    Auto.js 入门教程
    几乎纯css实现弹出框
    php curl访问https 域名接口一直报错的问题
    金山打字通刷打字速度脚本
    centos 安装 图像识别工具 tesseract-ocr 流程
    js 实现俄罗斯方块(三)
    小程序报错 .wxss 无法找到
    Redis存储数组
    问题记录-databinding/hilt踩坑记录
    问题记录-CoordinatorLayout+WebView使用遇到的问题
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228782.html
Copyright © 2011-2022 走看看