题目描述

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\n",ans);
}
return 0;
}

HUST 1605 Gene recombination(广搜,位运算)的更多相关文章

  1. hust 1605 - Gene recombination(bfs+字典树)

    1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...

  2. HUST 1605 Gene recombination

    简单广搜.4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态. #include<cstdio> #include<cstring> #include<cm ...

  3. UVA 565 565 Pizza Anyone? (深搜 +位运算)

      Pizza Anyone?  You are responsible for ordering a large pizza for you and your friends. Each of th ...

  4. UVa12726 one Friend at a Time (位 广搜)

    题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...

  5. php实现不用加减乘除号做加法(1、善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍)

    php实现不用加减乘除号做加法(1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍) 一.总结 1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍 二.ph ...

  6. 模拟赛T5 : domino ——深搜+剪枝+位运算优化

    这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...

  7. POJ 3220 位运算+搜索

    转载自:http://blog.csdn.net/lyhypacm/article/details/5813634 DES:相邻的两盏灯状态可以互换,给出初始状态.询问是否能在三步之内到达.如果能的话 ...

  8. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  9. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

随机推荐

  1. 错误 Unable to find vcvarsall.bat 的终极无敌最完美的解决办法

    Windows 上通过 pip 安装 python 包,经常会出现这种错误. 如:pip install pyodbc. 这种错误的简单明了解释就是:python 编译器找不到计算机上面的 VC 编译 ...

  2. node.js--Less

    摘要: 现在已经有许多站点使用Node.js,所以在Node.js上配置Less环境也是很重要的,下面分享下如何在Node上使用Less开发,前提是你电脑上已经安装node. 安装: 只需要执行下面一 ...

  3. Oauth2.0(一):为什么需要 Oauth2.0 协议?

    假设有两家互联网企业 A 和 B,其中 B 是一家提供相片云存储的公司.即 B 的用户可以把相片上传到 B 网站上长期保存,然后可以在不同的设备上查看.某一天,A 和 B 谈成了一项合作:希望 B 用 ...

  4. js实现webSocket客户端

    var ws = new WebSocket("ws://localhost:8080/msg"); //readyState属性返回实例对象的当前状态,共有四种. //CONNE ...

  5. 5 -- Hibernate的基本用法 --3 Hibernate的体系结构

    ⊙ SessionFactory : 这是Hibernate的关键对象,它是单个数据库映射关系经过编译后的内存镜像,也是线程安全的.它是生成Session的工厂,本身需要依赖于ConnectionPr ...

  6. PHP开发中,让var_dump调试函数输出更美观 ^_^#

    前提:php必须安装Xdebug模块. 用var_dump打印输出时,输出的内容没有被格式化.如下图: 通常使用var_dump打印的内容是被格式化后输出的,如下图: 造成没有格式化输出的原因是因为p ...

  7. xml文件的序列化示例

    1.创建activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...

  8. mongodb int字段的一个小坑

    在使用 php mongodb 搜索时,如果字段类型用 int,则使用 php 搜索时一定要把数值转换成整型来搜索,用字符串类型的数字搜索是没有结果的!!!! $condition = ['membe ...

  9. Redis 未授权访问漏洞(附Python脚本)

    0x01 环境搭建 #下载并安装 cd /tmp wget http://download.redis.io/releases/redis-2.8.17.tar.gz tar xzf redis-.t ...

  10. Android开发-- Genymotion模拟器

    模拟器安装 http://blog.csdn.net/beiminglei/article/details/13776013 连接ADB http://android3g.diandian.com/p ...