D. Let‘s Play the Words?(思维威威威威我i)

这题真的…想通了是个智障题,没想通思路巨尼玛复杂

, 00 , 11 , 01 , 10 串的本质之和开头结尾的字符相关,也就是只有00,11,01,10四种

关键来了,00和11是很特殊的,他们对当前局面不会有任何影响

01 10 , 00 11 只要存在01或10串,那么00和11就一定可以放下去且不会影响局面

01 10 , 00 11 , 若不存在01和10,且同时存在00和11,显然无法放置

00 11 , 01 10 ? \color{Red}那么接下来不就无视00和11,只考虑01和10吗?

01 10 1 当01串和10串相差不超过1时满足条件

, 所以遍历一遍数组,每次翻转能翻转且次数较多的那个串

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int t,n,l[maxn];
string s[maxn];
map<string,int>mp;
vector<int>vec;
int main()
{
	cin >> t;
	while( t-- )
	{
		mp.clear(); vec.clear();
		cin >> n;
		int zero=0,one=0,two=0,three=0;
		for(int i=1;i<=n;i++)
		{
			cin >> s[i];
			l[i]=s[i].length()-1;
			if( s[i][0]=='0'&&s[i][l[i]]=='0' )	zero++;
			else if( s[i][0]=='0'&&s[i][l[i]]=='1' )	one++;
			else if( s[i][0]=='1'&&s[i][l[i]]=='0' )	two++;
			else	three++;
			string w=s[i];
			reverse(w.begin(),w.end() );
			mp[w]=1;//翻转后存在vector 
		}
		for(int i=1;i<=n;i++)
		{
			if( s[i][0]=='0'&&s[i][ l[i] ]=='1'&&!mp[ s[i] ] )
			{
				if( one-two>1 )	vec.push_back(i),one--,two++;
			}
			if( s[i][0]=='1'&&s[i][ l[i] ]=='0'&&!mp[ s[i] ])
			{
				if( two-one>1 )	vec.push_back(i),two--,one++;
			}
		}
		if( !one&&!two&&zero&&three )
			cout << -1 << endl;
		else
		{
			int len=vec.size();
			cout << len << endl;
			for(int i=0;i<len;i++)	cout << vec[i] << " ";
			cout << endl;
		}
	}
} 

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/107915040
今日推荐