前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HUST 1605 Gene recombination(广搜,位运算)

HUST 1605 Gene recombination(广搜,位运算)

作者头像
ShenduCC
发布2018-04-26 11:53:31
6010
发布2018-04-26 11:53:31
举报
文章被收录于专栏:算法修养

题目描述 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

代码语言:javascript
复制
#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\n",ans);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档