码迷,mamicode.com
首页 > 其他好文 > 详细

hust 1605 - Gene recombination(bfs+字典树)

时间:2014-05-30 04:43:34      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

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深搜加字典树剪枝。
bubuko.com,布布扣
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<string>
  4 #include<cstring>
  5 #include<queue>
  6 using namespace std;
  7 
  8 const int maxn=5000000;
  9 string s1,s2;
 10 struct Trie //字典树查询字符串
 11 {
 12     int ch[maxn][4];
 13     int sz; //结点总数
 14     void clear(){sz=1;memset(ch[0],0,sizeof(ch[0]));}
 15     int Num(char ch)
 16     {
 17         if(ch==A) return 0;
 18         if(ch==C) return 1;
 19         if(ch==G) return 2;
 20         if(ch==T) return 3;
 21     }
 22     void insert(string s)
 23     {
 24         int len=s.size(),u=0;
 25         for(int i=0;i<len;i++)
 26         {
 27             int c=Num(s[i]);
 28             if(!ch[u][c])
 29             {
 30                 memset(ch[sz],0,sizeof(ch[sz]));
 31                 ch[u][c]=sz++;
 32             }
 33             u=ch[u][c];
 34         }
 35     }
 36     int Find(string s)
 37     {
 38         int u = 0,len=s.size();
 39         for(int i = 0; i < len; i++)
 40         {
 41             int c = Num(s[i]);
 42             if(!ch[u][c]) return 0;
 43             u = ch[u][c];
 44         }
 45         return 1;
 46     }
 47 }trie;
 48 struct point //队列数据
 49 {
 50     string s;
 51     int step;
 52 };
 53 void swap(char &a,char &b)
 54 {
 55     char t=a;
 56     a=b;
 57     b=t;
 58 }
 59 string change1(string s)
 60 {
 61     swap(s[0],s[1]);
 62     return s;
 63 }
 64 string change2(string s)
 65 {
 66     char ch=s[0];
 67     s.erase(s.begin());
 68     s+=ch;
 69     return s;
 70 }
 71 void solve()
 72 {
 73     trie.clear();
 74     queue<point> Q;
 75     point p,t;
 76     t.s=s1;t.step=0;
 77     trie.insert(t.s);
 78     Q.push(t);
 79     while(!Q.empty())
 80     {
 81         t=Q.front();Q.pop();
 82         p.step=t.step+1;
 83         p.s=change1(t.s);
 84         if(!trie.Find(p.s))
 85         {
 86             if(p.s==s2)
 87             {
 88                 printf("%d\n",p.step);
 89                 return ;
 90             }
 91             Q.push(p);
 92             trie.insert(p.s);
 93         }
 94         p.s=change2(t.s);
 95         if(!trie.Find(p.s))
 96         {
 97             if(p.s==s2)
 98             {
 99                 printf("%d\n",p.step);
100                 return ;
101             }
102             Q.push(p);
103             trie.insert(p.s);
104         }
105     }
106     return ;
107 }
108 int main()
109 {
110     int n;
111     while(~scanf("%d",&n))
112     {
113         cin>>s1>>s2;
114         if(s1==s2)
115         {
116             printf("0\n");
117             continue;
118         }
119         solve();
120     }
121     return 0;
122 }
bubuko.com,布布扣

 

hust 1605 - Gene recombination(bfs+字典树),布布扣,bubuko.com

hust 1605 - Gene recombination(bfs+字典树)

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/xiong-/p/3756980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!