CF1784B.Letter Exchange
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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'.
一个合作游戏由 m 个人参与。游戏中共有 3m 张纸片:其中 m 张印有字母 'w',m 张印有字母 'i',m 张印有字母 'n'。
初始时,每人分得三张纸片(这些纸片上的字母可能相同)。
游戏的目标是让这 m 个人均能用自己手中的纸片拼出单词 "win"。换言之,每个人都必须恰好拥有一张 'w'、一张 'i' 和一张 'n'。
为达成目标,参与者可进行交换操作。每次交换由两人参与:双方各自从自己当前持有的三张纸片中恰好选择一张,并相互交换。
请找出使所有人最终都恰好拥有一张 'w'、一张 'i' 和一张 'n' 所需的最少交换次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer m (2≤m≤105) — the number of people.
The i-th of the next m lines contains a string si 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 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 m(2≤m≤105)—— 表示人数。
接下来的 m 行中,第 i 行包含一个长度为 3 的字符串 si,由小写英文字母 'w'、'i' 和 'n' 组成,表示第 i 个人在游戏开始时其纸张上所写的字母(顺序任意)。
在整个所有人的纸张上,字母 'w'、'i' 和 'n' 各自恰好总共出现 m 次。
保证所有测试用例的 m 值之和不超过 105。
输出格式
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 a1 c1 a2 c2, describing the exchanges in chronological order (1≤a1,a2≤m; a1=a2; c1,c2 are one of w,i,n): person a1 gives letter c1 to person a2, while person a2 gives letter c2 to person a1 at the same time.
If there are multiple solutions, print any.
对于每个测试用例,输出一个非负整数 k —— 使得每个人最终都恰好拥有一张 'w'、一张 'i' 和一张 'n' 所需的最小交换次数。
接下来的 k 行中,每行输出四个标记 a1 c1 a2 c2,按时间顺序描述每次交换(其中 1≤a1,a2≤m;a1=a2;c1,c2 均为 w,i,n 中的一个):即第 a1 个人将字母 c1 给第 a2 个人,同时第 a2 个人将字母 c2 给第 a1 个人。
若存在多种解法,输出任意一种即可。
输入输出样例
输入#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
输入解题思路,AI测评打分。不知道怎么写?