Uva - 12545 - Bits Equalizer


刚開始想了一个变换顺序,模拟进行操作。写了浩浩荡荡200多行最后WA了,感觉还是方法不正确。。。后来从仅仅能0变1,不能1变0入手,应该先满足上1下0的情况,其它的三种情况用简单的变换就能够了。

先用0-1和1-0进行配对。配对一次。交换S中的0和1。

假设没有0-1了,那就用?-1和1-0进行配对。先把?

换成0,再运行上一步。

假设0-1和?

-1都没有了,可是1-0还有,说明无解。

把上面的1配对成功之后。其它的都能够通过一次操作配对。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

const int maxn = 105;
int oneVzero, zeroVone, qVone, qVzero;
int ans;
string S, T;

void solve()
{
	for (int i = 0; i < S.length(); i++) {
		if (S[i] == '1' && T[i] == '0') {
			oneVzero++;
		}
		else if (S[i] == '0' && T[i] == '1') {
			zeroVone++;
		}
		else if (S[i] == '?' && T[i] == '0') {
			qVzero++;
		}
		else if (S[i] == '?' && T[i] == '1') {
			qVone++;
		}
	}

	while (oneVzero && zeroVone) { // 交换1-0和0-1
		oneVzero--;
		zeroVone--;
		ans++;
	}
	while (oneVzero && qVone) { // 交换1-0和?-1
		oneVzero--;
		qVone--;
		ans += 2;
	}
	if (oneVzero) {
		ans = -1;
	}
	else {
		ans += zeroVone + qVone + qVzero;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	int C;
	cin >> C;
	int kase = 0;
	while (C--) {
		cin >> S >> T;

		// 初始化
		ans = 0;
		oneVzero = zeroVone = qVzero = qVone = 0;
		solve();
		cout << "Case " << ++kase << ": " << ans << endl;
	}

	return 0;
}




版权声明:本文博主原创文章。博客,未经同意不得转载。

posted on 2015-10-12 17:35  gcczhongduan  阅读(141)  评论(0编辑  收藏  举报