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

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深搜加字典树剪枝。
  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 }

 

转载于:https://www.cnblogs.com/xiong-/p/3756980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值