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. thinkphp 为什么访问路径错误,还可以访问

    在学习中访问入口文件,实际上应该访问public\index\index\   但其实也可以访问application.admin.controller\index,同样可以在网页下显示 原理:pub ...

  2. Dojo的ready函数:dojo.ready(以前的dojo.addOnLoad)

    dojo的dojo/domReady!插件和dojo/ready的区别:     In simple cases,dojo/domReady! should be used. If an app us ...

  3. 新环境安装 python3

    参考 安装 python3 时,不要覆盖原环境的 python2.因为环境中有些程序是依赖 2 的,比如 yum.直接覆盖是会影响环境的. 最好的是编译安装 python3,执行指令是用 python ...

  4. 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!

    // 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名! var username = document.getElementById("username&quo ...

  5. bug汇总

    bug 2018年8月23日 bug 1:散点图画不出来. plt.scatter(validation_examples["longitude"], validation_exa ...

  6. 洛谷P3372线段树1

    难以平复鸡冻的心情,虽然可能在大佬眼里这是水题,但对蒟蒻的我来说这是个巨大的突破(谢谢我最亲爱的lp陪我写完,给我力量).网上关于线段树的题解都很玄学,包括李煜东的<算法竞赛进阶指南>中的 ...

  7. 【mysql】mysql存储过程实例

    ```mysql DELIMITER $$   DROP PROCEDURE IF EXISTS `system_number_update` $$   CREATE DEFINER=`root`@` ...

  8. python 取余运算

    python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|. 经过测试可发现,pytho ...

  9. 笔记-python-实用-程序运算时间计算

    方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.no ...

  10. luogu1742 最小圆覆盖

    狗题卡我精度--sol #include <algorithm> #include <iostream> #include <cstdlib> #include & ...