记录编号 124779 评测结果 AAAAAAAAAT
题目名称 基因重组 最终得分 90
用户昵称 GravatarHouJikan 是否通过 未通过
代码语言 C++ 运行时间 4.196 s
提交时间 2014-10-05 22:44:33 内存使用 0.22 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============
struct Node{
  string gene;
  int depth;
  bool swaped;
};
//==============var declaration=================
string cur,tar;
int len,depth;
queue <Node> Q;
set <string> Exist;
//==============function declaration============

//==============main code=======================
int main()
{  
  string FileName="genea";//程序名 
  string FloderName="cogs";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif    
  ios::sync_with_stdio(false);
  cin>>len>>cur>>tar;
  Node x;
  x.gene=cur;x.depth=0;x.swaped=false;
  Q.push(x);
  while (!Q.empty()){
    x=Q.front();Q.pop();
    if (x.gene==tar){
      cout<<x.depth<<endl;
      break;
    }
    if (x.swaped){
      Node ins;
      ins.gene=x.gene.substr(1,len-1)+x.gene[0];
      ins.swaped=false;
      ins.depth=x.depth+1;
      if (!Exist.count(ins.gene)){
        Q.push(ins);
        Exist.insert(ins.gene);
      }
    }
    else{
      Node ins;
      ins.gene=x.gene.substr(1,len-1)+x.gene[0];
      ins.swaped=false;
      ins.depth=x.depth+1;
      if (!Exist.count(ins.gene)){
        Q.push(ins);
        Exist.insert(ins.gene);
      }
      swap(x.gene[0],x.gene[1]);
      x.depth++;
      x.swaped=true;
      if (!Exist.count(x.gene)){
        Q.push(x);
        Exist.insert(x.gene);
      }
    }
  }
#ifdef DEBUG  
  clock_t End_Time=clock();
  printf("\n\nTime Used: %.4lf Ms\n",double(End_Time-Start_Time)/CLOCKS_PER_SEC);
#endif    
  return 0;
}
//================fuction code====================