标签:des c style class blog code
Time Limit: 2s Memory Limit: 64MB
4 ATCC CTCA 4 ATCG GCTA 4 ATCG TAGC
2 4 6
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 }
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