1605 - Gene recombination

Time Limit: 2s Memory Limit: 64MB

Submissions: 264 Solved: 46
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+字典树)的更多相关文章

  1. HUST 1605 Gene recombination(广搜,位运算)

    题目描述 As a gene engineer of a gene engineering project, Enigma encountered a puzzle about gene recomb ...

  2. HUST 1605 Gene recombination

    简单广搜.4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态. #include<cstdio> #include<cstring> #include<cm ...

  3. nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...

  4. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  5. 算法笔记--字典树(trie 树)&& ac自动机 && 可持久化trie

    字典树 简介:字典树,又称单词查找树,Trie树,是一种树形结构,是哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较. 性质:根节点不包含字符,除根节点外每一个 ...

  6. 【AC自动机】【字符串】【字典树】AC自动机 学习笔记

    blog:www.wjyyy.top     AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维.     AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...

  7. LA_3942 LA_4670 从字典树到AC自动机

    首先看第一题,一道DP+字典树的题目,具体中文题意和题解见训练指南209页. 初看这题模型还很难想,看过蓝书提示之后发现,这实际上是一个标准DP题目:通过数组来储存后缀节点的出现次数.也就是用一颗字典 ...

  8. HDU5589:Tree(莫队+01字典树)

    传送门 题意 略 分析 f[u]表示u到根的边的异或 树上两点之间的异或值为f[u]^f[v], 然后将查询用莫队算法分块,每个点插入到字典树中,利用字典树维护两点异或值大于等于M复杂度O(N^(3/ ...

  9. ACM之路(15)—— 字典树入门练习

    刷的一套字典树的题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120748#overview 个人喜欢指针的字典树写法,但是大力 ...

随机推荐

  1. docker单主机网络

    当你安装Docker时,它会自动创建三个网络.你可以使用以下docker network ls命令列出这些网络: [root@localhost ~]# docker network ls NETWO ...

  2. 主成分分析法(PCA)答疑

    问:为什么要去均值? 1.我认为归一化的表述并不太准确,按统计的一般说法,叫标准化.数据的标准化过程是减去均值并除以标准差.而归一化仅包含除以标准差的意思或者类似做法.2.做标准化的原因是:减去均值等 ...

  3. Python 消息框的处理

    在实际系统中,在完成某些操作时会弹出对话框来提示,主要分为"警告消息框","确认消息框","提示消息对话"三种类型的对话框. 1.警告消息框 ...

  4. 类库日期和jsp导包

    一.日期类库 1.1. Date Date类创建一个时间,或者是创建一个与你计算机当前的时间:精确到毫秒. //实例化时间类 Date date = new Date(); 1.2.格式转换类 1.2 ...

  5. ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作

    UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...

  6. 八皇后问题(DFS)

    题目描述: 要在国际象棋棋盘中放八个皇后,使任意两个皇后都不能互相吃,皇后能吃同一行.同一列,同一对角线上(两个方向的对角线)的任意棋子.现在给一个整数n(n<=92),输出前n种的摆法. 输入 ...

  7. Ansible的使用和模块化深入

    Ansible配置 配置文件:/etc/ansible/ansible.cfg [default] 默认配置 inventory = /etc/ansible/hosts主机清单 library = ...

  8. vue 使用lib-flexable,px2rem 进行移动端适配 但是引入的第三方UI组件 vux 的样式缩小,解决方案

    最近在写移动端项目,就想用lib-flexable,px2rem来进行适配,把px转换成rem但是也用到了第三方UI组件库vux,把这个引入发现一个问题就是vux的组件都缩小了,在网上找不到答案,最后 ...

  9. java中常用的集合的一些区别 (2013-10-07-163写的日志迁移

    java中的以下几大集合: List结构的集合类: ArrayListl类,LinkedList类,Vector类,stack类 Map结构的集合类: HashMap类,Hashtable类(此是以k ...

  10. Python中变量的作用域

    一.变量作用域的含义 变量的作用域说白了就是变量的值从哪里获取,或者说变量取值的地方 我们在写代码过程中会用到很多变量,这些变量会出现在各种代码块中,有的出现在函数块里,有的在函数块外,例如: def ...