310113: CF1784B. Letter Exchange

Memory Limit:512 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Letter Exchange

题意翻译

有 $m$ 个人在一起做游戏,一共有 $3m$ 张纸条,$m$ 张纸条写着 "w",$m$ 张纸条写着 "i",$m$ 张纸条写着 "n",刚开始时每个人手里有 $3$ 张纸条。 一次操作即为交换两个人手里的 $1$ 张纸条,交换过程中每个人手里始终只有 $3$ 张纸条,你需要通过尽可能少的操作次数使得每个人手里的 $3$ 张纸条可以凑出单词 win,输出最小的次数并输出交换过程。

题目描述

A cooperative game is played by $ m $ people. In the game, there are $ 3m $ sheets of paper: $ m $ sheets with letter 'w', $ m $ sheets with letter 'i', and $ m $ sheets with letter 'n'. Initially, each person is given three sheets (possibly with equal letters). The goal of the game is to allow each of the $ m $ people to spell the word "win" using their sheets of paper. In other words, everyone should have one sheet with letter 'w', one sheet with letter 'i', and one sheet with letter 'n'. To achieve the goal, people can make exchanges. Two people participate in each exchange. Both of them choose exactly one sheet of paper from the three sheets they own and exchange it with each other. Find the shortest sequence of exchanges after which everyone has one 'w', one 'i', and one 'n'.

输入输出格式

输入格式


Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 10^4 $ ). The description of the test cases follows. The first line of each test case contains a single integer $ m $ ( $ 2 \le m \le 10^5 $ ) — the number of people. The $ i $ -th of the next $ m $ lines contains a string $ s_i $ of length $ 3 $ consisting of lowercase English letters 'w', 'i', and 'n', denoting the letters person $ i $ has on their sheets of paper at the beginning of the game, in arbitrary order. Each of the letters 'w', 'i', and 'n' appears on the sheets of paper exactly $ m $ times in total. It is guaranteed that the sum of $ m $ over all test cases does not exceed $ 10^5 $ .

输出格式


For each test case, print a non-negative integer $ k $ — the smallest number of exchanges people need to make everyone have one 'w', one 'i', and one 'n'. In each of the next $ k $ lines print four tokens $ a_1 $ $ c_1 $ $ a_2 $ $ c_2 $ , describing the exchanges in chronological order ( $ 1 \le a_1, a_2 \le m $ ; $ a_1 \ne a_2 $ ; $ c_1, c_2 $ are one of $ \{\mathtt{w}, \mathtt{i}, \mathtt{n}\} $ ): person $ a_1 $ gives letter $ c_1 $ to person $ a_2 $ , while person $ a_2 $ gives letter $ c_2 $ to person $ a_1 $ at the same time. If there are multiple solutions, print any.

输入输出样例

输入样例 #1

3
2
nwi
inw
3
inn
nww
wii
4
win
www
iii
nnn

输出样例 #1

0
2
2 w 3 i
3 w 1 n
3
2 w 3 i
2 w 4 n
3 i 4 n

Input

题意翻译

有 $m$ 个人在一起做游戏,一共有 $3m$ 张纸条,$m$ 张纸条写着 "w",$m$ 张纸条写着 "i",$m$ 张纸条写着 "n",刚开始时每个人手里有 $3$ 张纸条。 一次操作即为交换两个人手里的 $1$ 张纸条,交换过程中每个人手里始终只有 $3$ 张纸条,你需要通过尽可能少的操作次数使得每个人手里的 $3$ 张纸条可以凑出单词 win,输出最小的次数并输出交换过程。

Output

题目大意:

有 $ m $ 个人在一起做游戏,一共有 $ 3m $ 张纸条,$ m $ 张纸条写着 "w",$ m $ 张纸条写着 "i",$ m $ 张纸条写着 "n",刚开始时每个人手里有 3 张纸条。一次操作即为交换两个人手里的 1 张纸条,交换过程中每个人手里始终只有 3 张纸条,你需要通过尽可能少的操作次数使得每个人手里的 3 张纸条可以凑出单词 "win",输出最小的次数并输出交换过程。

输入输出数据格式:

输入格式:
- 第一行包含测试用例的数量 $ t $ ($ 1 \le t \le 10^4 $)
- 每个测试用例包含一个整数 $ m $

输出格式:
- 对于每个测试用例,输出最小的交换次数和交换过程。题目大意: 有 $ m $ 个人在一起做游戏,一共有 $ 3m $ 张纸条,$ m $ 张纸条写着 "w",$ m $ 张纸条写着 "i",$ m $ 张纸条写着 "n",刚开始时每个人手里有 3 张纸条。一次操作即为交换两个人手里的 1 张纸条,交换过程中每个人手里始终只有 3 张纸条,你需要通过尽可能少的操作次数使得每个人手里的 3 张纸条可以凑出单词 "win",输出最小的次数并输出交换过程。 输入输出数据格式: 输入格式: - 第一行包含测试用例的数量 $ t $ ($ 1 \le t \le 10^4 $) - 每个测试用例包含一个整数 $ m $ 输出格式: - 对于每个测试用例,输出最小的交换次数和交换过程。

加入题单

上一题 下一题 算法标签: