比赛 20120703 评测结果 MMMMMMMMMM
题目名称 基因重组 最终得分 0
用户昵称 ZhouHang 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-07-03 11:15:02
显示代码纯文本
/**
*Prob	: genea
*Data	: 2012-7-3
*Sol	: Search + hash
*/

#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>

#define MaxS 20000000

using namespace std;

struct node {
	int k;
	int s[20];
} list[MaxS];
int closd,open;

int n; 
bool v[MaxS];
char tma[20],tmb[20];
int a[20],b[20],tm1[20],tm2[20];

void Change_init()
{
	int tmp = 1; a[n] = b[n] = 0;
	for (int i=n-1; i>=0; i--) {
		a[n] += a[i]*tmp;
		b[n] += b[i]*tmp;
		tmp *= 4;
	}
}
void Change(int *a)
{
	int tmp = 1; a[n] = 0;
	for (int i=n-1; i>=0; i--) {
		a[n] += a[i]*tmp;
		tmp *= 4;
	}
}
void Change1(int *tm1)
{
	int tmp = tm1[0];
	tm1[0] = tm1[1]; tm1[1] = tmp;
}
void Change2(int *tm2)
{
	int tmp = tm2[0];
	for (int i=0; i<n-1; i++)
		tm2[i] = tm2[i+1];
	tm2[n-1] = tmp;
}

int Search()
{
	memset(v,false,sizeof(v));
	list[1].k = 0; v[a[n]] = true;
	for (int i=0; i<=n; i++)
		list[1].s[i] = a[i];
	open = 1; closd = 0;
	
	while (closd<open) {
		closd++;
		for (int i=0; i<=n; i++)
			tm2[i] = tm1[i] = list[closd].s[i];
		Change1(tm1); Change(tm1);
		Change2(tm2); Change(tm2);
		if (tm1[n]==b[n]||tm2[n]==b[n])
			return list[closd].k+1;
		if (!v[tm1[n]]) {
			v[tm1[n]] = true;
			list[++open].k = list[closd].k+1;
			for (int i=0; i<=n; i++)
				list[open].s[i] = tm1[i];
		}
		if (!v[tm2[n]]) {
			v[tm2[n]] = true;
			list[++open].k = list[closd].k+1;
			for (int i=0; i<=n; i++)
				list[open].s[i] = tm2[i];
		}
	}
	
}

int main()
{
	freopen("genea.in","r",stdin);
	freopen("genea.out","w",stdout);
	
	scanf("%d",&n);
	scanf("%s",&tma);
	scanf("%s",&tmb);
	for (int i=0; i<n; i++) {
		if (tma[i]=='A') a[i] = 0; 
		if (tma[i]=='C') a[i] = 1; 
		if (tma[i]=='G') a[i] = 2; 
		if (tma[i]=='T') a[i] = 3; 

		if (tmb[i]=='A') b[i] = 0; 
		if (tmb[i]=='C') b[i] = 1; 
		if (tmb[i]=='G') b[i] = 2; 
		if (tmb[i]=='T') b[i] = 3; 
	}
	Change_init();
	
	int ans;
	if (a[n]==b[n]) ans = 0;
	else ans=Search();
	
	printf("%d\n",ans);
	
	fclose(stdin); fclose(stdout);
	return 0;
}