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实现在复制文件时使用进度条
在对大文件操作时,可能会需要些时间,此时为用户提供进度条提示是非常常见的一项功能,这样用户就可以了解操作文件需要的时间信息.本实例为大家介绍了在复制大的文件时使用的进度条提示,需要注意的是,只有在读取 ...
- Java -- 异常的捕获及处理 -- throws与throw关键字
7.2 throws 与 throw关键字 7.2.1 throws 关键字 在定义一个方法时可以使用throws关键字声明,使用throws声明的方法标识此方法不处理异常,而交给方法的调用处进行处理 ...
- SpringMVC -- 梗概--源码--壹--收参
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- 解决select2在modal中无法输入的问题
解决办法: 在js里加一句 $.fn.modal.Constructor.prototype.enforceFocus = function(){};
- 【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟
using UnityEngine; using System.Linq; using System.Collections.Generic; [RequireComponent(typeof(Cha ...
- warning C4305:“初始化”:从“double”到“float”截断
编译VS项目时出现警告: warning C4305:“初始化”:从“double”到“float”截断(warning C4305: 'initializing' : truncation from ...
- linux下使用ftp传递文件的shell脚本
使用ftp传递文件,传递过程中防止对方取文件,后缀名为writing,传完后再改回来. #!/bin/bash dstpath=cnet ftpip="127.0.0.1" log ...
- struts1的配置文件详解11111
要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...
- <转>ML 相关算法参考
转自 国内外网站如果你想搜索比较新颖的机器学习资料或是文章,可以到以下网站中搜索,里面不仅包括了机器学习的内容,还有许多其它相关领域内容,如数据科学和云计算等.InfoWord:http://www. ...
- Linux(Ubuntu)下也能用搜狗输入法了!!!
Ubuntu原生的中文输入法是不是总有点别扭? 不用再别扭了. 告诉你一个好消息:Linux(Ubuntu)下也能用搜狗输入法了!!! 下载地址:http://pinyin.sogou.com/lin ...