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\n",ans);
}
return 0;
}
HUST 1605 Gene recombination(广搜,位运算)的更多相关文章
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- HUST 1605 Gene recombination
简单广搜.4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态. #include<cstdio> #include<cstring> #include<cm ...
- UVA 565 565 Pizza Anyone? (深搜 +位运算)
Pizza Anyone? You are responsible for ordering a large pizza for you and your friends. Each of th ...
- UVa12726 one Friend at a Time (位 广搜)
题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...
- php实现不用加减乘除号做加法(1、善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍)
php实现不用加减乘除号做加法(1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍) 一.总结 1.善于寻找资源:去搜为什么位运算可以实现加法,里面讲的肯定要详细一万倍 二.ph ...
- 模拟赛T5 : domino ——深搜+剪枝+位运算优化
这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...
- POJ 3220 位运算+搜索
转载自:http://blog.csdn.net/lyhypacm/article/details/5813634 DES:相邻的两盏灯状态可以互换,给出初始状态.询问是否能在三步之内到达.如果能的话 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
随机推荐
- [原] unity3d动态加载脚本
本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public class TestDll : MonoBehaviour { void Start () { ...
- 分表需要解决的问题 & 基于MyBatis 的轻量分表落地方案
分表:垂直拆分.水平拆分 垂直拆分:根据业务将一个表拆分为多个表. 如:将经常和不常访问的字段拆分至不同的表中.由于与业务关系密切,目前的分库分表产品均使用水平拆分方式. 水平拆分:根据分片算法将一个 ...
- An internal error occurred during: "Launching xxx on WebLogic10.x".
An internal error occurred during: "Launching xxx on WebLogic10.x". java.lang.NullPointerE ...
- str.split()与re.split()的区别
str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...
- python 学习笔记---文件处理
1.打开文件读取数据 f =open(“wenjian.txt”,"r") print(f) f.close() 直接变成列表--->list(f) for each_lin ...
- python --葵花宝典
1.python 函数 定义 ---def() def fun(): print(“我是小甲鱼!!”) 调用函数 a =fun() print (a) 即可 ,注:函数调用是由上而下: 2.pyt ...
- oracle闪回数据
方法一 数据删除了: select * from t_test as of timestamp to_timestamp('2011-10-25 13:45:00','yyyy-mm-dd hh2 ...
- KindEditor上传图片无法使用绝对路径
之前百度,一直查到的都是urlType使用domain,但是根本没有效果.想着去插件代码里面看,但是实在看不下去了. 最后还是百度去了.然后查到下面的一个方法.直接将其中的某部分代码注释到就好了.具体 ...
- Free Download Manager (FDM) 中文版 - 替代迅雷最佳免费开源下载工具软件
https://www.freedownloadmanager.org/ Free Download Manager (FDM) 是一款经典免费纯粹的下载软件,它开源无广告,界面简洁清爽,支持 BT. ...
- OGG日常运维监控的自动化脚本模板
#!/usr/bin/ksh export ORACLE_BASE=/oracle/ export ORACLE_SID=epmln1 export ORACLE_HOSTNAME=pmlnpdb1 ...