HUST 1605 Gene recombination(BFS)

 Gene recombination

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

题目大意是有两种操作,交换串第一二位,和将第一位放到最后一位,使得第一个串经过最少的步骤得到第二个串,共有 CGAT  四种字符 且字符串最长为12

 大致思路是用bfs搜索它达到满足条件的步骤,难点是如果直接开string类型的队列,空间消耗很严重,所以要将串变成4进制数

View Code
 1 # include<stdio.h>
 2 # include<iostream>
 3 # include<string.h>//很奇怪,在VC6.0中不能加.h,否则编译出错,但在HUST上提交时,不加.h会编译出错
 4 # include<algorithm>
 5 # include<queue>
 6 using namespace std;
 7 
 8 struct node
 9 {
10     int time;    //bfs的深度
11     int num;    //4进制数的大小
12 }s;
13 
14 string s1,s2;
15 queue<node>q;  //node类型队列
16 int n;
17 bool m[16777220]; //bfs的标记变量,没有的话会超内存的哦,大小为比4的12次方大一点
18 
19 int change(string s){ //将字符串变成4进制数
20     int i,sum = 0;
21     int k=1;
22     for(i=0;i<s.length();i++)
23     {
24         if(s[i]=='A') k=0;
25         else if(s[i]=='C') k=1;
26         else if(s[i]=='G') k=2;
27         else k=3;
28         sum = sum*4 +k;
29     }
30     return sum;
31 }
32 void bfs()
33 {
34     while(!q.empty()) q.pop();
35     s.num = change(s1);
36     s.time = 0;
37     int num = change(s2);
38     m[s.num] = 1;
39     q.push(s);
40     while(!q.empty()){
41         s = q.front(), q.pop();
42         if(s.num == num)
43         {
44             printf("%d\n",s.time);
45             return;
46         }
47         node temp;
48         temp.time = s.time + 1;
49         //交换第一、二位
50         int a = s.num/(1<<(2*n-2));        //取串的第一位
51         int b = s.num/(1<<(2*n-4))%4;    //取串的第二位
52         temp.num = s.num%(1<<(2*n-4)) + (b*4+a)*(1<<(2*n-4));
53         if(m[temp.num]==0){
54             m[temp.num]=1;
55             q.push(temp);
56         }
57         //将第一位移动到最后一位
58         temp.num = s.num%(1<<(2*n-2))*4 + s.num/(1<<(2*n-2));
59         if(m[temp.num]==0){
60             m[temp.num]=1;
61             q.push(temp);
62         }        
63     }
64 }
65 int main(){
66     while(scanf("%d",&n)!=EOF)
67     {
68         cin >>s1>>s2;
69         if(n==1)
70         {
71             printf("0\n");
72             continue;
73         }
74         memset(m,0,sizeof(m));
75         bfs();
76     }
77     return 0;
78 }

 

你可能感兴趣的:(com)