当前位置: 首页 > 工具软件 > mahjong > 使用案例 >

Codeforces Round #573 (Div. 2) B. Tokitsukaze and Mahjong

佟嘉祯
2023-12-01

B. Tokitsukaze and Mahjong

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 11 to 99). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, ……, 9m, 1p, 2p, ……, 9p, 1s, 2s, ……, 9s.

In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.

Do you know the minimum number of extra suited tiles she needs to draw so that she can win?

Here are some useful definitions in this game:

  • A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
  • A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
  • A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.

Some examples:

  • [2m, 3p, 2s, 4m, 1s, 2s, 4s] — it contains no koutsu or shuntsu, so it includes no mentsu;
  • [4s, 3m, 3p, 4s, 5p, 4s, 5p] — it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
  • [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] — it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.

Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.

Input

The only line contains three strings — the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 11 to 99 and the second character is m, p or s.

Output

Print a single integer — the minimum number of extra suited tiles she needs to draw.

Examples

input

Copy

1s 2s 3s

output

Copy

0

input

Copy

9m 9m 9m

output

Copy

0

input

Copy

3p 9m 2p

output

Copy

1

Note

In the first example, Tokitsukaze already has a shuntsu.

In the second example, Tokitsukaze already has a koutsu.

In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile — 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].

题解:考虑问题一定要全面,最大的ans=2;读懂题意就知道要干啥,但是一定要谨慎。cf测试样例100多个,我很服气!

注意ans=1的情况,要考虑清楚。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=5;
int main()
{
  int ans=0;
  char a[maxn],b[maxn],c[maxn];
  cin>>a>>b>>c;
  if(strcmp(a,b)==0&&strcmp(a,c)==0)
  {
    ans=0;
  }
  else if(a[1]==b[1]&&a[1]==c[1])
  {
    //cout<<a[1]<<" "<<b[1]<<" "<<c[1]<<endl;
    if((a[0]==b[0]-1&&a[0]==c[0]-2)||(a[0]==b[0]-2&&a[0]==c[0]-1)
    ||(a[0]==b[0]+1&&a[0]==c[0]-1)||(a[0]==b[0]-1&&a[0]==c[0]+1)
    ||(a[0]==b[0]+1&&a[0]==c[0]+2)||(a[0]==b[0]+2&&a[0]==c[0]+1))
    {
      ans=0;
      //cout<<a[0]<<b[0]<<c[0]<<endl;
    }
    else if(a[0]==b[0]+1||a[0]==b[0]-1||a[0]==b[0]-2||a[0]==b[0]+2||
    a[0]==c[0]+1||a[0]==c[0]-1||a[0]==c[0]-2||a[0]==c[0]+2||
    b[0]==c[0]+1||b[0]==c[0]-1||b[0]==c[0]-2||b[0]==c[0]+2||
    a[0]==b[0]||a[0]==c[0]||b[0]==c[0])
    {
      ans=1;
    }
    else
    {
      ans=2;
    }
  }
  else
  {
    if((strcmp(a,b)==0)||(strcmp(a,c)==0)||(strcmp(b,c)==0))
    {
      ans=1;
    }
    else if((a[0]==b[0]+1&&a[1]==b[1])||(a[0]==c[0]+1&&a[1]==c[1])
    ||(a[0]==b[0]-1&&a[1]==b[1])||(a[0]==c[0]-1&&a[1]==c[1])||
    (a[0]==b[0]-2&&a[1]==b[1])||(a[0]==c[0]-2&&a[1]==c[1])||
    (a[0]==b[0]+2&&a[1]==b[1])||(a[0]==c[0]+2&&a[1]==c[1])||
    (b[0]==c[0]-1&&b[1]==c[1])||(b[0]==c[0]+1&&b[1]==c[1])||
    (b[0]==c[0]-2&&b[1]==c[1])||(b[0]==c[0]+2&&b[1]==c[1]))
    {
      ans=1;
    }
    else
    {
      ans=2;
    }
    //cout<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}

主要是细心就可以交了5发终于过了。

 类似资料: