博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CodeForces 1277D --- Let's Play the Words?】
阅读量:2038 次
发布时间:2019-04-28

本文共 4642 字,大约阅读时间需要 15 分钟。

【CodeForces 1277D --- Let's Play the Words?】

题目来源:

Description

Polycarp has n different binary words. A word called binary if it contains only characters ‘0’ and ‘1’. For example, these words are binary: “0001”, “11”, “0” and “0011100”.

Polycarp wants to offer his set of n binary words to play a game “words”. In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: “0101”, “1”, “10”, “00”, “00001”.

Word reversal is the operation of reversing the order of the characters. For example, the word “0111” after the reversal becomes “1110”, the word “11010” after the reversal becomes “01011”.

Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that:

  • the final set of n words still contains different words (i.e. all words are unique);
  • there is a way to put all words of the final set of words in the order so that the final sequence of n words is consistent with the game rules.

Polycarp wants to reverse minimal number of words. Please, help him.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.

The first line of a test case contains one integer n (1≤n≤2⋅105) — the number of words in the Polycarp’s set. Next n lines contain these words. All of n words aren’t empty and contains only characters ‘0’ and ‘1’. The sum of word lengths doesn’t exceed 4⋅106. All words are different.

Guaranteed, that the sum of n for all test cases in the input doesn’t exceed 2⋅105. Also, guaranteed that the sum of word lengths for all test cases in the input doesn’t exceed 4⋅106.

Output

Print answer for all of t test cases in the order they appear.

If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain k (0≤k≤n) — the minimal number of words in the set which should be reversed. The second line of the output should contain k distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from 1 to n in the order they appear. If k=0 you can skip this line (or you can print an empty line). If there are many answers you can print any of them.

Sample Input

4

4
0001
1000
0011
0111
3
010
101
0
2
00000
00001
4
01
001
0001
00001

Sample Output

1

3
-1
0

2

1 2

解题思路

输入时分别记录"0…1",“1…0”,“0…0”,"1…1"的个数分别为a,b,c,d。

  • 当a=0并且b=0时:
    • 如果c,d都不为0那么无法符合要求。
    • 如果c,d中有一个为0,那么就是满足要求的,直接输出0.
  • 当a==b时:满足要求直接输出0.
  • 其他情况时我们只需通过反转转化字符串,注意不能出现相同的,当abs(a-b)<=1时即满足题意。(例如:1010,0101,101,010)

AC代码:

#include 
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'int main(){
SIS; int T; cin >> T; while(T--) {
int n,a=0,b=0,c=0,d=0; string s; cin >> n; map
m1,m2; for(int i=0;i
> s; char sf=s[0],se=s[s.length()-1]; if(sf=='0' && se=='1') a++,m1[s]=i+1; else if(sf=='1' && se=='0') b++,m2[s]=i+1; else if(sf=='0' && se=='0') c++; else if(sf=='1' && se=='1') d++; } if(a==0 && b==0) {
if(c!=0 && d!=0) cout << -1 << endl; else cout << 0 << endl << endl; } else if(a==b) cout << 0 << endl << endl; else {
vector
ans; while(abs(a-b)>1) {
if(a>b) {
for(auto m:m1) {
if(a-b<=1) break; s = m.first; reverse(s.begin(),s.end()); if(!m2[s]) {
a--; b++; ans.emplace_back(m.second); } } } else {
for(auto m:m2) {
if(b-a<=1) break; s = m.first; reverse(s.begin(),s.end()); if(!m1[s]) {
b--; a++; ans.emplace_back(m.second); } } } } int len=ans.size(); cout << len << endl; for(int i=0;i

转载地址:http://kpyof.baihongyu.com/

你可能感兴趣的文章
Spring Cloud各组件超时总结
查看>>
设计模式六大原则的理解
查看>>
spring cloud Feign请求响应启用压缩
查看>>
一个接口多个实现类的Spring注入方式(注解方式)
查看>>
MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)
查看>>
HBase深度简介
查看>>
Spring Cloud的分布式事务框架压测第一轮
查看>>
上海高级java/前端内推
查看>>
java split点号的处理
查看>>
linux命令学习之:systemctl
查看>>
linux查看某个应用占用多少线程
查看>>
html移动的文字
查看>>
一份非常完整的 MySQL 规范
查看>>
Collections.unmodifiableMap():map得深拷贝
查看>>
Metrics教程
查看>>
Dropwizard官方教程(一) 入门
查看>>
Dropwizard官方教程(二) 核心
查看>>
es 搜索多个index和多个type下的数据
查看>>
@ConditionalOnProperty来控制Configuration是否生效
查看>>
springboot使用加密的配置属性
查看>>