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 1 to 9). 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 1 to 9 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
1s 2s 3s
0
9m 9m 9m
0
3p 9m 2p
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].
**题意:**输入三个由数字和字母组成的字符串,要使其满足字母相同且数字相同或字母相同,数字连续的条件下还需额外至少添加多少个这样的字符串满足这个要求。
**分析:**可知最坏的情况输出2(即出现三个不同的字母或出现相同字母而不相邻且不相隔的数字);输出1的情况为出现两个相同字符串或两个字母相同且数字两个连续或相隔;输出0的情况为三个字符串相同或字母相同且数字连续。 用vector存储每个字母对应的数值,再按上述情况分类讨论,详情看代码。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v[10];
// printf("%d\n", 's');//115
// printf("%d\n", 'm');//109
// printf("%d\n", 'p');//112
// v[0]对应m,v[3]对应p,v[6]对应s;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if ((s1 == s2) && (s2 == s3)) cout << 0 << endl;
else {
v[s1[1] - 'm'].push_back((s1[0] - '0'));
v[s2[1] - 'm'].push_back((s2[0] - '0'));
v[s3[1] - 'm'].push_back((s3[0] - '0'));
if (v[0].size() == 3) { // 如果含m的有三个数字
sort(v[0].begin(), v[0].end()); // 排序
if (v[0][2] - v[0][1] == 1 && v[0][1] - v[0][0] == 1) { // 相邻两个相差为一,输出0
cout << 0 << endl;
} else if (v[0][2] - v[0][1] == 1 || v[0][1] - v[0][0] == 1 ||
v[0][2] - v[0][1] == 2 || v[0][1] - v[0][0] == 2 ||
v[0][2] - v[0][1] == 0 || v[0][1] - v[0][0] == 0) { //如果出现相邻或相等或差值为2(比如3,5),则为1
cout << 1 << endl;
} else { // 以上都不符合为2
cout << 2 << endl;
}
} else if (v[3].size() == 3) {
sort(v[3].begin(), v[3].end());
if (v[3][2] - v[3][1] == 1 && v[3][1] - v[3][0] == 1) {
cout << 0 << endl;
} else if (v[3][2] - v[3][1] == 1 || v[3][1] - v[3][0] == 1 ||
v[3][2] - v[3][1] == 2 || v[3][1] - v[3][0] == 2 ||
v[3][2] - v[3][1] == 0 || v[3][1] - v[3][0] == 0) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else if (v[6].size() == 3) {
sort(v[6].begin(), v[6].end());
if (v[6][2] - v[6][1] == 1 && v[6][1] - v[6][0] == 1) {
cout << 0 << endl;
} else if (v[6][2] - v[6][1] == 1 || v[6][1] - v[6][0] == 1 ||
v[6][2] - v[6][1] == 2 || v[6][1] - v[6][0] == 2 ||
v[6][2] - v[6][1] == 0 || v[6][1] - v[6][0] == 0) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else {
if (v[0].size() == 1 && v[3].size() == 1 && v[6].size() == 1) { // 如果每个字母都出现一次,说明还需要2个
cout << 2 << endl;
}
if (v[0].size() == 2) { // 如果含m的有两个
if (abs(v[0][0] - v[0][1]) == 1 || abs(v[0][0] - v[0][1]) == 0 || abs(v[0][0] - v[0][1]) == 2) {// 如果出现相邻,相隔,相等,输出1
cout << 1 << endl;
} else { // 否则输出2
cout << 2 << endl;
}
}
if (v[3].size() == 2) {
if (abs(v[3][0] - v[3][1]) == 1 || abs(v[3][0] - v[3][1]) == 0 || abs(v[3][0] - v[3][1]) == 2) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
}
if (v[6].size() == 2) {
if (abs(v[6][0] - v[6][1]) == 1 || abs(v[6][0] - v[6][1]) == 0 || abs(v[6][0] - v[6][1]) == 2) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
}
}
}
return 0;
}