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. BCB:使用CppWebBrowser判断网页加载完成

    void __fastcall TForm1::CppWebBrowser1DocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *U ...

  2. opensuse 15.0 安装ctdb

    问题 1 2019/05/20 15:27:14.574363 ctdb-eventd[26329]: 60.nfs: /etc/ctdb/nfs-linux-kernel-callout: line ...

  3. Launch Instance---source for openstack

    If you want to create an instance that uses ephemeral storage, meaning the instance data is lost whe ...

  4. ajax的dataType有哪些类型?

    ajax的dataType有哪些类型? 格式为:dataType:"xxx", •"xml": 返回 XML 文档,可用 jQuery 处理 •"ht ...

  5. C11 C语言文件的读写

    目录 文件的打开和关闭 字符流读写文件 文件的打开和关闭 fopen( ) fopen( ) 函数来创建一个新的文件或者打开一个已有的文件,这个调用会初始化类型 FILE 的一个对象,类型 FILE ...

  6. NOIP模拟赛 路面修整

    [题目描述] FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N段,N个 ...

  7. C#基础-数组

    数组定义 定义数组并赋值 int[] scores = { 45, 56, 78, 98, 100 }; //在定义数组时赋值 for(int i = 0; i < scores.Length; ...

  8. docker镜像下载

    获得CentOS的Docker CE 预计阅读时间: 10分钟 要在CentOS上开始使用Docker CE,请确保 满足先决条件,然后 安装Docker. 先决条件 Docker EE客户 要安装D ...

  9. token验证机制

    最近在vue-cli项目实现登录的过程中用到了token验证,在此总结如下 1. 登录时,客户端通过用户名与密码请求登录 2. 服务端收到请求去验证用户名与密码 3. 验证通过,服务端会签发一个Tok ...

  10. DAOMYSQLI工具类

    <?php //DAOMySQLI.class.php //完成对mysql数据库操作,单例模式 //开发类 //1. 定类名 //2. 定成员属性 //3. 定成员方法[查询,dml操作] f ...