CF1784B.Letter Exchange

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A cooperative game is played by mm people. In the game, there are 3m3m sheets of paper: mm sheets with letter 'w', mm sheets with letter 'i', and mm 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 mm 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 tt ( 1t1041 \le t \le 10^4 ). The description of the test cases follows.

The first line of each test case contains a single integer mm ( 2m1052 \le m \le 10^5 ) — the number of people.

The ii -th of the next mm lines contains a string sis_i of length 33 consisting of lowercase English letters 'w', 'i', and 'n', denoting the letters person ii 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 mm times in total.

It is guaranteed that the sum of mm over all test cases does not exceed 10510^5 .

输出格式

For each test case, print a non-negative integer kk — the smallest number of exchanges people need to make everyone have one 'w', one 'i', and one 'n'.

In each of the next kk lines print four tokens a1a_1 c1c_1 a2a_2 c2c_2 , describing the exchanges in chronological order ( 1a1,a2m1 \le a_1, a_2 \le m ; a1a2a_1 \ne a_2 ; c1,c2c_1, c_2 are one of {w,i,n}\{\mathtt{w}, \mathtt{i}, \mathtt{n}\} ): person a1a_1 gives letter c1c_1 to person a2a_2 , while person a2a_2 gives letter c2c_2 to person a1a_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
首页