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 个人喜欢指针的字典树写法,但是大力 ...
随机推荐
- BCB:AnsiString BSTR WideString
WideString wstr;AnsiString astr;wchar_t *wp;//或者 BSTR wp; wp=wstr.c_bstr(); //WideString转化为BSTRwstr= ...
- javaweb基础(19)_jsp标签
一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...
- Bootstrap历练实例:可取消的警告
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- linux :没有找到 ifconfig netstat
linux :没有找到 ifconfig netstat ubuntu sudo apt install net-tools -y centos yum install net-tools
- k8s的flannel网络插件配置
flannel的网络插件配置 Kubernetes网络通信需要解决以下问题: (1)容器间通信:同一个Pod内的多个容器间的通信,lo (2)Pod通信:P ...
- 【android】【android studio】修改emulator的本地化环境
Changing the emulator locale from the adb shell To change the locale in the emulator by using the ad ...
- python模块之pickle
和json不同的是: json只支持str,int,tuple,list,dict. pickle支持python里所有的数据类型,但是只能在python里序列化,不跨平台,python独有. 代码示 ...
- Linux学习-透过 systemctl 管理服务
透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...
- 求1+2+...+n 【微软面试100题 第十二题】
题目要求: 要求不能使用乘除法,for/while/if/else/switch/case等关键字以及条件判断语句(A?B:C). 参考资料:剑指offer第46题 题目分析: 方法1:利用类的静态成 ...
- 命令行客户端操作pg数据库常用操作
登录 # su - postgres -c "psql" 或者 $psql -U user_name -d database_name -h serverhost psql (10 ...