比赛 20120703 评测结果 AAAAAAAAAA
题目名称 基因重组 最终得分 100
用户昵称 Citron酱 运行时间 0.333 s
代码语言 C++ 内存使用 19.16 MiB
提交时间 2012-07-03 11:19:47
显示代码纯文本
#include <cstdio>
#include <climits>
 
#define I_F "genea.in"
#define O_F "genea.out"
 
const short MAXn=12;
const long MAXs=16777216;
 
//A 0
//C 1
//G 2
//T 3
 
struct que
{
    long s;
    short d;
    que *next;
};
 
short n;
short a[MAXn],b[MAXn];
short ans;
long T[MAXn];
bool f[MAXs]={false};
 
void GetT();
void Input();
long Exchange(const short*);
void Exchange(const long&, short*);
void Work(const short*, short*, const short&);
bool Compare(const short*, const short*);
void Search();
void Output();
 
int main()
{
    GetT();
    Input();
    Search();
    Output();
    return 0;
}
 
void GetT()
{
    T[0]=1;
    for (short i=1; i<MAXn; ++i)
        T[i]=T[i-1]*4;
}
 
void Input()
{
    char t;
    freopen(I_F,"r",stdin);
    scanf("%hd\n",&n);
    for (short i=0; i<n; ++i)
    {
        scanf("%c",&t);
        if (t=='A')
            a[i]=0;
        else if (t=='C')
            a[i]=1;
        else if (t=='G')
            a[i]=2;
        else if (t=='T')
            a[i]=3;
    }
    scanf("\n");
    for (short i=0; i<n; ++i)
    {
        scanf("%c",&t);
        if (t=='A')
            b[i]=0;
        else if (t=='C')
            b[i]=1;
        else if (t=='G')
            b[i]=2;
        else if (t=='T')
            b[i]=3;
    }
}
 
long Exchange(const short *s)
{
    long x=0;
    for (short i=0; i<n; ++i)
        x+=s[i]*T[i];
    return x;
}
 
void Exchange(const long &x, short *s)
{
    long t=x;
    for (short i=0; i<n; ++i)
    {
        s[i]=t%4;
        t/=4;
    }
}
 
void Work(const short *a, short *b, const short &x)
{
    if (x==0)
    {
        for (short i=2; i<n; ++i)
            b[i]=a[i];
        b[0]=a[1];
        b[1]=a[0];
    }
    else
    {
        for (short i=0; i<n-1; ++i)
            b[i]=a[i+1];
        b[n-1]=a[0];
    }
}
 
bool Compare(const short *a, const short *b)
{
    for (short i=0; i<n; ++i)
        if (a[i]!=b[i])
            return false;
    return true;
}
 
void Search()
{
    que *head, *tail, *temp;
    short t1[MAXn], t2[MAXn];
    long t;
    head=new que;
    head->s=Exchange(a);
    head->d=0;
    f[head->s]=tail=head;
    head->next=NULL;
    while (head!=NULL)
    {
        Exchange(head->s,t1);
        for (short i=0; i<2; ++i)
        {
            Work(t1,t2,i);
            t=Exchange(t2);
            if (f[t]==false)
            {
            	f[t]=true;
                tail->next=new que;
                tail=tail->next;
                tail->s=t;
                tail->d=head->d+1;
                tail->next=NULL;
                if (Compare(t2,b))
                {
                    ans=tail->d;
                    return;
                }
            }
        }
        temp=head;
        head=head->next;
        delete temp;
    }
}
 
void Output()
{
    freopen(O_F,"w",stdout);
    printf("%hd\n",ans);
}