zoukankan      html  css  js  c++  java
  • hust 1605

    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
    ",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
    ",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
    ");
    117             continue;
    118         }
    119         solve();
    120     }
    121     return 0;
    122 }
  • 相关阅读:
    readelf的使用,看函数地址
    Linux 打印堆栈和crash地址转换
    Android mk 添加打印信息
    Linux make file文件中常用的一些定义CPP CXX
    关于Android中RemoveView的错误理解
    Android悬浮窗注意事项
    在有EditText控件的AlertDialog对话框中自动弹出输入法
    联系旭日150安装CentOS5.X版本手记
    ListView配合CheckBox出现position位置错误的解决
    在Android中让Preference的宽度占满整个屏幕的宽度
  • 原文地址:https://www.cnblogs.com/xiong-/p/3756980.html
Copyright © 2011-2022 走看看