Welcome to ZOJ
Select Problem
ZOJ Problem Set - 3669
Japanese Mahjong I

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjong.

Japanese mahjong is usually played with 136 tiles, which can be organized into several categories:

  • Suited tiles. All suited tiles are of a rank and a suit.There are three suits of tiles, with ranks ranging from one to nine. There are four tiles of each rank and suit combination, thus there are 36 tiles in a suit, and 108 suited tiles in total.
    • The circle suit
    • The bamboo suit
    • The character suit
  • Honor tiles. Honor Tiles are tiles that do not have a rank or suit. They are divided into two categories. There are four types of Wind tiles and three types of Dragon tiles, with four of each type of honor tile. Thus, there are 16 wind tiles and 12 Dragon tiles for 28 honor tiles.
    • Wind tiles. The Wind tiles consist of four kinds of tile: East, South, West, and North.
    • Dragon tiles. The Dragon titles consist of three types of tile: Red, Green, White.

A winning hand consists of fourteen tiles, which is made of four melds (a specific pattern of three pieces) and the eyes (a pair of two identical pieces). The definition of melds and eyes is given as followed:

  • Melds are listed as followed:
    • Pong is a set of three identical tiles. You can form a pong with any tile. The tiles must be identical (you cannot mix suits). For example:
    • Kong is a set of four identical tiles, which is similar to Pong. For example:
    • Chow is a meld of three suited tiles in sequence. The meld must be in absolute numerical sequence. There is no skipping of numbers, nor does 9 loop around to 1. The sequence must be in the same suit. Honours cannot be used to make chows. For example:
  • Eyes, also known as a pair, are two identical tiles which are a component to the standard hand. For example:

When a hand is one tile short of winning, the hand is said to be a ready hand, or more figuratively, "on the pot". The player holding a ready hand is said to be waiting for certain tiles. Now, given thirteen tiles, can you answer how many types of tiles you are waiting for a winning hand? If the given hand were not a ready hand, output an integer zero instead.


There are multiple cases. Each case consists of 26 characters in one line, describing thirteen tiles. The manner for describing each type of tile is:

  • Two characters stand for one tile.
  • For Suited tiles, the first is a integer ranged from one to nine, the tile's rank, and the second is a character: 'p' for the circle suit, 's' for the bamboo suit, and 'm' for the character suit.
  • For Honor tiles, the first is a integer: from one to seven stands for East, South, West, North, White, Green, and Red respectively. The second one is always 'z'.
We promise that the input is a legal hand, which means there isn't another type of tiles described above or there are more than four same tiles.


For each case, first output the number of types of tiles you are waiting for in one line. Then output each type of tile you are waiting for in the manner described above. output them in this fixed order: 'm', 'p', 's', 'z', and for each suit, smaller rank first (Honor tiles is similar to suited tiles, only using the integer standing for in above manner instead of suited rank).

Sample Input


Sample Output

1 2z
2 6m9m
3 1s4s6z


  • The hand in the first picture is not a winning hand indeed, because there are fifteen tiles. If 1m or 9m is droped, it will be a winning hand.
  • Note that the input tiles is not ensured in the order of output.


Mahjong - Wikipedia, the free encyclopedia

Author: YU, Xiaoyao
Contest: ZOJ Monthly, November 2012
Submit    Status