hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination
Time Limit: 2s Memory Limit: 64MB
- DESCRIPTION
- 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. - INPUT
- 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. - OUTPUT
- For each test case, output the minimum number of operations.
- SAMPLE INPUT
-
4
ATCC
CTCA
4
ATCG
GCTA
4
ATCG
TAGC - SAMPLE OUTPUT
-
2
4
6 - 题目大意:给两个长度不超过十二的字符串S1,S2(由A、C、G、T组成),有两种操作:1.交换第一个跟第二个字母。2.把第一个移到字符串的末尾。
- 求最少的操作次数S1==S2。
- 分析:用BFS深搜加字典树剪枝。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
using namespace std; const int maxn=;
string s1,s2;
struct Trie //字典树查询字符串
{
int ch[maxn][];
int sz; //结点总数
void clear(){sz=;memset(ch[],,sizeof(ch[]));}
int Num(char ch)
{
if(ch=='A') return ;
if(ch=='C') return ;
if(ch=='G') return ;
if(ch=='T') return ;
}
void insert(string s)
{
int len=s.size(),u=;
for(int i=;i<len;i++)
{
int c=Num(s[i]);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
ch[u][c]=sz++;
}
u=ch[u][c];
}
}
int Find(string s)
{
int u = ,len=s.size();
for(int i = ; i < len; i++)
{
int c = Num(s[i]);
if(!ch[u][c]) return ;
u = ch[u][c];
}
return ;
}
}trie;
struct point //队列数据
{
string s;
int step;
};
void swap(char &a,char &b)
{
char t=a;
a=b;
b=t;
}
string change1(string s)
{
swap(s[],s[]);
return s;
}
string change2(string s)
{
char ch=s[];
s.erase(s.begin());
s+=ch;
return s;
}
void solve()
{
trie.clear();
queue<point> Q;
point p,t;
t.s=s1;t.step=;
trie.insert(t.s);
Q.push(t);
while(!Q.empty())
{
t=Q.front();Q.pop();
p.step=t.step+;
p.s=change1(t.s);
if(!trie.Find(p.s))
{
if(p.s==s2)
{
printf("%d\n",p.step);
return ;
}
Q.push(p);
trie.insert(p.s);
}
p.s=change2(t.s);
if(!trie.Find(p.s))
{
if(p.s==s2)
{
printf("%d\n",p.step);
return ;
}
Q.push(p);
trie.insert(p.s);
}
}
return ;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
cin>>s1>>s2;
if(s1==s2)
{
printf("0\n");
continue;
}
solve();
}
return ;
}
hust 1605 - Gene recombination(bfs+字典树)的更多相关文章
- HUST 1605 Gene recombination(广搜,位运算)
题目描述 As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recomb ...
- HUST 1605 Gene recombination
简单广搜.4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态. #include<cstdio> #include<cstring> #include<cm ...
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
- POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]
题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...
- 算法笔记--字典树(trie 树)&& ac自动机 && 可持久化trie
字典树 简介:字典树,又称单词查找树,Trie树,是一种树形结构,是哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较. 性质:根节点不包含字符,除根节点外每一个 ...
- 【AC自动机】【字符串】【字典树】AC自动机 学习笔记
blog:www.wjyyy.top AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维. AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...
- LA_3942 LA_4670 从字典树到AC自动机
首先看第一题,一道DP+字典树的题目,具体中文题意和题解见训练指南209页. 初看这题模型还很难想,看过蓝书提示之后发现,这实际上是一个标准DP题目:通过数组来储存后缀节点的出现次数.也就是用一颗字典 ...
- HDU5589:Tree(莫队+01字典树)
传送门 题意 略 分析 f[u]表示u到根的边的异或 树上两点之间的异或值为f[u]^f[v], 然后将查询用莫队算法分块,每个点插入到字典树中,利用字典树维护两点异或值大于等于M复杂度O(N^(3/ ...
- ACM之路(15)—— 字典树入门练习
刷的一套字典树的题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120748#overview 个人喜欢指针的字典树写法,但是大力 ...
随机推荐
- Nginx: ubuntu系统上如何判断是否安装了Nginx?
问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...
- Windows平台下MySQL常用操作与命令
Windows平台下MySQL常用操作与命令 Windows平台下MySQL常用操作与命令,学习mysql的朋友可以参考下. 1.导出整个数据库 mysqldump -u 用户名 -p --defau ...
- kvm笔记
1 virt-manager安装虚拟机无法使用键盘解决 今天远程用VNC登录服务器安装虚拟机,结果使用virt-manager安装虚拟机后在初始阶段无法使用键盘设置,这不雪崩了,后来来回试,找到了原因 ...
- 不依赖Hibernate的万能BaseDao---模仿了Hibernate底层的原理
今天写了个万能的BaseDao:有了这个BaseDao以后的Dao层直接继承这个BaseDao就能直接操作数据库了,增删改查,这是一个简易的Hibernate模型.写这个BaseDao的原因是最近在学 ...
- thinkcmf常用标签
1.图片地址:{:cmf_get_image_url($vo.icon)} 2.模板控件 模板变量调用:$theme_vars.title <widget name="aboutUs& ...
- 2.什么是composer与packgist,composer的安装
目录 学习地址: composer与packgist关系图片 composer的安装; 配置composer 修改国内镜像 用composer安装与卸载插件 composer插件升级后报错 学习地址: ...
- python标准输入输出
input() 读取键盘输入 input() 函数从标准输入读入一行文本,默认的标准输入是键盘. input 可以接收一个Python表达式作为输入,并将运算结果返回. print()和format( ...
- 安装好的IIS,发布成功后打开网站出现错误
开发web项目时需要安装IIS,在安装好IIS的Windows7本上发布asp.net网站时,web程序已经映射到了本地IIS上,但运行如下错误提示“处理程序“PageHandlerFactory ...
- BZOJ 5336: [TJOI2018]party
状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...
- java处理excel
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...