problem
stringlengths
1.25k
8.96k
answer
stringlengths
47
993
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```input() w = input() ans = set("abcdefghijklmnopqrstuvwxyz") for _ in range(int(input())): q = input() if any(a!='*' and a!=b or a=='*' and b in w for a, b in zip(w, q)): continue ans &= set(q)-set(w) print(len(ans))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```def is_good_guess(guess, original_string, pos_not_reveal, pos_reveal, reveal): for i in pos_not_reveal: if guess[i] in reveal: return False for i in pos_reveal: if guess[i] != original_string[i]: return False return True n = int(input()) l2 = list(input()) reveal ...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n, word = int(input()), input() stars_index, lttr_index = [], [] lttrs = set() for i in range(len(word)): if word[i] == '*': stars_index.append(i) else: lttr_index.append(i) lttrs.add(word[i]) m = int(input()) pssbl_lttrs = None for i in range(m): p_word = input() f = False for j in stars_index: if p_...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```from collections import * input() w, m, c, n = input(), int(input()), Counter(), 0 for _ in range(m): q = input() if any(a!='*' and a!=b or a=='*' and b in w for a, b in zip(w, q)): continue c.update(set(l for i, l in enumerate(q) if w[i]=='*')) n += 1 print(len([l for l, x in c.items() if x==n]))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) s = input() already = set() for i in s: if i != '*': already.add(i) ans = [chr(ord('a') + i) for i in range(26)] ans = set(ans) for i in range(int(input())): cur = input() can = True clos = set() #should be closed for j in range(n): if s[j] == '*': clo...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```import math n=int(input()) l=[] for i in range(n): s=input() while("kh" in s): s=s.replace("kh","h") while("u" in s): s=s.replace("u","oo") l.append(s) print(len(set(l)))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n=eval(input()) ans=set() for i in range(n): s=input() # print(s) t=0 while t<=100: s=s.replace("u","oo") s=s.replace("kh","h") t+=1 # print(s) ans.add(s) print(len(ans))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) st = set() for i in range(n): word = input() word = word.replace('u', 'oo') while word.replace('kh', 'h') != word: word = word.replace('kh', 'h') st.add(word) print(len(st))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) words = {} for i in range(n): w = input() while 'kh' in w or 'u' in w: w = w.replace('u','oo').replace('kh','h') try: words[w] += 1 except KeyError: words[w] = 1 print(len(words.keys()))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```#!/usr/bin/env python3 names = int(input()) def simplify_name(s): s_orig = s[:] s = s.replace('u', 'oo') s = s.replace('kh', 'h') if s == s_orig: return s else: return simplify_name(s) name_set = set() for idx in range(names): name_set.add(simplify_name(input())) print(le...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) ss = [0] * (n + 1) gg = [0] * (n + 1) #mins = [0] * n maxs = [0] * n curMin = -10 ** 10 curMax = -curMin for i in range(n): s, g = map(int, input().split(' ')) ss[i] = s gg[i] = g curMin = max(curMin - 1, s) curMax = min(curMax + 1, s + g) if curMin > curMax: print...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) s = [0]*n g = [0]*n for i in range(n): a,b = map(int,input().split()) s[i] = a g[i] = a+b for i in range(1,n): g[i] = min(g[i],g[i-1]+1) for i in range(n-2,-1,-1): g[i] = min(g[i],g[i+1]+1) ans = 0 for i in range(n): if s[i] <= g[i]: ans += g[i] - s[i] else: ...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) a[i][1]+=a[i][0] for i in range(1,n): if a[i][1]>a[i-1][1]+1: a[i][1]=a[i-1][1]+1 if a[i][1]<a[i][0]:exit(print(-1)) s=a[-1][1]-a[-1][0] for i in range(n-2,-1,-1): if a[i][1] > a[i + 1][1] + 1: a[i...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```I=lambda:map(int,input().split()) a,b=I() c,d=I() print((max(1,abs(a-c))+max(1,abs(b-d))<<1)+4)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```c_x,c_y = map(int,input().split()) f_x,f_y = map(int,input().split()) if c_x == f_x: print(2*(abs(c_y-f_y)+1)+4) elif c_y == f_y: print(2*(abs(c_x-f_x)+1)+4) else: p_x = f_x+1 if c_x < f_x else f_x-1 p_y = f_y+1 if c_y < f_y else f_y-1 print(2*(abs(p_x-c_x)+abs(p_y-c_y)))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) a, b = abs(x2-x1), abs(y2-y1) if a == 0: print(4 + 2 * (b + 1)) elif b == 0: print(2 * (a + 1) + 4) else: print(2*(a+1)+2*(b+1))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```import sys input = sys.stdin.readline x, y = map(int, input().split()) z, w = map(int, input().split()) print(2*(max(1, abs(z-x)) + max(1, abs(w-y))) + 4)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) if x1 == x2 or y1 == y2: z = abs(x1 - x2) + abs(y1 - y2) - 1 print(z * 2 + 8) else: mas = [[x2 - 1, y2 - 1], [x2 + 1, y2 - 1], [x2 - 1, y2 + 1], [x2 + 1, y2 + 1]] mx = 0 for i in mas: a = i[0] b = i[1] #print(a, b) mx = max(mx, abs(x1...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```# Problem G num = input() num_list = [] for i in range(len(num)): num_list.append(int(num[i])) myMod = (10 ** 9) + 7 length = len(num_list) f = [0] * (length + 1) t = [1] * (length + 1) for i in range(length): f[i+1] = (f[i] * 10 + 1) % myMod t[i+1] = (t[i] * 10) % myMod ans = 0 for i in range(1, 10): ...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```from sys import stdin _, *l = stdin.read().splitlines() for i, s in enumerate(l): p, q, b = map(int, s.split()) l[i] = 'Infinite' if p * pow(b, 64, q) % q else 'Finite' print('\n'.join(l))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```# python3 import sys from math import gcd import sys sys.set_int_max_str_digits(0) def is_finite(p, q, b): return not p * pow(b, 64, q) % q input() print("\n".join("Finite" if is_finite(*map(int, line.split())) else "Infinite" for line in sys.stdin.readlines()))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```input() print('\n'.join(['Infinite' if p * pow(b, 99, q) % q else 'Finite' for p, q, b in map(lambda l: map(int, l.split()), __import__('sys').stdin.readlines())]))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```input() print('\n'.join(map(lambda x: (lambda p, q, b: 'Infinite' if p * pow(b, 60, q) % q else 'Finite')(*x), map(lambda l:map(int, l.split()), __import__('sys').stdin.readlines()))))```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n = int(input()) ans = '' while n > 0: p, q, b = map(int, input().split(' ')) for i in range(6): b = b * b % q if b * p % q == 0: ans += 'Finite\n' else: ans += 'Infinite\n' n -= 1 print (ans)```
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```import decimal from math import ceil, floor, inf, log n = input() if n=='1': print(1) exit() decimal.getcontext().prec = len(n)+6 decimal.getcontext().Emax = len(n)+6 log3 = log(10)*(len(n)-1)/log(3) pref = n if len(pref)>20: pref = pref[:20] pref = pref[0] + '.' + pref[1:] log3 += log(float(pref))/log(...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```from decimal import * from math import * getcontext().Emax = 1500010 getcontext().prec = 1500010 a = Decimal(input()) mi = int(log(float(a.scaleb(-a.logb())),3) + log(10,3) * (float(a.logb()) - 1) - 1) an = mi * 3 A = Decimal(3) ** mi while(A < a): A *= 3 an += 3 ans = an for i in range(2): A = A * 2 / 3 an -= 1...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n,k=map(int, input().split()) v=list(map(int, input().split())) pos, pref=[-1], [] ans=0 for i in range(n): if v[i]!=1: pos.append(i) if v[i]==1 and k==1: ans+=1 if i: pref.append(pref[-1]+v[i]) else: pref.append(v[i]) pos.append(n) m=len(pos) #print("m",m) inf=int(2e18+10) for i in range(1,m-1): p=1 ...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n, k = map(int, input().split()) A = list(map(int, input().split())) from itertools import accumulate C = [0]+A C = list(accumulate(C)) A = [0]+A P = [0]*(n+1) x = 0 # P[i]: iの直前にある2以上の項のindex for i in range(1, n+1): P[i] = x if A[i] > 1: x = i INF = 2*10**18+1 ans = 0 for i in range(1, n+1): p =...
You are a coding expert. Given a competition-level coding problem, you need to write a Python program to solve it. You may start by outlining your thought process. In the end, please provide the complete code in a code block enclosed with ``` ```. The code should take stdin as input and print the output. Your program s...
```n,k = map(int,input().split()) A = list(map(int,input().split())) A = [0]+A; x = 0 prev = [0 for i in range(n+1)] sm = [0 for i in range(n+1)] for i in range(1,n+1): prev[i] = x if A[i]>1: x = i sm[i] = A[i]+sm[i-1] lim = int(2*(10**18)) ans = 0 for i in range(1,n+1): p = 1 j = i while j: if lim//A[j]>p: ...