Codeforces 1277D Let's Play the Words?

题目链接:

1277D Let’s Play the Words?

思路:

1.首先只考虑首尾,分别统计01 10 00 11的个数,如果前两个没有,后两个都有那肯定不可以;反之后两个可以不考虑,因为肯定可以接上;
2.因为是首尾相接,01 10的数量需要相等或者相差1;
3.遍历所有字符串,如果01多,就将首尾为01的逆序变成10,同时需要判断这个逆序字符串是否已经存在,不存在才可以将它逆序,最后判断01 10的数量是否符合要求;反之10多,同理;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
#define fi first
#define sc second
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pt(a) cerr<<a<<"---\n"
#define rp(i,n) for(int i=0;i<n;i++)
#define rpn(i,n) for(int i=1;i<=n;i++)
const int maxn=2e5+99;
int ff[maxn];
string str[maxn];
void clear(int n){
	rp(i,n) ff[i]=0;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t; cin>>t;
	while(t--){
		int a=0,b=0,c=0,d=0; //01 10 00 11
		int n; cin>>n;
		clear(n);
		map<string,bool> ans;
		rp(i,n){
			string s; cin>>s; ans[s]=1; str[i]=s;
			int x=(s[0]-'0')*2+*s.rbegin()-'0';
			if(x==1) a++,ff[i]=1;
			if(x==2) b++,ff[i]=-1;
			if(x==0) c++;
			if(x==3) d++;
		}
//		pt(a); pt(b); pt(c); pt(d);
		if(!a&&!b&&c&&d){
			cout<<-1<<'\n'; continue;
		}
		int cnt=a-b; vector<int> v;
		if(cnt>1){
			rp(i,n){
				if(ff[i]==1){
					string s=str[i];
					reverse(s.begin(),s.end());
					if(ans[s]==false) v.pb(i+1),cnt-=2,ans[s]=true;
				}
				if(cnt<=1) break;
			}
		}else if(cnt<-1){
			rp(i,n){
				if(ff[i]==-1){
					string s=str[i];
					reverse(s.begin(),s.end());
					if(ans[s]==false) v.pb(i+1),cnt+=2,ans[s]=true;
				}
				if(cnt>=-1) break;
			}
		}
		if(cnt<=1&&cnt>=-1){
			cout<<v.size()<<'\n';
			for(int x:v) cout<<x<<' '; cout<<'\n';	
		}else cout<<-1<<'\n';
	}
	return 0;
}
posted @ 2019-12-21 18:14  YuhanのBlog  阅读(118)  评论(0编辑  收藏  举报