zoukankan      html  css  js  c++  java
  • 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<<"---
    "
    #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<<'
    '; 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()<<'
    ';
    			for(int x:v) cout<<x<<' '; cout<<'
    ';	
    		}else cout<<-1<<'
    ';
    	}
    	return 0;
    }
    
  • 相关阅读:
    你知道RAID的初始化过程吗?
    Dynamic Disk Pool技术解析
    Ubuntu-16.04.6 安装 oracle 11.2.0.4 数据库database软件
    Ubuntu-16.04.6 安装 oracle 12.2.0.1 数据库database软件
    IDEA 快捷键
    Redis 常用操作命令
    甘特图
    PERT图
    现金贷
    线上信贷提供解决方案
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308733.html
Copyright © 2011-2022 走看看