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代表不能走的格 子.之 ...
随机推荐
- Java动态代理和静态代理区别
静态代理 package staticproxy; /** * 接口 * @author newtouch * */ public interface IHello { public void say ...
- spring boot工程打成JAR包到服务器上运行
只需在项目的pom.xml中加入下面插件 <build> <plugins> <plugin> <groupId>org.springframework ...
- 【动态规划】数字分组I
[动态规划]数字分组I 时间限制: 1 Sec 内存限制: 64 MB提交: 10 解决: 6[提交][状态][讨论版] 题目描述 给出一堆魔法石的重量,问如何分成两堆,使得它们质量和之差最小,求 ...
- Go错误处理(二)
1,.error接口的定义 type error interface{ Error()string } 2.error的使用 func Foo(param int)(n int,err error){ ...
- Docker应用之容器
容器是独立运行的一个或一组应用,以及他们的运行态环境 1.启动容器(基于镜像新建一个容器并启动或将终止状态的容器重新启动) run后面添加--name参数可以指定容器的名称,否则系统默认会给名称:使用 ...
- LR 测试http协议xml格式数据接口
Action() { lr_start_transaction("T1"); web_custom_request("xxxxHTTPRequest", &qu ...
- 采用Post方式提交数据实例
项目目录 一.编写MainActivity.java package com.hyzhou.getdemo; import com.hyzhou.getdemo.service.LoginServer ...
- CentOS7--Firewalld防火墙
Firewalld服务是红帽RHEL7系统中默认的防火墙管理工具,特点是拥有运行时配置与永久配置选项且能够支持动态更新以及"zone"的区域功能概念,使用图形化工具firewall ...
- date 类型转为varchar
select t.type_id as typeId, t.type_name as typeName, t.type_order as typeOrder, t.type_link as typeL ...
- Excel 导入遍历
package com.founder.ec.cms.service.impl; import com.founder.ec.cms.service.ProductListImportService; ...