Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int arr[5], vis[10]; int main() { int a, b, c; char d, e, f; cin >> a >> d >> b >> e >> c >> f; vis[a]++, vis[b]++, vis[c]++; arr[2] = max(a, max(b, c)); arr[0] = min(a, min(b, c)); vis[arr[2]]++; vis[arr[0]]++; for (int i = 0; i <= 9; i++) { if (vis[i] == 1) { arr[1] = i; break; } } if (a == b && b == c && d == e && e == f) cout << 0 << endl; else if (d == e && e == f && arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if ((a == b && d == e) || (a == c && d == f) || (b == c && e == f)) cout << 1 << endl; else { if (abs(a - b) == 1 && d == e) cout << 1 << endl; else if (abs(a - c) == 1 && d == f) cout << 1 << endl; else if (abs(b - c) == 1 && e == f) cout << 1 << endl; else if (abs(a - b) == 2 && d == e) cout << 1 << endl; else if (abs(a - c) == 2 && d == f) cout << 1 << endl; else if (abs(b - c) == 2 && e == f) cout << 1 << endl; else cout << 2 << endl; } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
t = input().split(sep=' ') t.sort(key=lambda i: i[0]) n = list(map(lambda i: int(i[0]), t)) b = list(map(lambda i: i[1], t)) if t[0] == t[1] and t[1] == t[2]: print(0) elif n[0] + 1 == n[1] and n[1] + 1 == n[2] and b[0] == b[1] and b[1] == b[2]: print(0) elif ( (b[0] == b[1] and n[0] == n[1]) or (b[1] == b[2] and n[1] == n[2]) or (b[0] == b[2] and n[2] == n[0]) or (b[0] == b[2] and (n[0] + 1 == n[2] or n[0] + 2 == n[2])) or (b[1] == b[2] and (n[1] + 1 == n[2] or n[1] + 2 == n[2])) or (b[1] == b[0] and (n[0] + 1 == n[1] or n[0] + 2 == n[1])) ): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def fl(a, b): if (a==b): return 1 else: return 0 a, b, c = input().split() l1 = [i for i in range(1, 10)] su = 0 su += fl(a, b) su += fl(c, b) su += fl(a, c) dic = {} dic['s'] = 0 dic['m'] = 0 dic['p'] = 0 if (su == 1): print(1) elif (su == 3): print(0) else: dic[a[1]] += 1 dic[b[1]] += 1 dic[c[1]] += 1 f = 1 for i in dic: if (dic[i]>1): if (dic[i]==2): m = [] for j in [a,b,c]: if j[1]==i: m.append(int(j[0])) if (abs(m[0]-m[1])<=2): print(1) else: print(2) break elif (dic[i]==3): m = int(a[0]) n = int(b[0]) p = int(c[0]) if (abs(m-n)+abs(n-p)+abs(p-m)==4): print(0) else: flag = 0 if (abs(m-n)<=2): flag = 1 if (abs(p-n)<=2): flag = 1 if (abs(m-p)<=2): flag = 1 if (flag): print(1) else: print(2) break else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.*; import java.util.*; import java.math.*; import java.text.*; import java.util.regex.*; import java.awt.Point; public final class Main{ long mod = 1000000007; public static void main(String[] args)throws Exception{ new Main().run(); } { st = null; br = new BufferedReader(new InputStreamReader(System.in)); } void run()throws Exception{ String[] s = new String[3]; for(int i=0; i<3; i++)s[i] = nt(); HashSet<String> set = new HashSet<>(); for(String ss : s)set.add(ss); int min = ((set.size() == 1) ? 0 : ((set.size() == 2) ? 1 : 2)); int[][] temp = new int[3][10]; for(int i=0; i<3; i++){ //m p s if(s[i].charAt(1) == 'm'){ temp[0][s[i].charAt(0)-'0']++; }else if(s[i].charAt(1) == 'p'){ temp[1][s[i].charAt(0)-'0']++; }else{ temp[2][s[i].charAt(0)-'0']++; } } int min2 = -1; for(int i=0; i<temp.length; i++){ for(int j=2; j<temp[0].length; j++){ min2 = Math.max(min2, Math.min(1, temp[i][j-2]) + Math.min(1, temp[i][j-1]) + Math.min(1, temp[i][j])); } } min2 = 3 - min2; pl(Math.min(min, min2)); } //------------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------------// BufferedReader br; StringTokenizer st; String nextToken()throws Exception{ if(st == null || !st.hasMoreTokens()){st = new StringTokenizer(br.readLine(), " ");} return st.nextToken(); } int ni()throws Exception{return Integer.parseInt(nextToken());} long nl()throws Exception{return Long.parseLong(nextToken());} double nd()throws Exception{return Double.parseDouble(nextToken());} String nt()throws Exception{return nextToken();} String ns()throws Exception{return br.readLine();} void p(Object o){System.out.print(o);} void pl(Object o){System.out.println(o);} void pl(int[] a){System.out.println(Arrays.toString(a));} //------------------------------------------------------------------------------------------------// //------------------------------------------------------------------------------------------------// long min(long... nos){ if(nos.length == 0)return -1; long min = nos[0]; for(int i=1; i<nos.length; i++)min = Math.min(min, nos[i]); return min; } long max(long... nos){ if(nos.length == 0)return -1; long max = nos[0]; for(int i=1; i<nos.length; i++)max = Math.max(max, nos[i]); return max; } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def main(): buf = input() buflist = buf.split() hand = buflist; t = [] for i in range(3): t.append([]) for j in range(9): t[i].append(0) for x in hand: idx = 0 if x[1] == 'm': idx = 0 elif x[1] == 'p': idx = 1 elif x[1] == 's': idx = 2 t[idx][int(x[0])-1] += 1 max_cons = 0 max_mult = 0 for i in range(3): cons = [0, 0, 0] for j in range(9): cons[0] = cons[1] cons[1] = cons[2] if t[i][j] > 0: cons[2] = 1 else: cons[2] = 0 max_cons = max(sum(cons), max_cons) max_mult = max(max_mult, t[i][j]) print(3 - max(max_cons, max_mult)) if __name__ == '__main__': main()
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a, b, c = input().split() tiles = set() for i in a, b, c: tiles.add(i) if len(tiles) == 1: print(0) elif len(tiles) == 2: print(1) else: pairs = [] for i in a, b, c: num = i[:-1] let = i[-1] pairs.append((int(num), let)) pairs = sorted(pairs) if pairs[0][1] == pairs[1][1] and pairs[1][1] == pairs[2][1]: if pairs[0][0] == pairs[1][0] - 1 and pairs[1][0] == pairs[2][0] - 1: print(0) elif pairs[0][0] == pairs[1][0] - 1 or pairs[1][0] == pairs[2][0] - 1 or pairs[0][0] == pairs[1][0] - 2 or pairs[1][0] == pairs[2][0] - 2: print(1) else: print(2) else: matches = [0, 0] if pairs[0][1] == pairs[1][1]: matches = [pairs[0][0], pairs[1][0]] elif pairs[1][1] == pairs[2][1]: matches = [pairs[1][0], pairs[2][0]] elif pairs[0][1] == pairs[2][1]: matches = [pairs[0][0], pairs[2][0]] if matches[0] == matches[1] - 1 or matches[0] == matches[1] - 2: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
# int(input()) # [int(s) for s in input().split()] # input() def solve(): m = [s for s in input().split()] d = {"m": [], "p": [], "s": []} ans = float("inf") for s in m: d[s[1]].append(int(s[0])) for s in d: if len(d[s]) == 1: ans = min(ans, 2) elif len(d[s]) == 2: d[s] = sorted(d[s]) if d[s][0] == d[s][1]: ans = min(ans, 1) elif d[s][0]+1 == d[s][1] or d[s][0]+2 == d[s][1]: ans = min(ans, 1) elif len(d[s]) == 3: d[s] = sorted(d[s]) if d[s][0] == d[s][1]: if d[s][1] == d[s][2]: ans = min(ans, 0) else: ans = min(ans, 1) elif d[s][1] == d[s][2]: ans = min(ans, 1) else: if d[s][0]+1 == d[s][1]: if d[s][1] + 1 == d[s][2]: ans = min(ans, 0) else: ans = min(ans, 1) elif d[s][0]+2 == d[s][1]: ans = min(ans, 1) elif d[s][1] + 1 == d[s][2]: ans = min(ans, 1) if ans == float("inf"): print(2) else: print(ans) if __name__ == "__main__": solve()
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def checkConsecutive(l): return sorted(l) == list(range(min(l), max(l)+1)) a,b,c=map(str,input().split()) x,y,z=int(a[0]),int(b[0]),int(c[0]) p,q,r=a[1],b[1],c[1] l=[] l.append(x) l.append(y) l.append(z) if (a==b==c) or (checkConsecutive(l) and p==q==r): ans=0 elif ((x==y+1 or x==y+2) and p==q) or ((y==z+1 or y==z+2) and q==r) or ((z==x+1 or z==x+2) and p==r) or ((x==z+1 or x==z+2) and p==r) or ((y==x+1 or y==x+2) and q==p) or ((z==y+1 or z==y+2) and q==r): ans=1 elif a==b or b==c or a==c: ans=1 else: ans=2 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
l = input().split() p = [0]*10 s = [0]*10 m = [0]*10 for i in l: if i[1] == "p": p[int(i[0])] += 1 if i[1] == "s": s[int(i[0])] += 1 if i[1] == "m": m[int(i[0])] += 1 ans1 = max(max(s),max(m),max(p)) ans2 = 0 for x in p,s,m: for i in range(1,8): t = sum([1 for j in x[i:i+3] if j]) if t>ans2: ans2 = t print(3-max(ans1,ans2))
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; void ans(int64_t a) { cout << a; exit(0); } int32_t main() { ios_base::sync_with_stdio(false); vector<pair<int64_t, int64_t> > v; for (int64_t i = 0; i < 3; ++i) { int64_t a, b = 0; cin >> a; char c; cin >> c; if (c == 'm') b = 1; if (c == 'p') b = 2; v.push_back({a, b}); } sort(v.begin(), v.end()); if (v[0] == v[1] && v[1] == v[2]) ans(0); if (v[0].second == v[1].second && v[1].second == v[2].second && v[0].first + 1 == v[1].first && v[1].first + 1 == v[2].first) ans(0); if (v[0].second == v[1].second && v[0].first + 3 > v[1].first) ans(1); if (v[1].second == v[2].second && v[1].first + 3 > v[2].first) ans(1); if (v[0].second == v[2].second && v[0].first + 3 > v[2].first) ans(1); ans(2); }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
# import math def check(a,b): val1 = int(a[0]) val2 = int(b[0]) if(a==b): return 1 if((val1+1==val2 or val1+2==val2) and a[1]==b[1]): return 1 return 2 ct = 2 l = list(input().split()) l.sort() val1 = int(l[0][0]) val2 = int(l[1][0]) val3 = int(l[2][0]) if(l[0]==l[1] and l[1]==l[2]): print(0) exit() elif(val1+1==val2 and val2+1==val3 and (l[0][1]==l[1][1] and l[1][1]==l[2][1])): print(0) exit() ct = min(ct,check(l[0],l[1])) ct = min(ct,check(l[1],l[2])) ct = min(ct,check(l[0],l[2])) print(ct)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
// practice with rainboy import java.io.*; import java.util.*; public class CF1191B extends PrintWriter { CF1191B() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1191B o = new CF1191B(); o.main(); o.flush(); } boolean close(byte a, byte b) { return Math.abs(a - b) <= 2; } boolean good(byte a, byte b, byte c) { if (a > b) return good(b, a, c); if (a > c) return good(c, b, a); if (b > c) return good(a, c, b); return a == b && b == c || a == b - 1 && b + 1 == c; } void main() { byte[] aa = sc.next().getBytes(); byte[] bb = sc.next().getBytes(); byte[] cc = sc.next().getBytes(); int ans = 2; if (aa[1] == bb[1] && aa[1] == cc[1] && good(aa[0], bb[0], cc[0])) ans = 0; else if (aa[1] == bb[1] && close(aa[0], bb[0])) ans = 1; else if (bb[1] == cc[1] && close(bb[0], cc[0])) ans = 1; else if (cc[1] == aa[1] && close(cc[0], aa[0])) ans = 1; println(ans); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
""" Author : co_devil Chirag Garg Institute : JIIT """ from __future__ import division, print_function from sys import stdin,stdout import itertools, os, sys, threading from collections import deque, Counter, OrderedDict, defaultdict import heapq from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd # from bisect import bisect_left,bisect_right # from decimal import *,threading """from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip else: from builtins import str as __str__ str = lambda x=b'': x if type(x) is bytes else __str__(x).encode() BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._buffer = BytesIO() self._fd = file.fileno() self._writable = 'x' in file.mode or 'r' not in file.mode self.write = self._buffer.write if self._writable else None def read(self): return self._buffer.read() if self._buffer.tell() else os.read(self._fd, os.fstat(self._fd).st_size) def readline(self): while self.newlines == 0: b, ptr = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)), self._buffer.tell() self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr) self.newlines += b.count(b'\n') + (not b) self.newlines -= 1 return self._buffer.readline() def flush(self): if self._writable: os.write(self._fd, self._buffer.getvalue()) self._buffer.truncate(0), self._buffer.seek(0) sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) input = lambda: sys.stdin.readline().rstrip(b'\r\n') def print(*args, **kwargs): sep, file = kwargs.pop('sep', b' '), kwargs.pop('file', sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop('end', b'\n')) if kwargs.pop('flush', False): file.flush() """ def ii(): return int(input()) def si(): return str(input()) def mi(): return map(int,input().split()) def li(): return list(mi()) def fii(): return int(stdin.readline()) def fsi(): return str(stdin.readline()) def fmi(): return map(int,stdin.readline().split()) def fli(): return list(fmi()) abc = 'abcdefghijklmnopqrstuvwxyz' abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod = 1000000007 dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1] def getKey(item): return item[0] def sort2(l): return sorted(l, key=getKey) def d2(n, m, num): return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo(x): return (x and (not (x & (x - 1)))) def decimalToBinary(n): return bin(n).replace("0b", "") def ntl(n): return [int(i) for i in str(n)] def powerMod(x, y, p): res = 1 x %= p while y > 0: if y & 1: res = (res * x) % p y = y >> 1 x = (x * x) % p return res def gcd(x, y): while y: x, y = y, x % y return x # For getting input from input.txt file # sys.stdin = open('input.txt', 'r') # Printing the Output to output.txt file # sys.stdout = open('output.txt', 'w') graph = defaultdict(list) visited = [0] * 1000000 col = [-1] * 1000000 def dfs(v, c): if visited[v]: if col[v] != c: print('-1') exit() return col[v] = c visited[v] = 1 for i in graph[v]: dfs(i, c ^ 1) def bfs(d,v): q=[] q.append(v) visited[v]=1 while len(q)!=0: x=q[0] q.pop(0) for i in d[x]: if visited[i]!=1: visited[i]=1 q.append(i) print(x) print(l) def make_graph(e): d={} for i in range(e): x,y=mi() if x not in d.keys(): d[x]=[y] else: d[x].append(y) if y not in d.keys(): d[y] = [x] else: d[y].append(x) return d def gr2(n): d={} for i in range(n): x,y=mi() if x not in d.keys(): d[x]=[y] else: d[x].append(y) return d def connected_components(graph): seen = set() def dfs(v): vs = set([v]) component=[] while vs: v = vs.pop() seen.add(v) vs |= set(graph[v]) - seen component.append(v) return component ans=[] for v in graph: if v not in seen: d=dfs(v) ans.append(d) return ans def primeFactors(n): s=set() while n % 2 == 0: s.add(2) n = n // 2 for i in range(3, int(sqrt(n)) + 1, 2): while n % i == 0: s.add(i) n = n // i if n > 2: s.add(n) return s d = { 'm': 10, 's': 30, 'p': 60 } a, b, c = list(sorted([int(i[0]) + d[i[1]] for i in input().split(' ')])) if (a == b and b == c) or (a == b - 1 and b == c - 1): print(0) exit() if a == b - 1 or b == c - 1 or a == b or b == c or a == c - 2 or a == b - 2 or b == c - 2: print(1) exit() print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Arrays; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.math.*; public class Main6{ static class Pair { int x; int y; public Pair(int x,int y) { this.x= x; this.y= y; } } static class Pair1 { String x; int y; int z; } static class Compare { /*static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.start>p2.start) { return 1; } else if(p1.start==p2.start) { return 0; } else { return -1; } } }); } */ } public static long pow(long a, long b) { long result=1; while(b>0) { if (b % 2 != 0) { result=(result*a)%mod; b--; } a=(a*a)%mod; b /= 2; } return result; } public static long fact(long num) { long value=1; int i=0; for(i=2;i<num;i++) { value=((value%mod)*i%mod)%mod; } return value; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } /* public static long lcm(long a,long b) { return a * (b / gcd(a, b)); } */ public static long sum(int h) { return (h*(h+1)/2); } /*public static void dfs(int parent,boolean[] visited) { TreeSet<Integer> arr=new TreeSet<Integer>(); arr=graph.get(parent); visited[parent]=true; if(a[parent]==1) { flag=1; } if(a[parent]==2) { flag1=1; } if(flag==1 && flag1==1) { return; } Iterator itr=arr.iterator(); while(itr.hasNext()) { int num=(int)itr.next(); if(visited[num]==false) { dfs(num,visited); } } x1x` }*/ // static int flag1=0; static int[] dis; static int mod=1000000007; static ArrayList<ArrayList<Integer>> graph; public static void bfs(int num,int size) { boolean[] visited=new boolean[size+1]; Queue<Integer> q=new LinkedList<>(); q.add(num); ans[num]=1; visited[num]=true; while(!q.isEmpty()) { int x=q.poll(); ArrayList<Integer> al=graph.get(x); for(int i=0;i<al.size();i++) { int y=al.get(i); if(visited[y]==false) { q.add(y); ans[y]=ans[x]+1; visited[y]=true; } } } } static int[] ans; // static int[] a; public static int[] sort(int[] a) { int n=a.length; ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<a.length;i++) { ar.add(a[i]); } Collections.sort(ar); for(int i=0;i<n;i++) { a[i]=ar.get(i); } return a; } // static int flag=1; static public void main(String args[])throws IOException { // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); boolean[] prime=new boolean[1000001]; /* for(int i=2;i*i<=1000000;i++) { if(prime[i]==false) { for(int j=2*i;j<=1000000;j+=i) { prime[j]=true; } } }*/ String[] s=new String[3]; for(int i=0;i<3;i++) { s[i]=s(); } Arrays.sort(s); if(s[0].equals(s[1]) && s[1].equals(s[2])) { pln("0"); } else { // pln(Arrays.toString(s)); if(s[0].charAt(1)!=s[1].charAt(1) && s[1].charAt(1)!=s[2].charAt(1) && s[2].charAt(1)!=s[0].charAt(1)) { pln("2"); } else if(s[0].charAt(1)==s[1].charAt(1) && s[1].charAt(1)!=s[2].charAt(1)) { if(s[0].charAt(0)==s[1].charAt(0) || s[0].charAt(0)-'0'==s[1].charAt(0)-'0'-1 || s[1].charAt(0)-s[0].charAt(0)==2) { pln("1"); } else pln("2"); } else if(s[1].charAt(1)==s[2].charAt(1) && s[1].charAt(1)!=s[0].charAt(1)) { if(s[2].charAt(0)==s[1].charAt(0) || s[1].charAt(0)-'0'==s[2].charAt(0)-'0'-1 || s[2].charAt(0)-s[1].charAt(0)==2) { pln("1"); } else pln("2"); } else if(s[0].charAt(1)==s[2].charAt(1) && s[2].charAt(1)!=s[1].charAt(1)) { if(s[2].charAt(0)==s[0].charAt(0) || s[0].charAt(0)-'0'==s[2].charAt(0)-'0'-1 || s[2].charAt(0)-s[0].charAt(0)==2) { pln("1"); } else pln("2"); } else if(s[0].charAt(1)==s[1].charAt(1) && s[1].charAt(1)==s[2].charAt(1)) { if(s[0].charAt(0)-'0'==s[1].charAt(0)-'0'-1 && s[2].charAt(0)-'0'-1==s[1].charAt(0)-'0') { pln("0"); } else if(s[0].charAt(0)==s[1].charAt(0) || s[1].charAt(0)==s[2].charAt(0) || s[2].charAt(0)==s[0].charAt(0) || s[1].charAt(0)-s[0].charAt(0)==2 || s[2].charAt(0)-s[1].charAt(0)==2 || s[2].charAt(0)-s[0].charAt(0)==2) { pln("1"); } else if(s[0].charAt(0)-'0'==s[1].charAt(0)-'0'-1) { pln("1"); } else if(s[1].charAt(0)-'0'==s[2].charAt(0)-'0'-1) { pln("1"); } else pln("2"); } else pln("2"); } } /**/ static InputReader in=new InputReader(System.in); static OutputWriter out=new OutputWriter(System.out); public static long l() { String s=in.String(); return Long.parseLong(s); } public static void pln(String value) { System.out.println(value); } public static int i() { return in.Int(); } public static String s() { return in.String(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars== -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.Int(); return array; } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
s=input() p1=s[0:2] p2=s[3:5] p3=s[6:8] dict1={} dict1[p1[1]]=0 dict1[p2[1]]=0 dict1[p3[1]]=0 dict1[p1[1]]+=1 dict1[p2[1]]+=1 dict1[p3[1]]+=1 a1=int(p1[0]) a2=int(p2[0]) a3=int(p3[0]) l=[] l.append(a1) l.append(a2) l.append(a3) l.sort() if len(dict1)==1 : if (p1[0]==p2[0] and p2[0]==p3[0]) or (abs(l[0]-l[1])==1 and abs(l[1]-l[2])==1): print(0) else: if abs(a1-a2)==0 or abs(a1-a2)==1 or abs(a1-a2)==2 or abs(a1-a3)==0 or abs(a1-a3)==1 or abs(a1-a3)==2 or abs(a3-a2)==0 or abs(a3-a2)==1 or abs(a3-a2)==2: print(1) else: print(2) elif len(dict1)==3: print(2) else: if p1[1]==p2[1]: if abs(a1-a2)==1 or abs(a1-a2)==0 or abs(a1-a2)==2: print(1) else: print(2) elif p1[1]==p3[1] : if abs(a1-a3)==1 or abs(a1-a3)==0 or abs(a1-a3)==2: print(1) else: print(2) elif p2[1] == p3[1]: if abs(a2 - a3) == 1 or abs(a2 - a3) == 0 or abs(a2-a3)==2: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import math from collections import deque, defaultdict from sys import stdin, stdout #input = stdin.readline # print = stdout.write listin = lambda : list(map(int, input().split())) mapin = lambda : map(int, input().split()) s = list(input().split()) z = set(s) if len(z) == 1: print(0) elif len(z) == 2: print(1) else: counter = defaultdict(int) for i in s: counter[i[1]]+=1 for i in counter.items(): if i[1] == 3: a = [] for j in s: a.append(int(j[0])) a.sort() if abs(a[0] - a[1]) == 1 and a[-1] - a[1] == 1: print(0) elif abs(a[0]-a[1]) <= 2 or abs(a[-1] - a[1]) <= 2 or abs(a[-1]-a[0]) <= 2: print(1) else: print(2) exit() elif i[1] == 2: a = [] for j in s: if j[1] == i[0]: a.append(int(j[0])) if abs(a[0]-a[1]) <= 2: print(1) exit() print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a=list(input().split()) p=[] m=[] s=[] for i in a: if i[1]=='p': p.append(int(i[0])) if i[1]=='m': m.append(int(i[0])) if i[1]=='s': s.append(int(i[0])) ans=2 p.sort() m.sort() s.sort() if len(p)==2 and p[1]-p[0]<3: ans=1 if len(m)==2 and m[1]-m[0]<3: ans=1 if len(s)==2 and s[1]-s[0]<3: ans=1 if len(p)==3 and (p[1]-p[0]<3 or p[2]-p[1]<3): ans=1 if len(m)==3 and (m[1]-m[0]<3 or m[2]-m[1]<3): ans=1 if len(s)==3 and (s[1]-s[0]<3 or s[2]-s[1]<3): ans=1 if len(p)==3 and p[0]==p[1] and p[0]==p[2]: ans=0 if len(m)==3 and m[0]==m[1] and m[0]==m[2]: ans=0 if len(s)==3 and s[0]==s[1] and s[0]==s[2]: ans=0 if len(p)==3 and p[1]-p[0]==1 and p[2]-p[1]==1: ans=0 if len(m)==3 and m[1]-m[0]==1 and m[2]-m[1]==1: ans=0 if len(s)==3 and s[1]-s[0]==1 and s[2]-s[1]==1: ans=0 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
hand = raw_input().split() hand.sort() same = 1 pordered = 1 sordered = 1 mordered = 1 ps = [] ms = [] ss = [] if hand[0] == hand[1] or hand[0] == hand[2] or hand[1] == hand[2]: #if any two are the same same+=1 if hand[0] == hand[1] == hand[2]: #if all the same same+=1 for i in hand: if i[1] == 'p': ps.append(i) elif i[1] == 'm': ms.append(i) elif i[1] == 's': ss.append(i) prev = -5 flag = True for i in ps: if int(i[0]) - prev == 1: pordered+=1 flag = False elif int(i[0]) - prev == 2 and flag: pordered += 1 break prev = int(i[0]) prev = -5 for i in ss: if int(i[0]) - prev == 1: sordered+=1 flag = False elif int(i[0]) - prev == 2 and flag: sordered += 1 break prev = int(i[0]) prev = -5 for i in ms: if int(i[0]) - prev == 1: mordered+=1 flag = False elif int(i[0]) - prev == 2 and flag: mordered += 1 break prev = int(i[0]) print 3 - (max(same, pordered, sordered, mordered))
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
tiles = raw_input().split() koutsu = shuntsu = 2 for tile in tiles: if tiles.count(tile) == 3: koutsu = 0 break if tiles.count(tile) == 2: koutsu = 1 if koutsu > 0: for tile in tiles: neighb = 0 num, mast = int(tile[0]), tile[1] if num == 9: continue if str(num+1) + mast in tiles: neighb += 1 if num < 8 and str(num+2) + mast in tiles: neighb += 1 if neighb == 2: shuntsu = 0 break if neighb == 1: shuntsu = 1 print min(koutsu, shuntsu)
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; int i, j, aae = 0, co = 0; int ar[4]; char ss; cin >> a >> b >> c; if (a[1] != b[1]) co++; if (b[1] != c[1]) co++; if (c[1] != a[1]) co++; ar[0] = a[0] - '0'; ar[1] = b[0] - '0'; ar[2] = c[0] - '0'; if (co == 0) { if (ar[0] != ar[1]) aae++; if (ar[1] != ar[2]) aae++; if (ar[2] != ar[0]) aae++; if (aae == 0) cout << "0\n"; else { if (aae == 2) cout << "1\n"; else { aae = 0; int QQ = 3; sort(ar, ar + QQ); if ((ar[0] + 1) != ar[1]) aae++; if ((ar[1] + 1) != ar[2]) aae++; if (aae > 1) { if ((ar[0] + 2 == ar[1]) || (ar[1] + 2 == ar[2])) aae--; } cout << aae << '\n'; } } } else if (co == 2) { if (a[1] == b[1] || a[1] == c[1]) { ss = a[1]; ar[0] = a[0] - '0'; if (a[1] == b[1]) ar[1] = b[0] - '0'; else ar[1] = c[0] - '0'; } else { ss = b[1]; ar[0] = b[0] - '0'; ar[1] = c[0] - '0'; } i = 2; sort(ar, ar + i); if (ar[0] == ar[1]) cout << "1\n"; else if (((ar[0] + 1) == ar[1]) || ((ar[0] + 2) == ar[1])) cout << "1\n"; else cout << "2\n"; } else cout << "2\n"; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
tiles = { 'm': [], 'p': [], 's': [] } for tile in input().split(): tiles[tile[1]].append(int(tile[0])) max_ava = 0 for suit in tiles: # max for seq for x in range(1, 10): seq = 0 if x in tiles[suit]: seq += 1 if x+1 in tiles[suit]: seq += 1 if x+2 in tiles[suit]: seq += 1 max_ava = max(max_ava, seq) # max for grp for x in range(1, 10): count = tiles[suit].count(x) max_ava = max(max_ava, count) print(3 - max_ava)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; int ans = 2; char x, y, z; cin >> a; x = getchar(); cin >> b; y = getchar(); cin >> c; z = getchar(); vector<int> v; v.push_back(a); v.push_back(b); v.push_back(c); if (x == y && y == z) { sort(v.begin(), v.end()); a = v[0]; b = v[1]; c = v[2]; if (b - a == 1 || c - b == 1) { ans--; if (c - b == 1 && b - a == 1) ans--; } else if (a == b || b == c) { ans--; if (b == c && a == b) ans--; } else if (b - a == 2 || c - b == 2) ans--; } else if (x == y) { if (a - b == 1 || b - a == 1 || a - b == 2 || b - a == 2 || b == a) ans--; } else if (x == z) { if (a - c == 1 || c - a == 1 || a - c == 2 || c - a == 2 || c == a) ans--; } else if (z == y) { if (c - b == 1 || b - c == 1 || c - b == 2 || b - c == 2 || b == c) ans--; } cout << ans << endl; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int mod = 1e9 + 7; const double eps = 1e-15; const double pi = acos(-1); const int INF = 0x3f3f3f; long long read() { long long c = getchar(), Nig = 1, x = 0; while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') Nig = -1, c = getchar(); while (isdigit(c)) x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar(); return Nig * x; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int s[20], m[20], p[20]; int main() { for (int i = 1; i <= 3; i++) { string ss; cin >> ss; if (ss[1] == 's') s[(ss[0] - '0')]++; else if (ss[1] == 'p') p[ss[0] - '0']++; else m[ss[0] - '0']++; } int mx1 = 0, mx2 = 0, mx3 = 0; int l1 = 0, l2 = 0, l3 = 0; int n = 3; for (int i = 1; i <= 9; i++) { if (s[i] > mx1) mx1 = s[i]; if (s[i] && s[i + 1] && s[i + 2]) l1 = 3; else if (s[i] && s[i + 1] || s[i] && s[i + 2]) l1 = max(l1, 2); else if (s[i]) l1 = max(l1, 1); } int ans = min(n - mx1, n - l1); for (int i = 1; i <= 9; i++) { if (m[i] > mx2) mx2 = m[i]; if (m[i] && m[i + 1] && m[i + 2]) l2 = 3; else if (m[i] && m[i + 1] || m[i] && m[i + 2]) l2 = max(l2, 2); else if (m[i]) l2 = max(l2, 1); } ans = min(ans, min(n - mx2, n - l2)); for (int i = 1; i <= 9; i++) { if (p[i] > mx3) mx3 = p[i]; if (p[i] && p[i + 1] && p[i + 2]) l3 = 3; else if (p[i] && p[i + 1] || p[i] && p[i + 2]) l3 = max(l3, 2); else if (p[i]) l3 = max(l3, 1); } ans = min(ans, min(n - mx3, n - l3)); printf("%d\n", ans); }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a = [str(i) for i in input().split()] a.sort() first = a[0] second = a[1] third = a[2] firstnum = int(first[0]) secondnum = int(second[0]) thirdnum = int(third[0]) if(first == second): if(second == third): print(0) else: print(1) elif(second == third): print(1) elif(first[1] == second[1] and second[1] == third[1]): if(firstnum +1 == secondnum and secondnum + 1 == thirdnum): print(0) elif(firstnum + 1 == secondnum or firstnum + 2 == secondnum): print(1) elif(secondnum + 1 == thirdnum or secondnum + 2 == thirdnum): print(1) else: print(2) elif(first[1] == second[1] and (firstnum + 1 == secondnum or firstnum+2 == secondnum)): print(1) elif(second[1] == third[1] and (secondnum + 1 == thirdnum or secondnum+2 == thirdnum)): print(1) elif(first[1] == third[1] and (firstnum + 1 == thirdnum or firstnum + 2 == thirdnum)): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
//make sure to make new file! import java.io.*; import java.util.*; public class B573{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String s = f.readLine(); StringTokenizer st = new StringTokenizer(s); int[] ctoi = new int[26]; ctoi['s'-'a'] = 0; ctoi['m'-'a'] = 1; ctoi['p'-'a'] = 2; int max = 0; int[][] freq = new int[10][3]; for(int k = 0; k < 3; k++){ String cur = st.nextToken(); int i = Character.getNumericValue(cur.charAt(0)); char c = cur.charAt(1); freq[i][ctoi[c-'a']] ++; max = Math.max(freq[i][ctoi[c-'a']],max); } for(int k = 2; k <= 8; k++){ int counts = 0; int countm = 0; int countp = 0; for(int j = k-1; j <= k+1; j++){ if(freq[j][ctoi['s'-'a']] > 0) counts++; if(freq[j][ctoi['m'-'a']] > 0) countm++; if(freq[j][ctoi['p'-'a']] > 0) countp++; } max = Math.max(max,counts); max = Math.max(max,countm); max = Math.max(max,countp); } //check for 0 int answer = 3-max; out.println(answer); out.close(); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
x, y, z = sorted(int(n)+9*ord(s) for n,s in input().split()) D = {y-x, z-y} print(2 - bool(D&{0,1,2}) - (D<{0,1}))
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { char a[5], b[5], aa[5][5]; for (int i = 1; i <= 3; i++) { scanf("%s", aa[i]); a[i] = aa[i][0]; b[i] = aa[i][1]; } if ((a[1] == a[2]) && (a[2] == a[3]) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (!(strcmp(aa[1], aa[2])) || !strcmp(aa[2], aa[3]) || !strcmp(aa[1], aa[3])) { printf("1\n"); return 0; } int c[5], d[5]; for (int i = 1; i <= 3; i++) { c[i] = a[i] - 48; d[i] = a[i] - 48; } sort(c + 1, c + 4); if (((c[3] - c[2]) == 1) && ((c[2] - c[1]) == 1) && (b[1] == b[2]) && (b[2] == b[3])) { printf("0\n"); return 0; } if (b[1] == b[2]) { if ((abs(d[1] - d[2]) == 1) || (abs(d[1] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[2] == b[3]) { if ((abs(d[2] - d[3]) == 1) || (abs(d[3] - d[2]) == 2)) { printf("1\n"); return 0; } } if (b[1] == b[3]) { if ((abs(d[1] - d[3]) == 1) || (abs(d[1] - d[3]) == 2)) { printf("1\n"); return 0; } } printf("2\n"); return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.util.*; import java.lang.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.*; public class Main { public static void main(String[] args) throws Exception{ FastReader sc=new FastReader(); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); Main mm=new Main(); ArrayList<Integer>[] list=new ArrayList[3]; for(int i=0;i<3;i++) { list[i]=new ArrayList<Integer>(); } for(int i=0;i<3;i++) { String s=sc.next(); if(s.charAt(1)=='s') { list[0].add(s.charAt(0)-'0'); } else if(s.charAt(1)=='m') { list[1].add(s.charAt(0)-'0'); } else if(s.charAt(1)=='p') { list[2].add(s.charAt(0)-'0'); } } for(int i=0;i<3;i++) { Collections.sort(list[i]); } int ans=2; for(int i=0;i<3;i++) { if(list[i].size()==3 && list[i].get(0)==list[i].get(1) && list[i].get(1)==list[i].get(2) ) { ans=0; } else if(list[i].size()==3 && list[i].get(0)==list[i].get(1)-1 && list[i].get(1)==list[i].get(2)-1) { ans=0; } else if(list[i].size()==3 && (list[i].get(0)==list[i].get(1) || list[i].get(1)==list[i].get(2) || list[i].get(0)==list[i].get(2))) { if(ans>1) { ans=1; } } else if(list[i].size()==3 && (list[i].get(1)-list[i].get(0)<=2 || list[i].get(2)-list[i].get(1)<=2)) { if(ans>1) { ans=1; } } else if(list[i].size()==2 && list[i].get(1)-list[i].get(0)<=2){ if(ans>1) { ans=1; } } } System.out.println(ans); } } class node{ int val; int index; node(int val,int index){ this.val=val; this.index=index; } } class a implements Comparator<node>{ public int compare(node n1,node n2) { return n1.val-n2.val; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a=input().split() count=[0,0,0] alp=['m','p','s'] for i in range(3): if a[i][1]=='m': count[0]+=1 elif a[i][1]=='p': count[1]+=1 else: count[2]+=1 if max(count)==3: d=[0,0,0] d[0]=abs(int(a[0][0])-int(a[1][0])) d[1]=abs(int(a[0][0])-int(a[2][0])) d[2]=abs(int(a[1][0])-int(a[2][0])) d.sort() if d[0]<=2: if d==[0,0,0]: print(0) elif d==[1,1,2]: print(0) else : print(1) else: print(2) elif max(count)==2: i=0 l=[0,0] while i<3: if count[i]==2: break; i+=1 ct=0; for j in range(3): if a[j][1]==alp[i]: l[ct]=int(a[j][0]); ct+=1 if abs(l[0]-l[1])<=2: print(1) else: print(2) else: print(2);
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Stack; import java.util.regex.Pattern; public class ROUGH { public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static void main(String[] args) { FastReader sc = new FastReader(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); char[] c = sc.next().toCharArray(); List<Integer> M = new ArrayList<>(); List<Integer> P = new ArrayList<>(); List<Integer> S = new ArrayList<>(); if(a[1] == 'm') { M.add(a[0]-'0'); }else if(a[1] == 'p') { P.add(a[0]-'0'); }else { S.add(a[0]-'0'); } if(b[1] == 'm') { M.add(b[0]-'0'); }else if(b[1] == 'p') { P.add(b[0]-'0'); }else { S.add(b[0]-'0'); } if(c[1] == 'm') { M.add(c[0]-'0'); }else if(c[1] == 'p') { P.add(c[0]-'0'); }else { S.add(c[0]-'0'); } Collections.sort(M); Collections.sort(P); Collections.sort(S); // out.println(M); // out.println(P); // out.println(S); int ans = Integer.MAX_VALUE; int one = 0; int zero = 0; int two = 0; if(M.size() == 0) ans = Math.min(ans, 3); else if(M.size() == 1) ans = Math.min(ans, 2); for(int i=0;i+1<M.size();++i) { if(M.get(i+1)-M.get(i) == 1) ++one; else if(M.get(i+1)-M.get(i) == 0) ++zero; else if(M.get(i+1)-M.get(i) == 2) ++two; } if(one == 0) ans = Math.min(ans, 2); else if(one == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(zero == 0) ans = Math.min(ans, 2); else if(zero == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(two>0) ans = Math.min(ans, 1); one = 0; zero = 0; two = 0; if(P.size() == 0) ans = Math.min(ans, 3); else if(P.size() == 1) ans = Math.min(ans, 2); for(int i=0;i+1<P.size();++i) { if(P.get(i+1)-P.get(i) == 1) ++one; else if(P.get(i+1)-P.get(i) == 0) ++zero; else if(P.get(i+1)-P.get(i) == 2) ++two; } if(one == 0) ans = Math.min(ans, 2); else if(one == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(zero == 0) ans = Math.min(ans, 2); else if(zero == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(two>0) ans = Math.min(ans, 1); one = 0; zero =0 ; two = 0; if(S.size() == 0) ans = Math.min(ans, 3); else if(S.size() == 1) ans = Math.min(ans, 2); for(int i=0;i+1<S.size();++i) { if(S.get(i+1)-S.get(i) == 1) ++one; else if(S.get(i+1)-S.get(i) == 0) ++zero; else if(S.get(i+1)-S.get(i) == 2) ++two; } //out.println(one); if(one == 0) ans = Math.min(ans, 2); else if(one == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(zero == 0) ans = Math.min(ans, 2); else if(zero == 1) ans = Math.min(ans, 1); else ans = Math.min(ans, 0); if(two>0) ans = Math.min(ans, 1); out.println(ans); out.close(); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.util.*; import java.io.*; import java.math.*; public class Solution{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); String s1 = st.nextToken(); String s2 = st.nextToken(); String s3 = st.nextToken(); int res = 2; if(s1.equals(s2)&&s2.equals(s3)) res = 0; else if(s1.equals(s2)||s2.equals(s3)||s1.equals(s3)) res = 1; int a1 = Integer.valueOf(s1.substring(0,1)); int a2 = Integer.valueOf(s2.substring(0,1)); int a3 = Integer.valueOf(s3.substring(0,1)); char c1 = s1.charAt(1); char c2 = s2.charAt(1); char c3 = s3.charAt(1); if(c1==c2&&c2==c3){ int[] b = new int[3]; b[0] = a1; b[1] = a2; b[2] = a3; Arrays.sort(b); if(b[1]==b[0]+1&&b[2]==b[1]+1) res = 0; else if(b[1]-b[0]<=2||b[2]-b[1]<=2) res = Math.min(res,1); }else if(c1==c2){ if(Math.abs(a1-a2)<=2) res = Math.min(res,1); }else if(c2==c3){ if(Math.abs(a2-a3)<=2) res = Math.min(res,1); }else if(c1==c3){ if(Math.abs(a1-a3)<=2) res = Math.min(res,1); } System.out.println(res); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from math import * import sys input = lambda: sys.stdin.readline().strip() d = {'m': [], 's': [], 'p': []} ls = list(input().split()) for i in ls: d[i[1]].append(int(i[0])) for k, v in d.items(): v.sort() if len(v)==3 and len(set(v))==1: print(0); break if len(v)==3 and v[0]+1==v[1] and v[1]+1==v[2]: print(0); break else: for k, v in d.items(): if len(v)==2 and len(set(v))==1: print(1); break if len(v)==2 and v[1]-v[0]<=2: print(1); break if len(v)==3 and (v[0]==v[1] or v[1]==v[2]): print(1); break if len(v)==3 and (v[1]-v[0]<=2 or v[2]-v[1]<=2): print(1); break else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { String arr[]={sc.next(),sc.next(),sc.next()}; int m[]=new int[20],k[]=new int[20],p[]=new int[20]; for(String x : arr){ char ch=x.charAt(1); int take[]; if(ch=='m') take=m; else if(ch=='s') take=k; else take=p; take[(int)(x.charAt(0)-'0')]++; } //System.out.println(Arrays.toString(p)); int cnt1=Integer.MAX_VALUE,sum=0; for(int i=1;i<=9;i++){ sum=Math.max(sum,Math.max(m[i], (m[i]!=0?1:0)+(m[i+1]!=0?1:0)+(m[i+2]!=0?1:0))); } cnt1=Math.min(cnt1, 3-sum); sum=0;m=k; for(int i=1;i<=9;i++){ sum=Math.max(sum,Math.max(m[i], (m[i]!=0?1:0)+(m[i+1]!=0?1:0)+(m[i+2]!=0?1:0))); } cnt1=Math.min(cnt1, 3-sum); sum=0;m=p; for(int i=1;i<=9;i++){ sum=Math.max(sum,Math.max(m[i], (m[i]!=0?1:0)+(m[i+1]!=0?1:0)+(m[i+2]!=0?1:0))); } cnt1=Math.min(cnt1, 3-sum); pn(cnt1); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new AnotherReader(); boolean oj = true; oj = System.getProperty("ONLINE_JUDGE") != null; if(!oj) sc=new AnotherReader(100); long s = System.currentTimeMillis(); int t=1; //t=ni(); while(t-->0) process(); out.flush(); if(!oj) System.out.println(System.currentTimeMillis()-s+"ms"); System.out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static boolean multipleTC=false; static long mod=(long)1e9+7l; static void r_sort(int arr[],int n){ Random r = new Random(); for (int i = n-1; i > 0; i--){ int j = r.nextInt(i+1); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } Arrays.sort(arr); } static long mpow(long x, long n) { if(n == 0) return 1; if(n % 2 == 0) { long root = mpow(x, n / 2); return root * root % mod; }else { return x * mpow(x, n - 1) % mod; } } static long mcomb(long a, long b) { if(b > a - b) return mcomb(a, a - b); long m = 1; long d = 1; long i; for(i = 0; i < b; i++) { m *= (a - i); m %= mod; d *= (i + 1); d %= mod; } long ans = m * mpow(d, mod - 2) % mod; return ans; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import sys a,b,c=sys.stdin.readline().strip().split() if a==b and b==c: print(0) elif a==b or b==c or a==c: print(1) else: na = int(a[0]) nb = int(b[0]) nc = int(c[0]) if (a[1]==b[1] and a[1]==c[1]): cp=[na,nb,nc] cp.sort() cp[0]+=2 cp[1]+=1 if (cp[0]==cp[1] and cp[1]==cp[2]): print("0") elif (cp[0]==cp[1] or cp[1]==cp[2] or cp[0]==cp[1] or (cp[0]+1)==cp[1] or (cp[1]+1)==cp[2]): print("1") else: print("2") elif(a[1]==b[1]): mi=min(na,nb) ma=max(na,nb) if (mi==(ma-1) or mi==(ma-2)): print("1") else: print("2") elif(a[1]==c[1]): mi=min(na,nc) ma=max(na,nc) if (mi==(ma-1) or mi==(ma-2)): print("1") else: print("2") elif(b[1]==c[1]): mi = min(nb,nc) ma = max(nb,nc) if (mi==(ma-1) or mi==(ma-2)): print("1") else: print("2") else: print("2")
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def letter(i): return(i[1],i[0]) arr=[(i) for i in filter(None, input().split(" "))] a = sorted(arr, key=letter) min = 2 t = len(a) num=int(a[0][0]) letter = a[0][1] if a[0]==a[1]: min=1 if a[1]==a[2]: min=0 if int(a[0][0])+1==int(a[1][0])and (a[1][1]==letter): if min>0: min=1 if int( a[2][0])==num+2 and(a[2][1]==letter): min=0 if int(a[0][0])+2==int(a[1][0])and (a[1][1]==letter): if min>0: min=1 letter=a[1][1] num=int(a[1][0]) if a[1]==a[2]: if min>0: min=1 if int(a[1][0])+1==int(a[2][0])and (a[2][1]==letter): if min>0: min=1 if int(a[1][0])+2==int(a[2][0])and (a[2][1]==letter): if min>0: min=1 print(min)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import sys import math from collections import defaultdict #sys.stdin=open('input.txt','r') #sys.stdout=open('output.txt','w') a=list(input().split()) d={} b=[] c=0 for x in a: if x[1] in d: d[x[1]].append(int(x[0])) else: d[x[1]]=[int(x[0])] b.append(x[1]) c+=1 if c==1: t=b[0] d[b[0]].sort() if ((d[t][1]-d[t][0])==1 and (d[t][2]-d[t][1])==1) or ((d[t][1]-d[t][0])==0 and (d[t][2]-d[t][1])==0): print('0') elif (d[t][1]-d[t][0])==1 or (d[t][2]-d[t][1])==1 or (d[t][2]-d[t][1])==0 or (d[t][0]-d[t][1])==0 or (d[t][2]-d[t][0])==0 or abs(d[t][2]-d[t][1])==2 or abs(d[t][2]-d[t][0])==2 or abs(d[t][0]-d[t][1])==2: print('1') else: print('2') elif c==2: t1=b[0] t2=b[1] l1=len(d[t1]) l2=len(d[t2]) if l1==2 and (abs(d[t1][0]-d[t1][1])==1 or abs(d[t1][0]-d[t1][1])==0 or abs(d[t1][0]-d[t1][1])==2): print('1') elif l2==2 and (abs(d[t2][0]-d[t2][1])==1 or abs(d[t2][0]-d[t2][1])==0 or abs(d[t2][0]-d[t2][1])==2): print('1') else: print('2') else: print('2')
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from collections import defaultdict def main(): l=map(str,raw_input().split()) alp=[] if l.count(l[0])==3: print 0 exit() if (l.count(l[0])== 2 or l.count(l[1])== 2): print 1 exit() for i in l: n,a=map(str," ".join(i).split()) alp.append(n) alp.append(a) b=[] if alp.count("s")==3 or alp.count("m")==3 or alp.count("p")==3: b.append(int(alp[0])) b.append(int(alp[2])) b.append(int(alp[4])) b.sort() if (b[0]+1)==b[1] and (b[1]+1 ==b[2]): print 0 exit() if(b[0]+1)==b[1] or (b[1]+1 ==b[2]): print 1 exit() if(b[0]+2)==b[1] or (b[1]+2 ==b[2]): print 1 exit() if alp.count("s")==2 or alp.count("m")==2 or alp.count("p")==2: if (alp[1]==alp[3]): b.append(int(alp[0])) b.append(int(alp[2])) if (alp[1]==alp[5]): b.append(int(alp[0])) b.append(int(alp[4])) if (alp[5]==alp[3]): b.append(int(alp[4])) b.append(int(alp[2])) b.sort() if(b[0]+1)==b[1] or (b[0]+2) ==b[1]: print 1 exit() print 2 main()
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
arr=list(map(str,input().split())) num1=int(arr[0][0]) num2=int(arr[1][0]) num3=int(arr[2][0]) str1=arr[0][1] str2=arr[1][1] str3=arr[2][1] if str1==str2 and str2==str3: if num1==num2 and num2==num3: print(0) else: arr2=[num1,num2,num3] arr2=sorted(arr2) if arr2[0]==arr2[1]-1 and arr2[1]==arr2[2]-1: print(0) else: if 0<=abs(num1-num2)<=2: print(1) elif 0<=abs(num1-num3)<=2: print(1) elif 0<=abs(num2-num3)<=2: print(1) else: print(2) elif str1==str2: if abs(num1-num2)==0 or abs(num1-num2)==1 or abs(num1-num2)==2: print(1) else: print(2) elif str1==str3: if abs(num1-num3)==0 or abs(num1-num3)==1 or abs(num1-num3)==2: print(1) else: print(2) elif str2==str3: if abs(num2-num3)==0 or abs(num2-num3)==1 or abs(num2-num3)==2: print(1) else: print(2) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int arr[3]; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; if ((s1[1] == s2[1] && s2[1] == s3[1]) && (s1[0] == s2[0] && s2[0] == s3[0])) { cout << 0 << endl; } else if (s1[1] == s2[1] && s2[1] == s3[1]) { arr[0] = (int)s1[0]; arr[1] = (int)s2[0]; arr[2] = (int)s3[0]; sort(arr, arr + 3); if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) cout << 0 << endl; else if (arr[0] + 1 == arr[1] || arr[1] + 1 == arr[2] || arr[0] == arr[1] || arr[1] == arr[2]) cout << 1 << endl; else if (arr[0] + 1 == arr[1] || arr[0] + 2 == arr[1] || arr[1] + 1 == arr[2] || arr[1] + 2 == arr[2]) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s2[1]) { int x = (int)s1[0]; int y = (int)s2[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s1[1] == s3[1]) { int x = (int)s1[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] == s3[1]) { int x = (int)s2[0]; int y = (int)s3[0]; if (x > y) { int z = y; y = x; x = z; } if (x == y || x + 1 == y || x + 2 == y) cout << 1 << endl; else cout << 2 << endl; } else if (s2[1] != s3[1] && s1[1] != s3[1] && s2[1] != s3[1]) { cout << 2 << endl; } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from collections import Counter X, Temp = input()[::-1].split(), 1 MyDict = Counter(X) if 3 in MyDict.values(): print(0) exit() Key = sorted(MyDict.keys()) Max = max(MyDict.values()) for i in range(1, len(Key)): Diff = int(Key[i][1]) - int(Key[i - 1][1]) if Key[i][0] == Key[i - 1][0] and Diff <=2 : if Diff == 2: print(1) exit() Temp += 1 else: Temp = 1 Max = max(Max, Temp) print(max(0, 3 - Max)) # Show you deserve being the best to whom doesn't believe in you.
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3); if (s[0] == s[1] && s[1] == s[2]) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) && (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1])) { cout << "0" << endl; return 0; } if ((s[1][0] - s[0][0] == 1 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 1 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 1 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } if (s[1] == s[0] || s[2] == s[0] || s[1] == s[2]) { cout << "1" << endl; return 0; } if ((s[1][0] - s[0][0] == 2 && s[1][1] == s[0][1]) || (s[2][0] - s[1][0] == 2 && s[2][1] == s[1][1]) || (s[2][0] - s[0][0] == 2 && s[2][1] == s[0][1])) { cout << "1" << endl; return 0; } cout << "2"; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
t = [[],[],[]] l = 3 * [0] a = raw_input().split() for x in a: if x[1] == "m": t[0].append(int(x[0])) l[0] += 1 elif x[1] == "p": t[1].append(int(x[0])) l[1] += 1 elif x[1] == "s": t[2].append(int(x[0])) l[2] += 1 if max(l) == 1: print 2 elif max(l) == 2: x = l.index(max(l)) a, b = t[x] if abs(a-b) < 3: print 1 else: print 2 else: x = l.index(max(l)) a, b, c = t[x] if a == b == c or (len(set(t[x])) == 3 and a + b + c - 3 * min(a, b, c) == 3): print 0 elif abs(a-b) <= 2 or abs(b-c)<= 2 or abs(a-c) <= 2: print 1 else: print 2
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
s = input().split() s = sorted(s) s1, s2, s3 = s[0], s[1], s[2] if s1 == s2 == s3: print(0) elif (int(s1[0]) + 2 == int(s2[0]) + 1 == int(s3[0])) and (s1[1] == s2[1] == s3[1]): print(0) else: if s1 == s2 or s1 == s3 or s2 == s3: print(1) elif (((int(s1[0]) + 1 == int(s2[0])) or (int(s1[0]) + 2 == int(s2[0]))) and s1[1] == s2[1]) or (((int(s1[0]) + 1 == int(s3[0])) or (int(s1[0]) + 2 == int(s3[0]))) and s1[1] == s3[1]) or (((int(s2[0]) + 1 == int(s3[0])) or (int(s2[0]) + 2 == int(s3[0]))) and s2[1] == s3[1]): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; void print(vector<pair<int, int> > &v) { for (long long int i = 0; i < v.size(); ++i) cout << v[i].first << " " << v[i].second << endl; } int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } int main() { string a, b, c; cin >> a >> b >> c; if (a.compare(b) == 0 && b.compare(c) == 0) cout << 0; else { if (a[1] != b[1] && b[1] != c[1] && c[1] != a[1]) cout << 2; else if (a[1] == b[1] && b[1] == c[1]) { int x = a[0] - '0', y = b[0] - '0', z = c[0] - '0'; if (x == y || y == z || z == x) cout << 1; else { int a = min(min(x, y), z); int c = max(max(x, y), z); int b = x + y + z - (a + c); if (abs(b - a) == 1 && abs(c - b) == 1) cout << 0; else if (b == (a + 1) || b == (a + 2) || c == (b + 1) || c == (b + 2)) cout << 1; else cout << 2; } } else if (a[1] == b[1]) { int x = a[0] - '0', y = b[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (b[1] == c[1]) { int x = b[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } else if (a[1] == c[1]) { int x = a[0] - '0', y = c[0] - '0'; if (abs(x - y) == 0 || abs(x - y) == 1 || abs(x - y) == 2) cout << 1; else cout << 2; } } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def find(): s = input() s = s.split() d = {'m':[0 for i in range(10)], 'p':[0 for i in range(10)], 's':[0 for i in range(10)]} #m,p,s for e in s: d[e[1]][int(e[0])] += 1 res = 2 for e in d: # m = 3 e = d[e] for i in range(10): if e[i] == 3: print(0) exit() elif e[i] == 2: if res > 1: res = 1 if i < 8: if e[i] > 0: if e[i+1] > 0 and e[i+2] > 0: print(0) exit() elif e[i+1] > 0 or e[i+2] > 0: if res > 1: res = 1 elif i<9: if e[i]>0 and e[i+1]>0: if res > 1: res = 1 return res a = find() print(a)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int a[5]; char c[4]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 3; i++) { cin >> c; a[i] = c[0] - '0' + (c[1] - 'a') * 10; } sort(a, a + 3); int t1 = a[1] - a[0], t2 = a[2] - a[1], t3 = a[2] - a[0]; if (t1 == 0 && t2 == 0) { cout << 0; return 0; } else if (t1 == 1 && t2 == 1) { cout << 0; return 0; } else if (t1 == 2 || t2 == 2) { cout << 1; return 0; } else if (t1 == 1 || t2 == 1) { cout << 1; return 0; } else if (t1 == 0 || t2 == 0) { cout << 1; return 0; } else cout << 2; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a = input().split() r = 2 for i in range(48,58): for c in "mps": r = min(r,max(0,3-a.count(chr(i)+c))) r = min(r,(not chr(i-1)+c in a)+(not chr(i)+c in a)+(not chr(i+1)+c in a)) print(r)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c = map(str, input().split()) a = a[1]+a[0] b = b[1]+b[0] c = c[1]+c[0] n = [a,b,c] m = [0]*9 p = [0]*9 s = [0]*9 for i in range(3): if(n[i][0]=="m"): m[int(n[i][1])-1]+=1 if(n[i][0]=="p"): p[int(n[i][1])-1]+=1 if(n[i][0]=="s"): s[int(n[i][1])-1]+=1 ans = 2 #print(m,p,s) for i in range(9): if(3-m[i]<ans): ans = 3-m[i] if(0<i and i<8): if(3-min(1,m[i-1])-min(1,m[i])-min(1,m[i+1])<ans): ans = 3-min(1,m[i-1])-min(1,m[i])-min(1,m[i+1]) if(3-s[i]<ans): ans = 3-s[i] if(0<i and i<8): if(3-min(1,s[i-1])-min(1,s[i])-min(1,s[i+1])<ans): #print(23) ans = 3-min(1,s[i-1])-min(1,s[i])-min(1,s[i+1]) #print(ans, 9) if(3-p[i]<ans): ans = 3-p[i] #print(3-s[i-1]-s[i]-s[i+1]) if(0<i and i<8): if(3-min(1,p[i-1])-min(1,p[i])-min(1,p[i+1])<ans): ans = 3-min(1,p[i-1])-min(1,p[i])-min(1,p[i+1]) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c=input().split() d=[] d.append(int(a[0])) d.append(int(b[0])) d.append(int(c[0])) d.sort() if(a==b and b==c): print(0) elif(a[1]==b[1] and b[1]==c[1] and d[1]-d[0]==1 and d[2]-d[1]==1): print(0) elif(a==b or b==c or a==c): print(1) elif((a[1]==b[1] and abs(int(a[0])-int(b[0]))<=2) or (c[1]==b[1] and abs(int(c[0])-int(b[0]))<=2) or (a[1]==c[1] and abs(int(a[0])-int(c[0]))<=2) ): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c=map(str,input().split()) s=[0]*10 m=[0]*10 p=[0]*10 if list(a)[1]=="s": s[int(list(a)[0])]+=1 elif list(a)[1]=="m": m[int(list(a)[0])]+=1 elif list(a)[1]=="p": p[int(list(a)[0])]+=1 if list(b)[1]=="s": s[int(list(b)[0])]+=1 elif list(b)[1]=="m": m[int(list(b)[0])]+=1 elif list(b)[1]=="p": p[int(list(b)[0])]+=1 if list(c)[1]=="s": s[int(list(c)[0])]+=1 elif list(c)[1]=="m": m[int(list(c)[0])]+=1 elif list(c)[1]=="p": p[int(list(c)[0])]+=1 m1=0 for i in range(10): if m[i]>m1: m1=m[i] if p[i]>m1: m1=p[i] if s[i]>m1: m1=s[i] m2=0 for i in range(10): if s[i]>1: s[i]=1 if p[i]>1: p[i]=1 if m[i]>1: m[i]=1 for i in range(8): if sum(m[i:i+3])>m2: m2=sum(m[i:i+3]) if sum(p[i:i+3])>m2: m2=sum(p[i:i+3]) if sum(s[i:i+3])>m2: m2=sum(s[i:i+3]) print(3-max(m1,m2))
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a = input().split() r = 2 for x in a: r = min(r,max(0,3-a.count(x))) if(x[0] < '8'): r = min(r,(not x.replace(x[0],chr(ord(x[0])+1),1) in a)+(not x.replace(x[0],chr(ord(x[0])+2),1) in a)) if('1' < x[0] < '9'): r = min(r,(not x.replace(x[0],chr(ord(x[0])-1),1) in a)+(not x.replace(x[0],chr(ord(x[0])+1),1) in a)) print(r)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.util.Scanner; public class B1191 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][] stats = new int[26][9]; for (int i=0; i<3; i++) { String tile = in.next(); stats[tile.charAt(1)-'a'][tile.charAt(0)-'1']++; } int max = 0; for (int c=0; c<26; c++) { for (int d=0; d<9; d++) { max = Math.max(max, stats[c][d]); } } int minKoutsu = 3-max; max = 0; for (int c=0; c<26; c++) { for (int d=2; d<9; d++) { int count = 0; for (int i=-2; i<=0; i++) { if (stats[c][d+i] > 0) { count++; } } max = Math.max(max, count); } } int minShuntsu = 3-max; int answer = Math.min(minKoutsu, minShuntsu); System.out.println(answer); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } int ans = 1e9 + 7; bool check0(vector<string> tmp) { if (tmp.size() < 3) return false; string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b && b == c) return true; if (c[0] - b[0] == 1 && b[0] - a[0] == 1) return true; return false; } bool check1(vector<string> tmp) { if (tmp.size() < 2) return false; if (tmp.size() == 2) { string a = tmp[0], b = tmp[1]; if (a == b) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } string a = tmp[0], b = tmp[1], c = tmp[2]; if (a == b || b == c) return true; if (c[0] - b[0] == 1 || c[0] - b[0] == 2) return true; if (b[0] - a[0] == 1 || b[0] - a[0] == 2) return true; return false; } int main() { vector<string> mm, pp, second; for (int i = 0; i < 3; i++) { string s; cin >> s; if (s[1] == 'm') mm.push_back(s); if (s[1] == 'p') pp.push_back(s); if (s[1] == 's') second.push_back(s); } sort(begin(mm), end(mm)); sort(begin(pp), end(pp)); sort(begin(second), end(second)); if (check0(mm) || check0(pp) || check0(second)) return cout << 0 << endl, 0; if (check1(mm) || check1(pp) || check1(second)) return cout << 1 << endl, 0; cout << 2 << endl; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#! /usr/bin/env python3 if __name__ == '__main__': res = 3 pai = input().split() M = [0,0,0,0,0,0,0,0,0] S = [0,0,0,0,0,0,0,0,0] P = [0,0,0,0,0,0,0,0,0] for s in pai: if s[1]=='m': M[int(s[0])-1] += 1 elif s[1]=='s': S[int(s[0])-1] += 1 else: P[int(s[0])-1] += 1 for i in range(9): if i > 1: cnt = 0 for j in range(3): cnt += (M[i-j] != 0) res = min(res,3-cnt) res = min(res,3 - M[i]) for i in range(9): if i > 1: cnt = 0 for j in range(3): cnt += (P[i-j] != 0) res = min(res,3-cnt) res = min(res,3 - P[i]) for i in range(9): if i > 1: cnt = 0 for j in range(3): cnt += (S[i-j] != 0) res = min(res,3-cnt) res = min(res,3 - S[i]) print(res)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from collections import defaultdict st=input() st2=st.replace(" ","") g=defaultdict(list) for x in range(0,7): if(x%2==1): g[st2[x]].append(int(st2[x-1])) for x in g: g[x].sort() if(len(g)==3): print (2) elif(len(g)==2): for x in g: if(len(g[x])==2): if((g[x][0]==g[x][1]) or abs(g[x][0]-g[x][1])==1 or abs(g[x][0]-g[x][1])==2): print (1) else: print (2) elif (len(g)==1): for x in g: if((g[x][0]==g[x][1]==g[x][2]) or (abs(g[x][0]-g[x][1])==1 and abs(g[x][1]-g[x][2])==1)): print (0) elif(abs(g[x][0]-g[x][1])>2 and abs(g[x][1]-g[x][2])>2): print (2) else: print (1)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.IntStream; /** * * @author Saju * */ public class Main { static int[] dx = { 0, 1, 0, -1 }; static int[] dy = { -1, 0, 1, 0 }; static int[] ddx = { 0, 1, 0, -1, -1, 1, 1, -1 }; static int[] ddy = { -1, 0, 1, 0, -1, -1, 1, 1 }; static int[] kx = { 2, 1, -1, -2, -2, -1, 1, 2 }; static int[] ky = { 1, 2, 2, 1, -1, -2, -2, -1 }; static long MOD = Long.MAX_VALUE; static final int MAX = 50000; static long INF = Long.MAX_VALUE; static double PI = 3.1415926535; private static final double EPSILON = 1e-10; public static void main(String[] args) { InputReader in = new InputReader(System.in); /* */ Tile[] tiles = new Tile[3]; String str = in.next(); int number = Integer.parseInt(String.valueOf(str.charAt(0))); char ch = str.charAt(1); tiles[0] = new Tile(number, ch); str = in.next(); number = Integer.parseInt(String.valueOf(str.charAt(0))); ch = str.charAt(1); tiles[1] = new Tile(number, ch); str = in.next(); number = Integer.parseInt(String.valueOf(str.charAt(0))); ch = str.charAt(1); tiles[2] = new Tile(number, ch); int[] m = new int[10]; int[] s = new int[10]; int[] p = new int[10]; int scount = 0; int mcount = 0; int pcount = 0; for(int i = 0; i < 3; i++){ Tile tile = tiles[i]; switch(tile.type){ case 'm': mcount++; m[tile.number]++; break; case 's': scount++; s[tile.number]++; break; case 'p': pcount++; p[tile.number]++; break; } } int maxIdenticalM = 0; int maxIdenticalP = 0; int maxIdenticalS = 0; for(int i = 1; i <= 9; i++){ maxIdenticalM = Math.max(maxIdenticalM, m[i]); maxIdenticalP = Math.max(maxIdenticalP, p[i]); maxIdenticalS = Math.max(maxIdenticalS, s[i]); } if(maxIdenticalM >= 2 || maxIdenticalP >= 2 || maxIdenticalS >= 2){ if(maxIdenticalM == 3 || maxIdenticalP == 3 || maxIdenticalS == 3){ System.out.println("0"); return; } else{ System.out.println("1"); return; } } if(scount == 3 || pcount == 3 || mcount == 3){ if(scount == 3){ check3(s); } else if(pcount == 3){ check3(p); } else if(mcount == 3){ check3(m); } } else if(scount == 2 || pcount == 2 || mcount == 2){ if(scount == 2){ check2(s); } else if(pcount == 2){ check2(p); } else if(mcount == 2){ check2(m); } } else{ System.out.println("2"); } System.exit(0); } private static void check2(int[] s) { int firstNumber = -1; int secondNumber = -1; for(int i = 1; i <= 9; i++){ if(firstNumber == -1 && s[i] != 0){ firstNumber = i; } else if(secondNumber == -1 && s[i] != 0){ secondNumber = i; } } int diff1 = secondNumber - firstNumber; if(diff1 == 1){ System.out.println("1"); return; } else if(diff1 == 2){ System.out.println("1"); } else{ System.out.println("2"); } } private static void check3(int s[]){ int firstNumber = -1; int secondNumber = -1; int thirdNumber = -1; for(int i = 1; i <= 9; i++){ if(firstNumber == -1 && s[i] != 0){ firstNumber = i; } else if(secondNumber == -1 && s[i] != 0){ secondNumber = i; } else if(thirdNumber == -1 && s[i] != 0){ thirdNumber = i; } } int diff1 = secondNumber - firstNumber; int diff2 = thirdNumber - secondNumber; if(diff1 == 1 && diff2 == 1){ System.out.println("0"); return; } else if(diff1 == 1 || diff2 == 1 || diff1 == 2 || diff2 == 2){ System.out.println("1"); } else{ System.out.println("2"); } } private static class Tile{ int number; char type; Tile(int number, char type){ this.number = number; this.type = type; } } // nCk // private static long binomialCoeff(int n, int k){ // long[] arr = new long[k + 1]; // arr[0] = 1; // for(int i = 1; i <= n; i++){ // for(int j = Math.min(i, k); j > 0; j--){ // arr[j] = (arr[j] + arr[j - 1]) % MOD; // } // } // return arr[k]; // } // BIT => // https://www.hackerearth.com/practice/notes/binary-indexed-tree-or-fenwick-tree/ // static int BIT[] = new int[MAX + 5]; // // private static int query(int index) { // int sum = 0; // for (; index > 0; index -= (index & (-index))) { // sum += BIT[index]; // } // return sum; // } // // private static void update(int size, int index, int val) { // for (; index <= size; index += (index & (-index))) { // BIT[index] += val; // } // } /* * * tutorial : * https://helloacm.com/algorithms-series-0-1-backpack-dynamic-programming- * and-backtracking/ * */ // here arr and prob zero based index // static int backPack(int n, int m, int[] arr, double[] prob, double p) { // double dp[][] = new double[n + 1][m + 1]; // for (int i = 0; i <= n; ++i) { // dp[i][0] = 1.0; // } // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= m; j++) { // // if (j < arr[i - 1]) { // insufficient capacity // dp[i][j] = dp[i - 1][j]; // } else { // dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - arr[i - 1]] * (1.0 - // prob[i - 1])); // } // // } // } // for (int i = m; i >= 0; i--) { // reverse checking the maximum weight // if ((1.0 - dp[n][i]) <= p) { // return i; // } // } // return 0; // } // here arr and prob one based index // static int backPack1(int n, int m, int[] arr, double[] prob, double p) { // // double dp[] = new double[m + 1]; // // dp[0] = 1.0; // for (int i = 1; i <= n; i++) { // for (int j = m; j >= arr[i]; --j) { // // dp[j] = dp[j] || dp[j - A[i - 1]]; // dp[j] = Math.max(dp[j], dp[j - arr[i]] * (1.0 - prob[i])); // } // } // for (int i = m; i >= 0; i--) { // if (1.0 - dp[i] <= p) { // return i; // } // } // return 0; // } /* * * tutorial : * https://www.hackerearth.com/practice/algorithms/searching/ternary-search/ * tutorial/ * */ // lightoj -> 1146 // static double ternary(Point a, Point b, Point c, Point d) { // // double l = 0; // double r = 1; // // for (int i = 0; i < 200; i++) { // double mid1 = l + ((r - l) / 3); // double mid2 = r - ((r - l) / 3); // // double dis1 = func(a, b, c, d, mid1); // double dis2 = func(a, b, c, d, mid2); // // if(dis1 > dis2){ // l = mid1; // // } // else{ // r = mid2; // } // } // // return l; // // } // // static double func(Point a, Point b, Point c, Point d, double multiple) { // // double xDifAB = b.x - a.x; // double yDifAB = b.y - a.y; // double xDifCD = d.x - c.x; // double yDifCD = d.y - c.y; // // double x1 = a.x + xDifAB * multiple; // double y1 = a.y + yDifAB * multiple; // double x2 = c.x + xDifCD * multiple; // double y2 = c.y + yDifCD * multiple; // // double distance = Math.sqrt(((x1 - x2) * (x1 - x2)) + // ((y1 - y2) * (y1 - y2))); // // return distance; // } // // static class Point{ // double x; // double y; // // Point(double x, double y){ // this.x = x; // this.y = y; // } // } // static void primeFactorization(int n) { // int temp = n; // Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // for (int i = 2; i <= Math.sqrt(n); i++) { // if (n % i == 0) { // int count = 0; // while (n % i == 0) { // count++; // n = n / i; // } // System.out.println("i: " + i + ", count: " + count); // map.put(i, count); // } // // } // if (n != 1) { // System.out.println(n); // map.put(n, 1); // } // //// calculateSumOfDivisor(map, temp); // } // static int counter[] = new int[MAX]; // // private static void calculateSumOfDivisor(Map<Integer, Integer> map, int // n) { // int sum = 1; // for(Integer key : map.keySet()){ // int count = map.get(key); // sum *= ((Math.pow(key, count + 1) - 1) / (key - 1)); // } // if(sum < MAX){ // if(counter[sum] < n){ //// System.out.println("H"); // counter[sum] = n; // } // // } // //// System.out.println(sum); // } // static int phi[] = new int[MAX]; // static int phiStepCount[] = new int[MAX]; // static void computeTotient() { // // // Create and initialize an array to store // // phi or totient values // for (int i = 1; i < MAX; i++) { // phi[i] = i; // indicates not evaluated yet // // and initializes for product // // formula. // } // // // Compute other Phi values // for (int p = 2; p < MAX; p++) { // // // If phi[p] is not computed already, // // then number p is prime // if (phi[p] == p) { // // // Phi of a prime number p is // // always equal to p-1. // phi[p] = p - 1; // // // Update phi values of all // // multiples of p // for (int i = 2 * p; i < MAX; i += p) { // // // Add contribution of p to its // // multiple i by multiplying with // // (1 - 1/p) // phi[i] = (phi[i] / p) * (p - 1); // } // // // for (int i = p; i < MAX; i += p) { // // // // phi[i] -= (phi[i] / p); // // } // // } // phiStepCount[p] = phiStepCount[phi[p]] + 1; // } // // for(int i = 1; i < MAX; i++){ // phiStepCount[i] += phiStepCount[i - 1]; // } // } // public static BigInteger floorOfNthRoot(BigInteger x, int n) { // int sign = x.signum(); // if (n <= 0 || (sign < 0)) // throw new IllegalArgumentException(); // if (sign == 0) // return BigInteger.ZERO; // if (n == 1) // return x; // BigInteger a; // BigInteger bigN = BigInteger.valueOf(n); // BigInteger bigNMinusOne = BigInteger.valueOf(n - 1); // BigInteger b = BigInteger.ZERO.setBit(1 + x.bitLength() / n); // do { // a = b; // b = a.multiply(bigNMinusOne).add(x.divide(a.pow(n - 1))).divide(bigN); // } while (b.compareTo(a) == -1); // return a; // } // O(log(max(A, B))). // static long gcd(long a, long b) { // if (b == 0) // return a; // return gcd(b, a % b); // } private static double getDistance(double x1, double y1, double x2, double y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); } // private static int getDecimal(String str) { // int val = 0; // // for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) { // if (str.charAt(i) == '1') { // val += Math.pow(2, j); // } // } // return val; // } // private static int call(int i, int j, int[][] colors) { // // if (i >= colors.length) { // return 0; // } // // if (dp[i][j] != -1) { // return dp[i][j]; // // } // // int result = Integer.MAX_VALUE; // // for (int k = 0; k < 3; k++) { // if (k == j) { // continue; // } else { // result = Math.min(result, call(i + 1, k, colors) + colors[i][j]); // } // } // // return dp[i][j] = result; // } static int dp[][]; // private static BigInteger fac(int n, int mod) { // // if (n <= 0) { // return BigInteger.ONE; // } // return BigInteger.valueOf(n).multiply(fac(n - 1, mod)); // } // private static long modInverse(long n, long m){ // return bigMod(n, m - 2, m); // } private static long bigMod(long n, long k, long m) { long ans = 1; while (k > 0) { if ((k & 1) == 1) { ans = (ans * n) % m; } n = (n * n) % m; k >>= 1; } return ans; } // Returns an iterator pointing to the first element // in the range [first, last] which does not compare less than val. // private static int lowerBoundNew(long[] arr, long num){ // int start = 0; // int end = arr.length - 1; // int index = 0; // int len = arr.length; // int mid = 0; // while(true){ // if(start > end){ // break; // } // mid = (start + end) / 2; // if(arr[mid] > num){ // end = mid - 1; // } // else if(arr[mid] < num){ // start = mid + 1; // } // else{ // while(mid >= 0 && arr[mid] == num){ // mid--; // } // return mid + 1; // } // } // if(arr[mid] < num){ // return mid + 1; // } // return mid; // } // upper_bound() is a standard library function // in C++ defined in the header . // It returns an iterator pointing to // the first element in the range [first, last) // that is greater than value, or last if no such element is found // private static int upperBoundNew(long[] arr, long num){ // // int start = 0; // int end = arr.length - 1; // int index = 0; // int len = arr.length; // int mid = 0; // while(true){ // if(start > end){ // break; // } // mid = (start + end) / 2; // if(arr[mid] > num){ // end = mid - 1; // } // else if(arr[mid] < num){ // start = mid + 1; // } // else{ // while(mid < len && arr[mid] == num){ // mid++; // } // if(mid == len - 1 && arr[mid] == num){ // return mid + 1; // } // else{ // return mid; // } // } // } // if(arr[mid] < num){ // return mid + 1; // } // return mid; // } // private static int upperBound(long[] arr, long num) { // // int start = 0; // int end = arr.length; // // int mid = 0; // int index = 0; // while (true) { // //// System.out.println(start + " " + end); // if (start > end) { // break; // } // mid = (start + end) / 2; // if (arr[mid] > num) { // end = mid - 1; // // } else if (arr[mid] < num) { // start = mid + 1; // } else { // return mid; // } // //// System.out.println("a: " + start + " " + end); // } // // // System.out.println(mid); // if (arr[mid] < num) { // index = mid + 1; // } else { // index = mid; // } // return index; // } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { return null; } return tokenizer.nextToken(); } public String nextLine() { String line = null; try { tokenizer = null; line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class CodeForces { public static void main(String[] args) { Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in))); String s1 = input.next(); String s2 = input.next(); String s3 = input.next(); int[] arrayS = new int[10]; int[] arrayM = new int[10]; int[] arrayP = new int[10]; boolean done = false; boolean oneMore = false; if (s1.charAt(1) == 's') { arrayS[s1.charAt(0) - '0']++; } else if (s1.charAt(1) == 'm') { arrayM[s1.charAt(0) - '0']++; } else { arrayP[s1.charAt(0) - '0']++; } if (s2.charAt(1) == 's') { arrayS[s2.charAt(0) - '0']++; } else if (s2.charAt(1) == 'm') { arrayM[s2.charAt(0) - '0']++; } else { arrayP[s2.charAt(0) - '0']++; } if (s3.charAt(1) == 's') { arrayS[s3.charAt(0) - '0']++; } else if (s3.charAt(1) == 'm') { arrayM[s3.charAt(0) - '0']++; } else { arrayP[s3.charAt(0) - '0']++; } for (int i = 1; i < arrayS.length; i++) { if (arrayS[i] >= 3) { done = true; } } for (int i = 1; i < arrayM.length; i++) { if (arrayM[i] >= 3) { done = true; } } for (int i = 1; i < arrayP.length; i++) { if (arrayP[i] >= 3) { done = true; } } for (int i = 1; i < arrayS.length; i++) { if (arrayS[i] >= 2) { oneMore = true; } } for (int i = 1; i < arrayM.length; i++) { if (arrayM[i] >= 2) { oneMore = true; } } for (int i = 1; i < arrayP.length; i++) { if (arrayP[i] >= 2) { oneMore = true; } } for (int i = 1; i < arrayS.length - 2; i++) { if (arrayS[i] >= 1 && arrayS[i + 1] >= 1 && arrayS[i + 2] >= 1) { done = true; } } for (int i = 1; i < arrayM.length - 2; i++) { if (arrayM[i] >= 1 && arrayM[i + 1] >= 1 && arrayM[i + 2] >= 1) { done = true; } } for (int i = 1; i < arrayP.length - 2; i++) { if (arrayP[i] >= 1 && arrayP[i + 1] >= 1 && arrayP[i + 2] >= 1) { done = true; } } for (int i = 1; i < arrayS.length - 1; i++) { if (arrayS[i] >= 1 && arrayS[i + 1] >= 1) { oneMore = true; } } for (int i = 1; i < arrayM.length - 1; i++) { if (arrayM[i] >= 1 && arrayM[i + 1] >= 1) { oneMore = true; } } for (int i = 1; i < arrayP.length - 1; i++) { if (arrayP[i] >= 1 && arrayP[i + 1] >= 1) { oneMore = true; } } for (int i = 1; i < arrayS.length - 2; i++) { if (arrayS[i] >= 1 && arrayS[i + 2] >= 1) { oneMore = true; } } for (int i = 1; i < arrayM.length - 2; i++) { if (arrayM[i] >= 1 && arrayM[i + 2] >= 1) { oneMore = true; } } for (int i = 1; i < arrayP.length - 2; i++) { if (arrayP[i] >= 1 && arrayP[i + 2] >= 1) { oneMore = true; } } if (done) { System.out.println(0); } else { if (oneMore) { System.out.println(1); } else { System.out.println(2); } } } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
arr=input().split() flag=0 t1=arr[0] t2=arr[1] t3=arr[2] if t1[1]==t2[1] and t1[1]==t3[1]: arr_num=[int(t1[0]),int(t2[0]),int(t3[0])] arr_num.sort() if arr_num[0]==arr_num[1]-1 and arr_num[1]==arr_num[2]-1: print(0) flag=1 if t1==t2 and t1==t3 and flag==0: print(0) flag=1 if t1[1]==t2[1] and (abs(int(t1[0])-int(t2[0]))==1 or t1[0]==t2[0] or abs(int(t1[0])-int(t2[0]))==2 ) and flag==0: print(1) flag=1 if t1[1]==t3[1] and (abs(int(t1[0])-int(t3[0]))==1 or t1[0]==t3[0] or abs(int(t1[0])-int(t3[0]))==2) and flag==0: print(1) flag=1 if t2[1]==t3[1] and (abs(int(t2[0])-int(t3[0]))==1 or t2[0]==t3[0] or abs(int(t2[0])-int(t3[0]))==2) and flag==0: print(1) flag=1 if flag==0: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
suit=list(map(str,input().split())) suit=sorted(suit) number=[] alpha=[] counts=0 countm=0 countp=0 if suit[0]==suit[1] and suit[1]==suit[2]: print('0') exit() for i in range(len(suit)): number.append(int(suit[i][0])) alpha.append(suit[i][1]) if suit[i][1]=='s': counts+=1 if suit[i][1]=='m': countm+=1 if suit[i][1]=='p': countp+=1 if number[1]==number[0]+1 and number[2]==number[1]+1 and alpha[0]==alpha[1] and alpha[1]==alpha[2]: print('0') exit() if max(counts,countm,countp)==3: if number[2]-number[1]>2 and number[1]-number[0]>2: print('2') else: print('1') elif max(counts,countm,countp)==2: if alpha[0]==alpha[1]: if number[1]-number[0]>2: print('2') else: print('1') elif alpha[1]==alpha[2]: if number[2]-number[1]>2: print('2') else: print('1') else: if number[2]-number[0]>2: print('2') else: print('1') else: print('2')
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string s1, s2, s3; cin >> s1 >> s2 >> s3; map<char, vector<long long int> > ma; ma[s1[1]].push_back(s1[0] - '0'); ma[s2[1]].push_back(s2[0] - '0'); ma[s3[1]].push_back(s3[0] - '0'); long long int us = 0; for (map<char, vector<long long int> >::iterator it = ma.begin(); it != ma.end(); it++) { vector<long long int> v = it->second; sort((v).begin(), (v).end()); if (v.size() == 1) { us = max(us, 1LL); } else if (v.size() == 2) { if (v[1] - v[0] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } else if (v.size() == 3) { if (v[0] == v[1] && v[1] == v[2]) { us = 3LL; } else if (v[1] - v[0] == 1 && v[2] - v[1] == 1) { us = 3LL; } else if (v[1] - v[0] <= 2 || v[2] - v[1] <= 2) { us = max(us, 2LL); } else { us = max(us, 1LL); } } } cout << (3LL - us) << '\n'; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c=[x for x in input().split(' ')] n=0 if a==b and b==c: n=0 elif a==b or b==c or c==a: n=1 elif a[1]==b[1] and a[1]==c[1]: z=[int(a[0]),int(b[0]),int(c[0])] z.sort() if a[0]==b[0] or b[0]==c[0] or a[0]==c[0]: n=1 elif z[2]-z[1]==1 and z[1]-z[0]==1: n=0 elif abs(int(a[0])-int(b[0])) in [1,2] or abs(int(a[0])-int(c[0])) in [1,2] or abs(int(b[0])-int(c[0])) in [1,2]: n=1 else: n=2 elif a[1]==b[1] or a[1]==c[1] or b[1]==c[1]: if a[1]==b[1] and abs(int(a[0])-int(b[0])) in [1,2]: n=1 elif a[1]==c[1] and abs(int(a[0])-int(c[0])) in [1,2]: n=1 elif c[1]==b[1] and abs(int(c[0])-int(b[0])) in [1,2]: n=1 else: n=2 else: n=2 print(n)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#import math #n, m = input().split() #n = int (n) #m = int (m) #k = int (k) s1, s2, s3 = input().split() #x = int(input()) #h = list(map(int, input().split())) #b = list(map(int, input().split())) #c = list(map(int, input().split())) #x1, y1, x2, y2 =map(int,input().split()) #n = int(input()) #f = [] #f = [0]*n #t = [0]*n f = [(int(s1[0]),s1[1]), (int(s2[0]),s2[1]), (int(s3[0]), s3[1])] f = sorted (f, key = lambda tup: tup[0] ) #h = [""] * n #f1 = sorted(f, key = lambda tup: tup[0]) #f1 = sorted(t, key = lambda tup: tup[0]) m = 2 if (s1 == s2 and s2 == s3): m = 0 elif (f[0][0] == f[1][0]-1 and f[1][0] == f[2][0]-1 and s1[1] == s2[1] and s2[1] == s3[1]): m = 0 elif ((f[0][1] == f[1][1] and f[0][0] == f[1][0]-1) or (f[0][1] == f[2][1] and f[0][0] == f[2][0]-1) or (f[0][1] == f[1][1] and f[0][0] == f[1][0]-2) or (f[1][1] == f[2][1] and f[1][0] == f[2][0]-2) or (f[0][1] == f[2][1] and f[0][0] == f[2][0]-2) or (f[1][1] == f[2][1] and f[1][0] == f[2][0]-1) or s1 == s2 or s2 == s3 or s1 == s3): m = 1 print(m)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int possibleKoutsu(string str1, string str2) { if (str1[0] == str2[0] && str1[1] == str2[1]) return 1; return 0; } int possibleShuntsu(string str1, string str2) { int diff = (int)str1[0] - (int)str2[0]; if (str1[1] == str2[1] && (abs(diff) == 1 || abs(diff) == 2)) return 1; return 0; } int mentsu(string str1, string str2, string str3) { int koutsu = possibleKoutsu(str1, str2) && possibleKoutsu(str2, str3); int shuntsu = possibleShuntsu(str1, str2) && possibleShuntsu(str1, str3) && possibleShuntsu(str2, str3); return (koutsu || shuntsu); } int main() { int tst = 0, i = 0, tilesToDraw = 0; string str1, str2, str3; tst = 1; while (tst > 0) { cin >> str1 >> str2 >> str3; if (mentsu(str1, str2, str3)) tilesToDraw = 0; else if (possibleKoutsu(str1, str2) || possibleKoutsu(str1, str3) || possibleKoutsu(str2, str3) || possibleShuntsu(str1, str2) || possibleShuntsu(str1, str3) || possibleShuntsu(str3, str2)) { tilesToDraw = 1; } else { tilesToDraw = 2; } tst--; } cout << tilesToDraw; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from collections import Counter import sys readline = sys.stdin.readline def calc(t): ct = Counter(t) if not t: return 3 shun = max(sum(ct[c+i] > 0 for i in range(-1, 2)) for c in range(2, 9)) ko = max(min(3, ct[i]) for i in range(1, 10)) res = max(ko, shun) if res == 3: return 0 if res == 2: return 1 if res == 1: return 2 assert True, '' Hand = list(readline().strip().split()) Manzu = [int(h[0]) for h in Hand if h[1] == 'm'] Pinzu = [int(h[0]) for h in Hand if h[1] == 'p'] Souzu = [int(h[0]) for h in Hand if h[1] == 's'] print(min(calc(t) for t in [Manzu, Pinzu, Souzu]))
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; bitset<65000000> f; long long m[200995]; vector<long long> z; map<long long, long long> u; map<long long, long long> u1; map<string, int> v; string w[200008]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long t, c, d, e, i, j[4000], k, n; string a, b, x; cin >> a >> b >> x; j[a[1] - '0']++; j[b[1] - '0']++; j[x[1] - '0']++; c = a[0] + b[0] + (x[0] - '0') - '0' - '0'; if (j['s' - '0'] == 3 || j['m' - '0'] == 3 || j['p' - '0'] == 3) { k = max(max((a[0] - '0'), (b[0] - '0')), (x[0] - '0')); n = min(min((a[0] - '0'), (b[0] - '0')), (x[0] - '0')); c = c - (k + n); if ((k == n && n == c) || (k == c + 1 && k == n + 2)) cout << "0" << endl; else if (k - n < 3 || c - n < 3 || k - c < 3) cout << "1" << endl; else cout << "2" << endl; } else if (j['s' - '0'] == 2 || j['m' - '0'] == 2 || j['p' - '0'] == 2) { if (a[1] == b[1] && abs(a[0] - b[0]) < 3) cout << "1" << endl; else if (a[1] == x[1] && abs(a[0] - x[0]) < 3) cout << "1" << endl; else if (x[1] == b[1] && abs(x[0] - b[0]) < 3) cout << "1" << endl; else cout << "2" << endl; } else cout << "2" << endl; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
# -*- coding: utf-8 -*- # @Date : 2019-08-01 07:30:35 # @Author : raj lath ([email protected]) # @Link : link # @Version : 1.0.0 import sys sys.setrecursionlimit(10**5+1) inf = int(10 ** 20) max_val = inf min_val = -inf RW = lambda : sys.stdin.readline().strip() RI = lambda : int(RW()) RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()] RWI = lambda : [x for x in sys.stdin.readline().strip().split()] options = {x:[0] * 9 for x in "smp"} for x in input().split(): options[x[1]][int(x[0]) - 1] += 1 ans = 2 for c in "smp": curr = options[c] if max(curr) >= 2: ans = min(ans, 3 - max(curr)) else: for i in range(7): sums = sum(curr[i:i+3]) ans = min(ans, 3 - sums) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.util.*; public class substring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s=sc.next(),s2=sc.next(),s3=sc.next(); char c=s.charAt(1),c2=s2.charAt(1),c3=s3.charAt(1); int a=Integer.parseInt(s.charAt(0)+""),a2=Integer.parseInt(s2.charAt(0)+""),a3=Integer.parseInt(s3.charAt(0)+""); PriorityQueue<Integer> pq=new PriorityQueue<>(); pq.add(a);pq.add(a2);pq.add(a3); boolean sequence3=false,sequencecc2=false,sequencec2c3=false,sequencecc3=false,ssequencecc2=false,ssequencecc3=false,ssequencec2c3=false; if(a+1==a2||a2+1==a)sequencecc2=true; if(a+2==a2||a2+2==a)ssequencecc2=true; if(a2+1==a3||a3+1==a2)sequencec2c3=true; if(a2+2==a3||a3+2==a2)ssequencec2c3=true; if(a+1==a3||a3+1==a)sequencecc3=true; if(a+2==a3||a3+2==a)ssequencecc3=true; a=pq.poll();a2=pq.poll();a3=pq.poll(); if(a+1==a2&&a2+1==a3)sequence3=true; if(s.equals(s2)&&s.equals(s3))System.out.println(0); else if(c==c2&&c3==c&&sequence3)System.out.println(0); else if(s.equals(s2)||s2.equals(s3)||s3.equals(s))System.out.println(1); else if((c==c2&&sequencecc2)||(c==c3&&sequencecc3)||(c2==c3&&sequencec2c3))System.out.println(1); else if((c==c2&&ssequencecc2)||(c==c3&&ssequencecc3)||(c2==c3)&&ssequencec2c3)System.out.println(1); else System.out.println(2); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.*; import java.util.*; public class Main implements Runnable { String[] arr=new String[27]; int pos(String cad){ for (int i = 0; i < arr.length; i++) { if(cad.equals(arr[i])) return i; } return -1; } boolean consecutivo(int Pa,int Pb){ if (Pa==Pb) return false; return Pb-Pa==1 && arr[Pa].charAt(1)== arr[Pb].charAt(1); } boolean consecutivo1(int Pa,int Pb){ if (Pa==Pb) return false; return Pb-Pa==2 && arr[Pa].charAt(1)== arr[Pb].charAt(1); } boolean iguales(int Pa,int Pb){ return Math.abs(Pa-Pb)==0; } private void solve() throws IOException { char[] mps={'m','p','s'}; int k=0; { for (int j = 0; j < mps.length; j++) for (int i = 1; i <= 9; i++) { arr[k]=i+""+mps[j]; k++; } } /* for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]+" "+ i); }*/ String a,b,c; a=nextToken(); b=nextToken(); c=nextToken(); int posA,posB,posC; posA=pos(a); posB=pos(b); posC=pos(c); /* System.out.println("dddddddddddd"); System.out.println(a+" " +posA); System.out.println(b+" " +posB); System.out.println(c+" " +posC);*/ if((posA==posB)&&(posA==posC)&&(posB==posC)) System.out.println("0"); else if(consecutivo(posA, posB)&&consecutivo(posB, posC)) System.out.println("0"); else if(consecutivo(posA, posC)&&consecutivo(posC, posB)) System.out.println("0"); else if(consecutivo(posB, posC)&&consecutivo(posC, posA)) System.out.println("0"); else if(consecutivo(posB, posA)&&consecutivo(posA, posC)) System.out.println("0"); else if(consecutivo(posC, posA)&&consecutivo(posA, posB)) System.out.println("0"); else if(consecutivo(posC, posB)&&consecutivo(posB, posA)) System.out.println("0"); /* else if(posA==posB-1&&posA==posC-2) System.out.println("0"); else if(posA==posB-2&&posA==posC-1) System.out.println("0"); else if(posA==posB+1&&posA==posC-1) System.out.println("0"); else if(posA==posB-1&&posA==posC+1) System.out.println("0"); else if(posA==posB+2&&posA==posC-1) System.out.println("0"); else if(posA==posB+1&&posA==posC+2) System.out.println("0");*/ else if(iguales(posA, posB)&&!iguales(posA, posC)&&!iguales(posB, posC)) System.out.println("1"); else if(iguales(posA, posC)&&!iguales(posA, posB)&&!iguales(posB, posC)) System.out.println("1"); else if(iguales(posC, posB)&&!iguales(posA, posC)&&!iguales(posB, posA)) System.out.println("1"); else if(consecutivo(posA, posB)||consecutivo1(posA, posB)) System.out.println("1"); else if(consecutivo(posB, posA)||consecutivo1(posB, posA)) System.out.println("1"); else if(consecutivo(posA, posC)||consecutivo1(posA, posC)) System.out.println("1"); else if(consecutivo(posC, posA)||consecutivo1(posC, posA)) System.out.println("1"); else if(consecutivo(posB, posC)||consecutivo1(posB, posC)) System.out.println("1"); else if(consecutivo(posC, posB)||consecutivo1(posC, posB)) System.out.println("1"); else // if(!consecutivo(posA, posB)&&!consecutivo(posB, posC)&&!consecutivo(posA, posC)) // System.out.println("2"); // else if(!iguales(posA, posB)&&!iguales(posB, posC)&&!iguales(posA, posC)) System.out.println("2"); } public static void main(String[] args) { new Main().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
s=raw_input() a=int(s[0]) b=int(s[3]) c=int(s[6]) a1=s[1] b1=s[4] c1=s[7] a2=b2=c2=0 if ((a1=='m' and b1=='m' and c1=='m') or (a1=='s' and b1=='s' and c1=='s') or (a1=='p' and b1=='p' and c1=='p')) and ((a+1==b and b+1==c) or (a==b and b==c) or (c+1==b and b+1==a) or (c+1==a and a+1==b) or (b+1==c and c+1==a) or (b+1==a and a+1==c) or (a+1==c and c+1==b)): print 0 else: if a1=='m': a2+=1 if b1=='m': a2+=1 if c1=='m': a2+=1 if a1=='s': b2+=1 if b1=='s': b2+=1 if c1=='s': b2+=1 if a1=='p': c2+=1 if b1=='p': c2+=1 if c1=='p': c2+=1 if a1==1 and b1==1 and c1==1: print 2 else: if (a1==b1 and a+1==b) or (b1==c1 and b+1==c) or (c1==a1 and c+1==a) or (a1==b1 and b+1==a) or (b1==c1 and c+1==b) or (c1==a1 and a+1==c): print 1 elif (a1==b1 and a==b) or (a1==c1 and a==c) or (b1==c1 and b==c) or (c1==a1 and a==c): print 1 elif (a1==b1 and a+2==b) or (b1==c1 and b+2==c) or (c1==a1 and c+2==a) or (a1==b1 and b+2==a) or (b1==c1 and c+2==b) or (c1==a1 and a+2==c): print 1 else: print 2
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string tiles[4]; cin >> tiles[0] >> tiles[1] >> tiles[2]; sort(tiles, tiles + 3); map<string, int> k; map<char, vector<int> > s; for (int i = 0; i < 3; ++i) k[tiles[i]]++, s[tiles[i][1]].push_back(tiles[i][0] - '0'); int mx = -1; for (auto tl : k) mx = max(mx, tl.second); int mn = 10, cards; for (auto tl : s) { if (tl.second.size() == 1) cards = 2; if (tl.second.size() == 2) { int dif = tl.second[1] - tl.second[0]; if (dif == 1 || dif == 2) cards = 1; else cards = 2; } if (tl.second.size() == 3) { int dif1 = tl.second[1] - tl.second[0]; int dif2 = tl.second[2] - tl.second[1]; if (dif1 == 1 && dif2 == 1) cards = 0; else if ((dif1 != 1 && dif2 == 1) || (dif1 == 1 && dif2 != 1)) cards = 1; else if ((dif1 != 1 && dif2 == 2) || (dif1 == 2 && dif2 != 1)) cards = 1; else cards = 2; } mn = min(mn, cards); } cout << min(3 - mx, mn) << "\n"; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c=raw_input().split() if a==b and b==c: print 0 # all equal elif a==b or b==c or a==c: print 1 # any two equal elif a[1]==b[1]: if b[1]==c[1]: # all same cat if abs(int(a[0])-int(b[0]))<3 and abs(int(c[0])-int(b[0]))<3 and abs(int(a[0])-int(c[0]))<3: # if 2*int(b[0])==int(a[0])+int(c[0]) and 2*int(c[0])==int(a[0])+int(b[0]) or 2*int(a[0])==int(c[0])+int(b[0]): # same cat and AP # print 0 # else: print 0 elif abs(int(a[0])-int(b[0]))<3 or abs(int(c[0])-int(b[0]))<3 or abs(int(a[0])-int(c[0]))<3: print 1 else: print 2 elif abs(int(a[0])-int(b[0]))<3: # only a and b same cat print 1 else: print 2 elif c[1]==b[1]: # b and c same cat if abs(int(c[0])-int(b[0]))<3: # b and c consec print 1 else: print 2 # b and c not consec elif a[1]==c[1]: if abs(int(a[0])-int(c[0]))<3: print 1 else: print 2 else: print 2
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
z=sorted([int(i[0])+ord(i[1])*9 for i in map(str,input().split())]) if len(set(z))==1 or (z[0]==z[1]-1 and z[1]==z[2]-1):print(0) elif (z[0]==z[1] or z[1]==z[2]) or (z[1]-z[0] in [1,2] or z[2]-z[1] in [1,2]):print(1) else:print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
tt = input().split() tt = sorted(tt) def koutsu(): one_and_two_equal = tt[0] == tt[1] two_and_three_equal = tt[1] == tt[2] one_and_three_equal = tt[0] == tt[2] all_equal = one_and_two_equal and two_and_three_equal two_equal = one_and_two_equal or two_and_three_equal or one_and_three_equal if all_equal: return 0 if two_equal: return 1 return 2 def part_shuntsu(a, b): return int(tt[b][0]) - int(tt[a][0]) == 1 and tt[b][1] == tt[a][1] def possible_shuntsu(a, b): return int(tt[b][0]) - int(tt[a][0]) == 2 and tt[b][1] == tt[a][1] def shuntsu(): one_and_two_seq = part_shuntsu(0, 1) two_and_three_seq = part_shuntsu(1, 2) one_and_three_seq = part_shuntsu(0, 2) one_and_two_possible_seq = possible_shuntsu(0, 1) one_and_three_possible_seq = possible_shuntsu(0, 2) two_and_three_possible_seq = possible_shuntsu(1, 2) full_seq = one_and_two_seq and two_and_three_seq possible_seq = one_and_two_possible_seq or one_and_three_possible_seq or two_and_three_possible_seq part_seq = one_and_two_seq or two_and_three_seq or one_and_three_seq or possible_seq if full_seq: return 0 if part_seq: return 1 return 2 print(min(shuntsu(), koutsu()))
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
arr = [i for i in input().split()] mem = sorted([[int(i[0]),i[1]] for i in arr]) if len(set(arr)) == 1 or (arr[0][1] == arr[1][1] == arr[2][1] and mem[0][0] == mem[1][0] - 1 == mem[2][0] - 2): print(0) else: if 1 <= mem[1][0] - mem[0][0] <= 2 and mem[1][1] == mem[0][1] or 1 <= mem[2][0] - mem[1][0] <= 2 and mem[2][1] == mem[1][1] or 1 <= mem[2][0] - mem[0][0] <= 2 and mem[2][1] == mem[0][1]: print(1) elif len(set(arr)) == 2: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
d=list(map(str,input().split())) s=[0]*9;m=[0]*9;p=[0]*9 def kut(l): t=max(l) if t>=3: return 0 return (3-t) def sut(l): for i in range(len(l)): if l[i]>1: l[i]=1 s=''.join([str(i) for i in l]) if '111' in s: return 0 elif '11' in s or '101'in s: return 1 elif '1' in s: return 2 else: return 3 for i in d: if i[1]=='s': s[int(i[0])-1]+=1 elif i[1]=='m': m[int(i[0])-1]+=1 else: p[int(i[0])-1]+=1 l=[0]*6 l[0]=kut(s) l[1]=kut(m) l[2]=kut(p) l[3]=sut(s) l[4]=sut(p) l[5]=sut(m) t=min(l) print(t)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { string s[3]; for (int i = 0; i < 3; i++) { cin >> s[i]; } if (s[0][0] > s[1][0]) swap(s[0], s[1]); if (s[1][0] > s[2][0]) swap(s[1], s[2]); if (s[0][0] > s[1][0]) swap(s[0], s[1]); int c1 = 1, c2 = 1; for (int i = 0; i < 2; i++) { if (s[i] == s[i + 1]) c1++; } for (int i = 0; i < 2; i++) { if (s[i + 1][0] - s[i][0] == 1 && s[i][1] == s[i + 1][1]) c2++; } if (c1 == 1) { if (s[0] == s[2]) c1++; } if (c1 == 1 && c2 == 1) { if (s[2][0] - s[0][0] == 1 && s[0][1] == s[2][1]) c2++; else if (s[1][0] - s[0][0] == 2 && s[0][1] == s[1][1]) c2++; else if (s[2][0] - s[0][0] == 2 && s[0][1] == s[2][1]) c2++; else if (s[2][0] - s[1][0] == 2 && s[1][1] == s[2][1]) c2++; } cout << 3 - max(c1, c2); return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}; string alpha = "abcdefghijklmnopqrstuvwxyz"; signed main() { string s[3]; ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> s[0] >> s[1] >> s[2]; swap(s[0][0], s[0][1]); swap(s[1][0], s[1][1]); swap(s[2][0], s[2][1]); sort(s, s + 3); swap(s[0][0], s[0][1]); swap(s[1][0], s[1][1]); swap(s[2][0], s[2][1]); int IDENTICAL = 0, MAXIDENTICAL = 0, folo = 0, MAXFOLOING = 0, special = 3; for (int i = 1; i < 3; i++) { if (s[i] == s[i - 1]) { IDENTICAL++; MAXIDENTICAL = max(MAXIDENTICAL, IDENTICAL); } else { IDENTICAL = 0; } if (s[i][1] == s[i - 1][1] and char(int(s[i - 1][0]) + 1) == s[i][0]) { folo++; MAXFOLOING = max(MAXFOLOING, folo); } else { folo = 0; } if (s[i][1] == s[i - 1][1] && char(int(s[i - 1][0]) + 2) == s[i][0]) { special = 1; } } MAXIDENTICAL = 2 - MAXIDENTICAL; MAXFOLOING = 2 - MAXFOLOING; int ans = min(MAXIDENTICAL, MAXFOLOING); ans = min(ans, special); cout << ans; cout << "\n"; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class Main { static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pr = new PrintWriter(new BufferedOutputStream(System.out)); static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws NumberFormatException, IOException { String[] s = new String[3]; s[0] = sc.next(); s[1] = sc.next(); s[2] = sc.next(); Arrays.sort(s); if(s[0].equals(s[1])&&s[0].equals(s[2])) { System.out.println(0); }else if(s[0].charAt(1)==s[1].charAt(1)&&s[0].charAt(1)==s[2].charAt(1)){ if(s[0].charAt(0)==s[1].charAt(0)-1&&s[0].charAt(0)==s[2].charAt(0)-2) { System.out.println(0); }else if(s[1].charAt(0)-s[0].charAt(0)<=2||s[2].charAt(0)-s[1].charAt(0)<=2){ System.out.println(1); }else if(s[0].charAt(0)==s[1].charAt(0)||s[1].charAt(0)==s[2].charAt(0)) { System.out.println(1); }else { System.out.println(2); } }else if(s[0].charAt(1)==s[1].charAt(1)){ if(s[1].charAt(0)-s[0].charAt(0)<=2){ System.out.println(1); }else if(s[0].charAt(0)==s[1].charAt(0)) { System.out.println(1); }else { System.out.println(2); } }else if(s[0].charAt(1)==s[2].charAt(1)){ if(s[2].charAt(0)-s[0].charAt(0)<=2){ System.out.println(1); }else if(s[0].charAt(0)==s[2].charAt(0)) { System.out.println(1); }else { System.out.println(2); } }else if(s[1].charAt(1)==s[2].charAt(1)){ if(s[2].charAt(0)-s[1].charAt(0)<=2){ System.out.println(1); }else if(s[1].charAt(0)==s[2].charAt(0)) { System.out.println(1); }else { System.out.println(2); } }else { System.out.println(2); } } private static int nextInt() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (int) st.nval; } } class node implements Comparable { int begin; int sy; public node() { } public node(int begin, int sy) { super(); this.begin = begin; this.sy = sy; } @Override public int compareTo(Object o) { return begin - ((node) o).begin; } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c = tuple([x for x in input().split()]) m,p,s=[],[],[] if a[1]=='m': m.append(int(a[0])) elif a[1]=='p': p.append(int(a[0])) elif a[1]=='s': s.append(int(a[0])) if b[1]=='m': m.append(int(b[0])) elif b[1]=='p': p.append(int(b[0])) elif b[1]=='s': s.append(int(b[0])) if c[1]=='m': m.append(int(c[0])) elif c[1]=='p': p.append(int(c[0])) elif c[1]=='s': s.append(int(c[0])) m.sort() p.sort() s.sort() ans=0 if len(m)==1 and len(p)==1 and len(s)==1: ans=2 elif len(m)==2: if (m[0]-m[1]) in [-2,-1,0]: ans=1 else: ans=2 elif len(p)==2: if (p[0]-p[1]) in [-2,-1,0]: ans=1 else: ans=2 elif len(s)==2: if (s[0]-s[1]) in [-2,-1,0]: ans=1 else: ans=2 elif len(m)==3: if ((m[0]-m[1])==-1 and m[1]-m[2]==-1) or (m[0]-m[1]==0 and m[1]-m[2]==0): ans=0 elif (m[0]-m[1]==0 or m[1]-m[2]==0) or (m[0]-m[1]==-1 or m[1]-m[2]==-1) or (m[0]-m[1]==-2 or m[1]-m[2]==-2): ans=1 else: ans=2 elif len(p)==3: if ((p[0]-p[1])==-1 and p[1]-p[2]==-1) or (p[0]-p[1]==0 and p[1]-p[2]==0): ans=0 elif (p[0]-p[1]==0 or p[1]-p[2]==0) or (p[0]-p[1]==-1 or p[1]-p[2]==-1) or (p[0]-p[1]==-2 or p[1]-p[2]==-2): ans=1 else: ans=2 elif len(s)==3: if ((s[0]-s[1])==-1 and s[1]-s[2]==-1) or (s[0]-s[1]==0 and s[1]-s[2]==0): ans=0 elif (s[0]-s[1]==0 or s[1]-s[2]==0) or (s[0]-s[1]==-1 or s[1]-s[2]==-1) or (s[0]-s[1]==-2 or s[1]-s[2]==-2): ans=1 else: ans=2 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7, siz = 1e6 + 5; long long t, n, m, k, a[siz]; vector<long long> v[4]; string s1, s2, s3; void chk(string s) { if (s[1] == 'm') v[1].push_back(s[0] - '0'); else if (s[1] == 'p') v[2].push_back(s[0] - '0'); else v[3].push_back(s[0] - '0'); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s1 >> s2 >> s3; chk(s1); chk(s2); chk(s3); sort(v[1].begin(), v[1].end()); sort(v[2].begin(), v[2].end()); sort(v[3].begin(), v[3].end()); if (v[1].size() and v[2].size() and v[3].size()) return cout << 2 << endl, 0; for (long long i = 1; i <= 3; i++) { if (v[i].size() == 2) { if ((v[i][0] == v[i][1]) or (v[i][1] == v[i][0] + 1) or (v[i][1] - v[i][0] == 2)) return cout << 1 << endl, 0; } } for (long long i = 1; i <= 3; i++) { if (v[i].size() == 3) { if (v[i][0] == v[i][2]) return cout << 0 << endl, 0; if ((v[i][0] == v[i][1] - 1) and (v[i][1] == v[i][2] - 1)) return cout << 0 << endl, 0; if ((v[i][0] == v[i][1] - 1) or (v[i][1] == v[i][2] - 1)) return cout << 1 << endl, 0; if ((v[i][0] == v[i][1] and v[i][1] != v[i][2]) or (v[i][0] != v[i][1] and v[i][1] == v[i][2])) return cout << 1 << endl, 0; if (v[i][1] - v[i][0] == 2 or v[i][2] - v[i][1] == 2) return cout << 1 << endl, 0; } } cout << 2 << endl; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; char s[100]; pair<int, int> arr[10]; int ttt(char a) { if (a == 's') return 1; if (a == 'p') return 2; return 3; } int main() { gets(s); int pos = 0; for (int i = 1; i <= 3; i++) { int x = s[pos] - '0'; pos++; int y = ttt(s[pos]); pos += 2; arr[i] = pair<int, int>(y, x); } sort(arr + 1, arr + 1 + 3); if (arr[1].first == arr[2].first && arr[2].first == arr[3].first) { if (arr[1].second == arr[2].second && arr[2].second == arr[3].second) { cout << "0" << endl; return 0; } if (arr[1].second + 1 == arr[2].second && arr[2].second + 1 == arr[3].second) { cout << "0" << endl; return 0; } } if (arr[1].first == arr[2].first) { if (arr[1].second == arr[2].second) { cout << "1" << endl; return 0; } if (arr[1].second == arr[2].second - 1) { cout << "1" << endl; return 0; } if (arr[1].second == arr[2].second - 2) { cout << "1" << endl; return 0; } } if (arr[2].first == arr[3].first) { if (arr[3].second == arr[2].second) { cout << "1" << endl; return 0; } if (arr[3].second == arr[2].second + 1) { cout << "1" << endl; return 0; } if (arr[3].second == arr[2].second + 2) { cout << "1" << endl; return 0; } } cout << "2" << endl; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
def similar_number(arr): m = 1 for i in range(1, 10): i_cnt = arr.count(i) m = max(i_cnt, m) return m def subseq_num(arr): if len(arr) == 0 or len(arr) == 1: return len(arr) arr.sort() if len(arr) == 2: if arr[1] == arr[0] + 1 or arr[1] == arr[0] + 2: return 2 else: return 1 if arr[2] == arr[1] + 1 and arr[1] == arr[0] + 1: return 3 elif arr[2] == arr[1] + 1 or arr[1] == arr[0] + 1 or arr[2] == arr[1] + 2 or arr[1] == arr[0] + 2: return 2 else: return 1 def solve(): cards = input().split() s_cards = list(filter(lambda c: c[1] == 's', cards)) m_cards = list(filter(lambda c: c[1] == 'm', cards)) p_cards = list(filter(lambda c: c[1] == 'p', cards)) s_numbers = list(map(lambda c: int(c[0]), s_cards)) m_numbers = list(map(lambda c: int(c[0]), m_cards)) p_numbers = list(map(lambda c: int(c[0]), p_cards)) s_n = similar_number(s_numbers) m_n = similar_number(m_numbers) p_n = similar_number(p_numbers) s_s = subseq_num(s_numbers) m_s = subseq_num(m_numbers) p_s = subseq_num(p_numbers) _n = max(s_n, m_n, p_n, s_s, m_s, p_s) print(3 - _n) solve()
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a,b,c=map(str,input().split()) l=[int(a[0]),int(b[0]),int(c[0])] l.sort() if(a==b and b==c): print(0) elif(a==b or a==c or b==c): print(1) else: if(a[1]==b[1] and b[1]==c[1]): if (l[2]==l[1]+1 and l[1]==l[0]+1 ): print(0) else: if(l[1]==l[0]+1 or l[2]==l[1]+1 or l[2]==l[0]+2 or l[1]==l[0]+2 or l[2]==l[1]+2): print(1) else: print(2) else: if(a[1]==b[1] or b[1]==c[1] or a[1]==c[1]): if(a[1]==b[1]): if (int(abs(int(a[0])-int(b[0])))<=2): print(1) else: print(2) if(a[1]==c[1]): if (int(abs(int(a[0])-int(c[0]))))<=2: print(1) else: print(2) if(b[1]==c[1]): if(int(abs(int(b[0])-int(c[0]))))<=2: print(1) else: print(2) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; vector<string> v(3); bool f0() { if (v[0] == v[1] && v[0] == v[2]) return true; if (v[0][1] == v[1][1] && v[0][1] == v[2][1] && v[1][0] == v[0][0] + 1 && v[2][0] == v[1][0] + 1) return true; return false; } bool f1() { for (int i = 0; i < 3; ++i) for (int j = i + 1; j < 3; ++j) if (v[i][1] == v[j][1] && abs(v[i][0] - v[j][0]) < 3) return true; return false; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); cin >> v[0] >> v[1] >> v[2]; sort(v.begin(), v.end()); if (f0()) cout << 0; else if (f1()) cout << 1; else cout << 2; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { int i; vector<int> v; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1[1] == s2[1] && s1[1] == s3[1]) { v.push_back(s1[0]); v.push_back(s2[0]); v.push_back(s3[0]); sort(v.begin(), v.end()); if ((v[0] + 1) == v[1] && (v[1] + 1 == v[2])) cout << "0"; else if (v[0] == v[1] && v[0] == v[2]) cout << "0"; else if (v[0] == v[1] || v[1] == v[2]) cout << "1"; else if (v[0] + 1 == v[1] || v[1] + 1 == v[2] || v[0] + 2 == v[1] || v[1] + 2 == v[2]) cout << "1"; else cout << "2"; } else if (s1 == s2 || s2 == s3 || s3 == s1) cout << "1"; else if (s1[1] == s2[1]) { if (abs(s1[0] - s2[0]) == 1 || abs(s1[0] - s2[0]) == 2) cout << "1"; else cout << "2"; } else if (s3[1] == s2[1]) { if (abs(s3[0] - s2[0]) == 1 || abs(s3[0] - s2[0]) == 2) cout << "1"; else cout << "2"; } else if (s1[1] == s3[1]) { if (abs(s1[0] - s3[0]) == 1 || abs(s1[0] - s3[0]) == 2) cout << "1"; else cout << "2"; } else cout << "2"; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import sys import math import bisect import atexit import io import heapq from collections import defaultdict, Counter MOD = int(1e9+7) # n = map(int, raw_input().split()) # input = map(int, raw_input().split()) def main(): a, b, c = raw_input().split() d = defaultdict(list) num = [] ans = 2 for st in (a,b,c): nu, ch = st nu = int(nu) d[ch].append(int(nu)) for k, v in d.items(): v.sort() if len(v) == 3 and v[0] == v[-1]: ans = 0 if len(v) == 3 and v[0] == v[1]-1 and v[1] == v[2]-1: ans = 0 for idx in range(len(v)): for jdx in range(idx+1, len(v)): if v[idx] == v[jdx] or v[idx] == v[jdx]-1 or v[idx] == v[jdx]-2: ans = min(1, ans) print ans main()
PYTHON
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
s=input() a=s[0:2] b=s[3:5] c=s[6]+s[7] l=[int(a[0]),int(b[0]),int(c[0])] l.sort() if(a==b and a==c): print(0) elif(l[1]==l[0]+1 and l[2]==l[0]+2 and a[1]==b[1] and a[1]==c[1]): print(0) elif((a==b and a!=c) or (a==c and a!=b) or (b==c and b!=a)): print(1) elif(l[1]==l[0]+1 or l[2]==l[1]+1): if(abs(int(a[0])-int(b[0]))==1 and a[1]==b[1]): print(1) elif(abs(int(b[0])-int(c[0]))==1 and b[1]==c[1]): print(1) elif(abs(int(a[0])-int(c[0]))==1 and a[1]==c[1]): print(1) elif(l[2]==l[0]+2 or l[1]==l[0]+2 or l[2]==l[1]+2): if(abs(int(a[0])-int(b[0]))==2 and a[1]==b[1]): print(1) elif(abs(int(b[0])-int(c[0]))==2 and b[1]==c[1]): print(1) elif(abs(int(a[0])-int(c[0]))==2 and a[1]==c[1]): print(1) else: print(2) else: print(2) elif(l[2]==l[0]+2 or l[1]==l[0]+2 or l[2]==l[1]+2): if(abs(int(a[0])-int(b[0]))==2 and a[1]==b[1]): print(1) elif(abs(int(b[0])-int(c[0]))==2 and b[1]==c[1]): print(1) elif(abs(int(a[0])-int(c[0]))==2 and a[1]==c[1]): print(1) else: print(2) elif(l[2]==l[1]+1 and((a[1]==b[1] and a!=c) or (b[1]==c[1] and b!=a) or (a[1]==c[1] and a!=b))): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
from collections import Counter cards = input().split() suits = ['m', 'p', 's'] def hasKoutsu(cards): return max([y for x, y in Counter(cards).items()]) >= 3 def hasShuntsu(cards): cnt = Counter(cards) for suit in suits: for i in range(3, 10): a = [str(j) + suit for j in range(i-2,i+1)] if sum([1 if cnt.get(x, 0) > 0 else 0 for x in a]) >= 3: return True return False def hasMentsu(cards): return hasKoutsu(cards) or hasShuntsu(cards) allCards = [None] for i in range(1, 10): for suit in suits: allCards.append(str(i) + suit) result = 3 for x in allCards: for y in allCards: newCards = list(cards) if x: newCards.append(x) if y: newCards.append(y) if hasMentsu(newCards): result = min(result, len(newCards) - 3) print(result)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
x, y, z = map(str, input().split()) s = set() s.add(x) s.add(y) s.add(z) f = 3 if len(s)==1: print(0) else: if len(s)==2: print(1) else: z = 0 for t in s: d = s.copy() if int(t[0])<=7: d.add(f"{int(t[0])+1}{t[1]}") d.add(f"{int(t[0])+2}{t[1]}") else: d.add("dfdsf") d.add("dfdf") a = s.copy() if 1<=int(t[0])<=8: a.add(f"{int(t[0])-1}{t[1]}") a.add(f"{int(t[0])+1}{t[1]}") else: a.add("dfdsf") a.add("dfdf") b = s.copy() if int(t[0])>=2: b.add(f"{int(t[0])-1}{t[1]}") b.add(f"{int(t[0])-2}{t[1]}") else: b.add("dfdsf") b.add("dfdf") #print(d, b, a) if z: f = min(f, len(d), len(a), len(b)) else: f = min(len(d), len(a), len(b)) z+=1 if f-3<0: print(0) else: print(f-3)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
cards = input().split() ans = 2 if cards[0] == cards[1] and cards[2] == cards[1]: ans = 0 elif cards[0] == cards[1] or cards[0] == cards[2] or cards[2] == cards[1]: ans = 1 else: if cards[0][1] == cards[1][1] and cards[0][1] == cards[2][1]: if abs(int(cards[0][0]) - int(cards[1][0])) <= 2 and abs(int(cards[0][0]) - int(cards[2][0])) <= 2 and abs(int(cards[1][0]) - int(cards[2][0])) <= 2: ans = 0 elif abs(int(cards[0][0]) - int(cards[1][0])) <= 2 or abs(int(cards[0][0]) - int(cards[2][0])) <= 2 or abs(int(cards[1][0]) - int(cards[2][0])) <= 2: ans = 1 elif not(cards[0][1] != cards[1][1] and cards[0][1] != cards[2][1] and cards[2][1] != cards[1][1]): if cards[0][1] == cards[1][1]: card1, card2 = cards[0], cards[1] elif cards[0][1] == cards[2][1]: card1, card2 = cards[0], cards[2] else: card1, card2 = cards[1], cards[2] if abs(int(card1[0]) - int(card2[0])) <= 2: ans = 1 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a, b, c = map(str, input().split()) a0, b0, c0 = int(a[0]), int(b[0]), int(c[0]) l = [a0, b0, c0] l.sort() mx, mn = max(a0, b0, c0), min(a0, b0, c0) if a == b == c or (l[2] - l[1] == 1 and l[1] - l[0] == 1 and a[1] == b[1] == c[1]): print(0) else: if (abs(a0 - b0) <= 2 and a[1] == b[1]) or (abs(c0 - b0) <= 2 and c[1] == b[1]) or (abs(a0 - c0) <= 2 and a[1] == c[1]): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; int main() { string t1, t2, t3; char m1, m2, m3; cin >> t1 >> t2 >> t3; int n1, n2, n3; vector<int> a(3); n1 = t1[0] - '0'; n2 = t2[0] - '0'; n3 = t3[0] - '0'; m1 = t1[1]; m2 = t2[1]; m3 = t3[1]; a[0] = n1; a[1] = n2; a[2] = n3; sort(a.begin(), a.end()); if (t1 == t2 && t2 == t3) { cout << 0; return 0; } else if (t1 == t2 || t2 == t3 || t3 == t1) { cout << 1; return 0; } if (m1 == m2 && m2 == m3 && a[0] + 1 == a[1] && a[1] + 1 == a[2]) { cout << 0; return 0; } else if (m1 == m2 && m2 == m3 && abs(a[0] - a[1]) <= 2) { cout << 1; return 0; } else if (m1 == m2 && m2 == m3 && abs(a[1] - a[2]) <= 2) { cout << 1; return 0; } else if (m1 == m2 && m2 == m3 && abs(a[0] - a[2]) <= 2) { cout << 1; return 0; } else if (m1 == m2 && m2 == m3) { cout << 2; return 0; } if (m1 != m2 && m2 != m3 && m1 != m3) { cout << 2; return 0; } if (m1 == m2 && abs(n1 - n2) <= 2) { cout << 1; return 0; } else if (m1 == m2) { cout << 2; return 0; } if (m1 == m3 && abs(n1 - n3) <= 2) { cout << 1; return 0; } else if (m1 == m3) { cout << 2; return 0; } if (m3 == m2 && abs(n3 - n2) <= 2) { cout << 1; return 0; } else if (m3 == m2) cout << 2; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
l=[x for x in input().split()] m=[0]*10 p=[0]*10 s=[0]*10 for i in range(len(l)): if l[i][1]=="m": m[int(l[i][0])]+=1 elif l[i][1]=="p": p[int(l[i][0])]+=1 else: s[int(l[i][0])]+=1 flag1=1 flag2=1 flag3=1 flag4=1 flag5=1 for i in range(10): if m[i]>=3 or p[i]>=3 or s[i]>=3: print(0) flag1=0 flag2=0 flag3=0 flag4=0 flag5=0 break if flag3: for i in range(8): if m[i]>=1 and m[i+1]>=1 and m[i+2]>=1 or p[i]>=1 and p[i+1]>=1 and p[i+2]>=1 or s[i]>=1 and s[i+1]>=1 and s[i+2]>=1: print(0) flag1=0 flag2=0 flag4=0 flag5=0 break if flag1: for i in range(9): if m[i]>=1 and m[i+1]>=1 or p[i]>=1 and p[i+1]>=1 or s[i]>=1 and s[i+1]>=1: print(1) flag2=0 flag4=0 flag5=0 break if flag4: for i in range(10): if m[i]==2 or p[i]==2 or s[i]==2: print(1) flag2=0 flag5=0 break if flag5: for i in range(8): if m[i]>=1 and m[i+2]>=1 or p[i]>=1 and p[i+2]>=1 or s[i]>=1 and s[i+2]>=1: print(1) flag2=0 break if flag2: if len(l)>=1: print(2) else: print(3)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
mast={ 'm':0, 'p':1, 's':2 } cd = input().split() ms = {} pd = {} for c in cd: x=int(c[0]) y=mast[c[1]] ms[x] = ms.get(x,[]) ms[x].append(y) pd[y] = pd.get(y, []) pd[y].append(x) #print(ms) rm = 2 for k,v in ms.items(): if len(v)==2: if v[0]==v[1]: rm = min(rm,1) elif len(v)==3: if v[0]==v[1] and v[1]==v[2] and v[0]==v[2]: rm = min(rm, 0) elif v[0]!=v[1] and v[1]!=v[2] and v[0]!=v[2]: pass elif v[0]==v[1] or v[1]==v[2] or v[0]==v[2]: rm = min(rm, 1) for k,v in pd.items(): v.sort() if len(v) == 2: if v[0]-v[1]==-1 or v[0]-v[1]==-2: rm = min(rm, 1) elif len(v) == 3: if v[2]-v[1]==1 and v[1]-v[0]==1: rm = min(rm, 0) elif v[1]-v[0]==1 or v[1]-v[0]==2: rm = min(rm, 1) elif v[2]-v[1]==1 or v[2]-v[1]==2: rm = min(rm, 1) print(rm)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; string s[3]; cin >> s[0] >> s[1] >> s[2]; if (s[0] == s[1] and s[1] == s[2]) { cout << "0\n"; return 0; } sort(s, s + 3); char a = s[0][0] + 1; char c = s[2][0] - 1; if (s[0][1] == s[1][1] and s[1][1] == s[2][1]) { if (a == c and c == s[1][0]) { cout << "0\n"; return 0; } } if (s[0] == s[1] or s[1] == s[2] or s[0] == s[2]) { cout << "1\n"; return 0; } if (s[0][1] == s[1][1] and abs((long long)(s[0][0] - s[1][0])) <= 2 and abs((long long)(s[0][0] - s[1][0])) > 0) { cout << "1\n"; return 0; } if (s[0][1] == s[2][1] and abs((long long)(s[0][0] - s[2][0])) <= 2 and abs((long long)(s[0][0] - s[2][0])) > 0) { cout << "1\n"; return 0; } if (s[2][1] == s[1][1] and abs((long long)(s[2][0] - s[1][0])) <= 2 and abs((long long)(s[2][0] - s[1][0])) > 0) { cout << "1\n"; return 0; } cout << "2\n"; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class P28 { public static void main(String args[]) { try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String a[]=br.readLine().split(" "); int countm=0,countp=0,counts=0; for(String x:a) { if(x.charAt(1)=='m') countm++; else if(x.charAt(1)=='p') countp++; else counts++; } if(countm==3||counts==3||countp==3) { int num1=Integer.parseInt(String.valueOf(a[0].charAt(0))); int num2=Integer.parseInt(String.valueOf(a[(1)].charAt(0))); int num3=Integer.parseInt(String.valueOf(a[2].charAt(0))); if(num1==num2&&num3==num2||consecutive(num1,num2,num3)) System.out.println(0); else if(Math.abs(num1-num2)==1||Math.abs(num1-num2)==2||Math.abs(num1-num2)==0||Math.abs(num3-num2)==1||Math.abs(num3-num2)==2||Math.abs(num3-num2)==0||Math.abs(num1-num3)==1||Math.abs(num1-num3)==2||Math.abs(num1-num3)==0) System.out.println(1); else System.out.println(2); } else { int temp=Math.max(countp, Math.max(countm, counts)); if(temp==1) System.out.println(2); else { for(int i=0;i<a.length;i++) { if(a[i].charAt(1)==a[(i+1)%3].charAt(1)) { int num1=Integer.parseInt(String.valueOf(a[i].charAt(0))); int num2=Integer.parseInt(String.valueOf(a[(i+1)%3].charAt(0))); if(Math.abs(num1-num2)==0||Math.abs(num1-num2)==1||Math.abs(num1-num2)==2) { System.out.println(1); return; } else { System.out.println(2); return; } } } } } } catch(Exception e) { e.printStackTrace(); } } public static boolean consecutive( int numberOne,int numberTwo,int numberThree) { int[] test = new int[] {numberOne, numberTwo, numberThree}; Arrays.sort(test); for (int i = 0; i < test.length - 1; i++) { if (test[i] + 1 != test[i + 1]) { // Not sequential return false; } } return true; } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
import java.io.*; public class Codechef { public static void main(String args[]) throws NumberFormatException, IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[][]=new int[10][27]; int count=0; String []line=br.readLine().split(" "); String str1=line[0]; String str2=line[1]; String str3=line[2]; //System.out.println(arr[str1.charAt(0)][(str1.charAt(1)-'a')+1]); arr[str1.charAt(0)-'0'][(str1.charAt(1)-'a')+1]++; arr[str2.charAt(0)-'0'][(str2.charAt(1)-'a')+1]++; arr[str3.charAt(0)-'0'][(str3.charAt(1)-'a')+1]++; if(count<=arr[str1.charAt(0)-'0'][str1.charAt(1)-'a'+1]) { count=arr[str1.charAt(0)-'0'][str1.charAt(1)-'a'+1]; //System.out.println(count); } if(count<=arr[str2.charAt(0)-'0'][str2.charAt(1)-'a'+1]) { count=arr[str2.charAt(0)-'0'][str2.charAt(1)-'a'+1]; //System.out.println(count); } if(count<=arr[str3.charAt(0)-'0'][str3.charAt(1)-'a'+1]) { count=arr[str3.charAt(0)-'0'][str3.charAt(1)-'a'+1]; //System.out.println(count); } for(int i=1;i<10;i++) { if(arr[i][13]>0) arr[i][13]=1; if(arr[i][16]>0) arr[i][16]=1; if(arr[i][19]>0) arr[i][19]=1; } //System.out.println(arr[1][13]+" "+arr[1][(str1.charAt(1)-'a')+1]+" "+arr[1][13]); for(int i=1;i<8;i++) { if(count<=(arr[i][13]+arr[i+1][13]+arr[i+2][13])) { count=(arr[i][13]+arr[i+1][13]+arr[i+2][13]); //System.out.println(count); } if(count<=(arr[i][16]+arr[i+1][16]+arr[i+2][16])) { count=(arr[i][16]+arr[i+1][16]+arr[i+2][16]); //System.out.println(count); } if(count<=(arr[i][19]+arr[i+1][19]+arr[i+2][19])) { count=(arr[i][19]+arr[i+1][19]+arr[i+2][19]); //System.out.println(count); } } System.out.println(3-count); } }
JAVA
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
#include <bits/stdc++.h> using namespace std; long long smallest(long long x, long long y, long long z) { long long c = 0; while (x && y && z) { x--; y--; z--; c++; } return c; } long long smallest(long long x, long long y) { return (x < y) ? x : y; } void solve() { string input[3]; long long countType1 = 0; long long countType2 = 0; long long countType3 = 0; for (long long i = 0; i < 3; i++) cin >> input[i]; long long count = 0; if (input[1] == input[0]) { if (input[2] == input[1]) { count = 3; } else count = 2; } else { if (input[1] == input[2] || input[0] == input[2]) { count = 2; } else count = 1; } if (countType1 == 2) { cout << 0 << endl; } else { long long moreReqd[3][3]; vector<long long> k(3, 0); for (long long i = 0; i < 3; i++) { if (input[i][1] == 'm') { moreReqd[0][k[0]++] = input[i][0]; } else if (input[i][1] == 'p') { moreReqd[1][k[1]++] = input[i][0]; } else { moreReqd[2][k[2]++] = input[i][0]; } } sort(moreReqd[0], moreReqd[0] + k[0]); sort(moreReqd[1], moreReqd[1] + k[1]); sort(moreReqd[2], moreReqd[2] + k[2]); long long maxi = INT_MIN; for (long long i = 0; i < 3; i++) { if (k[i] == 0) { } else if (k[i] == 1) { if (maxi < 1) { maxi = 1; } } else if (k[i] == 2) { if (moreReqd[i][1] - moreReqd[i][0] > 2) { if (maxi < 1) { maxi = 1; } } else { if (maxi < 2) { maxi = 2; } } } else { if (moreReqd[i][0] == moreReqd[i][1] - 1 && moreReqd[i][0] == moreReqd[i][2] - 2) { maxi = 3; break; } else if (moreReqd[i][0] == moreReqd[i][1] - 1 || moreReqd[i][0] == moreReqd[i][1] - 2 || moreReqd[i][1] == moreReqd[i][2] - 1 || moreReqd[i][1] == moreReqd[i][2] - 2) { if (maxi < 2) maxi = 2; } else if (moreReqd[i][0] == moreReqd[i][1] && moreReqd[i][2] - moreReqd[i][0] <= 2 || moreReqd[i][1] == moreReqd[i][2] && moreReqd[i][2] - moreReqd[i][0] <= 2) { if (maxi < 2) { maxi = 2; } } else { if (maxi < 1) maxi = 1; } } } long long min2 = 3 - maxi; long long min1 = 3 - count; cout << min({min1, min2}) << endl; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
a = input().split() a.sort() if len(set(a)) == 1: print(0) elif len(set(a)) == 2: print(1) else: d = {} for i in a: n = int(i[0]) c = i[1] if c in d: d[c].append(n) else: d[c] = [n] v = list(d.values()) cond0 = False cond1 = False for i in v: if len(i) == 2: i = sorted(i) x = i[0] y = i[1] if x + 1 == y or y - x == 2: cond1 = True if len(i) == 3: i = sorted(i) x = i[0] y = i[1] z = i[2] if x + 1 == y and y + 1 == z: cond0 = True if (x+1 == y) or (x+2 == y) or (y+2 ==z) or (y+1 ==z): cond1 = True if cond0: print(0) elif cond1: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
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 Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 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
8
t=raw_input().split() t.sort() k = {} def get_draws(): for x in t: if not x[-1] in k: k[x[-1]]=[] k[x[-1]].append(int(x[:-1])) for i in k.keys(): length=len(k[i]) all_same = k[i].count(k[i][0])==length if all_same: if (length==3): print '0' return elif length==2: print '1' return else: diff_list = [] for x, y in zip(k[i][0::], k[i][1::]): diff_list.append(y-x) proceed = any(n in [0,1,2] for n in diff_list) one_diff = all(n==1 for n in diff_list) if proceed: if length==3 and one_diff: print '0' return else: print '1' return print '2' return get_draws()
PYTHON