比赛 20160421s 评测结果 AAATTTTTTT
题目名称 病毒编码 最终得分 30
用户昵称 Fmuckss 运行时间 7.019 s
代码语言 C++ 内存使用 1.07 MiB
提交时间 2016-04-21 11:44:49
显示代码纯文本
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1e5;
const int inf = 1e9;

int test[maxn];
int data[maxn];
int n, m;

int get_line(int *a) {
	int i = 0;
	char tmp = getchar();
	while(tmp != '0' && tmp != '1') tmp = getchar();
	while(tmp == '1' || tmp == '0') {
		a[i++] = tmp - '0';
		tmp = getchar();
	}
}

void read() {
	scanf("%d %d", &m, &n);
	get_line(data);
	get_line(test);
	m += 2;
}

int cnt, res;

int judge(int x1, int x2, int c) {
	int num = (x2*2-1) * c;
	num %= n;
	int ans = 0;
	for(int i = 0; i < n; i++) {
		if(test[i] != data[(i + num + n) % n] ^ x1) ans++;
		if(cnt + ans >= res) return inf;
	}
	return ans;
}

int pow[32];

void get_pow() {
	for(int i = 1; i <= m; i++) {
		pow[i] = 1 << (i-1);
	}
}

int get_zip(int num) {
	int tot = 0;
	for(int i = 1; i <= m; i++) {
		if(pow[i] & num) tot++;
	}
	return tot;
}

void solve() {
	int top = (1 << m) -1;
	int tmp1 = 1 << (m-1);//x1
	int tmp2 = 1 << (m-2);//x2
	int x1, x2, c;
	res = inf;
	for(int i = 1; i <= top; i++) {
		x1 = (i & tmp1) ? 1 : 0;
		x2 = (i & tmp2) ? 1 : 0;
		c = i & (top - tmp1) & (top - tmp2);
		cnt = 0;
		cnt = get_zip(i);
		if(cnt >= res) continue;
		int tmp = judge(x1, x2, c);
		res = min(res, tmp + cnt);
	}
	printf("%d\n", res);
}

int main() {
	freopen("virusb.in", "r", stdin);
	freopen("virusb.out", "w", stdout);
	read();
	get_pow();
	solve();
	return 0;
}