比赛 20120703 评测结果 AAAAAAAAAA
题目名称 基因重组 最终得分 100
用户昵称 zhengtn03 运行时间 0.683 s
代码语言 C++ 内存使用 64.31 MiB
提交时间 2016-02-21 10:38:18
显示代码纯文本
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<sstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
#define ll long long
#define INF 2000000000

char word1[50];
char word2[50];
int state1;
int state2;
int n;

int getState(char word[], int n)
{
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		char c = word[i];
		int num;
		if (c == 'A')
		{
			num = 0;
		}
		else if (c == 'C')
		{
			num = 1;
		}
		else if (c == 'G')
		{
			num = 2;
		}
		else num = 3;
		ans <<= 2;
		ans += num;
	}
	return ans;
}

int hash1[16777217];

int BFS()
{
	int ans = 0;
	hash1[state1] = 1;
	queue<int> que;
	que.push(state1);
	que.push(-1);
	while (1)
	{
		int state = que.front();
		que.pop();
		if (state == state2)
		{
			return ans;
		}
		if (state == -1)
		{
			ans++;
			que.push(-1);
			continue;
		}
		int temp1 = ((state >> (n - 1)) & 1);
		int temp2 = ((state >> (n - 2)) & 1);
		int tempstate = state << 2;
		tempstate &= ~(1 << (n + 1));
		tempstate &= ~(1 << n);
		tempstate &= ~3;
		tempstate |= temp1 << 1;
		tempstate |= temp2;
		if (hash1[tempstate] == 0)
		{
			hash1[tempstate] = 1;
			que.push(tempstate);
		}
		int temp3 = ((state >> (n - 3)) & 1);
		int temp4 = ((state >> (n - 4)) & 1);
		tempstate = state;
		tempstate &= ~(1 << (n - 1));
		tempstate &= ~(1 << (n - 2));
		tempstate &= ~(1 << (n - 3));
		tempstate &= ~(1 << (n - 4));
		tempstate |= temp1 << (n - 3);
		tempstate |= temp2 << (n - 4);
		tempstate |= temp3 << (n - 1);
		tempstate |= temp4 << (n - 2);
		if (hash1[tempstate] == 0)
		{
			hash1[tempstate] = 1;
			que.push(tempstate);
		}
	}
}

int main()
{
	freopen("genea.in", "r", stdin);
	freopen("genea.out", "w", stdout);
	scanf("%d", &n);
	scanf("%s%s", word1, word2);
	state1 = getState(word1, n);
	state2 = getState(word2, n);
	if (state1 == state2)
	{
		printf("%d\n", 0);
		return 0;
	}
	n *= 2;
	int ans = BFS();
	printf("%d\n", ans);
	return 0;
}