text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. problem Hanako is playing with n (4 ≤ n ≤ 10) cards side by side. Each card has one integer between 1 and 99. Hanako chose k cards (2 ≤ k ≤ 4) from these cards and arranged them in a horizontal row to make an integer. How many kinds of integers can Hanako make in total? For example, consider that you are given five cards of 1, 2, 3, 13, 21 and choose three of them to make an integer. By arranging 2, 1, and 13 in this order, we can make an integer 2113. Also, by arranging 21, 1, and 3 in this order, the same integer 2113 can be created. In this way, the same integer may be created from a combination of different cards. Given the integers written on n cards, create a program to find the number of integers that can be created by selecting k cards from them and arranging them in a horizontal row. input The input consists of multiple datasets. Each dataset is given in the following format. Each dataset consists of 2 + n rows. The number of cards n (4 ≤ n ≤ 10) is written on the first line, and the number of cards to be selected k (2 ≤ k ≤ 4) is written on the second line. On the 2 + i line (1 ≤ i ≤ n), the integer from 1 to 99 written on the i-th card is written. When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5. output For each data set, the number of integers that Hanako can create is output on one line. Examples Input 4 2 1 2 12 1 6 3 72 2 12 7 2 1 0 0 Output 7 68 Input None Output None Submitted Solution: ``` from itertools import permutations as P import sys r=sys.stdin.readline for e in iter(r,'0'): n,k=int(e),int(r()) C=[r()for _ in[0]*n] print(len(set(''.join(s)for s in P(C,k)))) ``` No
9,000
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. problem Hanako is playing with n (4 ≤ n ≤ 10) cards side by side. Each card has one integer between 1 and 99. Hanako chose k cards (2 ≤ k ≤ 4) from these cards and arranged them in a horizontal row to make an integer. How many kinds of integers can Hanako make in total? For example, consider that you are given five cards of 1, 2, 3, 13, 21 and choose three of them to make an integer. By arranging 2, 1, and 13 in this order, we can make an integer 2113. Also, by arranging 21, 1, and 3 in this order, the same integer 2113 can be created. In this way, the same integer may be created from a combination of different cards. Given the integers written on n cards, create a program to find the number of integers that can be created by selecting k cards from them and arranging them in a horizontal row. input The input consists of multiple datasets. Each dataset is given in the following format. Each dataset consists of 2 + n rows. The number of cards n (4 ≤ n ≤ 10) is written on the first line, and the number of cards to be selected k (2 ≤ k ≤ 4) is written on the second line. On the 2 + i line (1 ≤ i ≤ n), the integer from 1 to 99 written on the i-th card is written. When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5. output For each data set, the number of integers that Hanako can create is output on one line. Examples Input 4 2 1 2 12 1 6 3 72 2 12 7 2 1 0 0 Output 7 68 Input None Output None Submitted Solution: ``` from itertools import permutations as P import sys r=sys.stdin.readline for e in iter(r,'0\n'): n,k=int(e),int(r()) C=[r()for _ in[0]*n] print(len(set(''.join(s)for s in P(C,k)))) ``` No
9,001
Provide a correct Python 3 solution for this coding contest problem. She is an apprentice wizard. She first learned the magic of manipulating time. And she decided to open a liquor store to earn a living. It has to do with the fact that all the inhabitants of the country where she lives love alcohol. Residents especially like sake that has been aged for many years, and its value increases proportionally with the number of years it has been aged. It's easy to imagine that 100-year-old liquor is more valuable than 50-year-old liquor. And it's also difficult to get. She used her magic to instantly make a long-aged liquor and sell it to earn money. She priced the sake according to the number of years it was aged. For example, if you need 5 years of aging, you will have to pay 5 emers, and if you have 100 years of aging, you will have to pay 100 emers. For her, making either one uses the same magic, so there is no difference. A long-term aging order is better for her. Since she is still immature, there are two restrictions on the magic that can be used now. She can't age sake for more than n years now. Also, there are m integers, and it is impossible for her to age sake for those and multiple years. She is worried if she can live only by running a liquor store. She cannot predict how much income she will earn. So I decided to decide if I needed to do another job based on my expected daily income. Fortunately, she can grow vegetables, take care of pets, cook, sew, carpentry, and so on, so even if she has a small income at a liquor store, she doesn't seem to be in trouble. It's your job to calculate the expected value of her daily income. The following three may be assumed when calculating the expected value. Only one order can be accepted per day. Residents do not order liquor for years that she cannot make. The number of years of inhabitants' orders is evenly distributed. Input The input consists of multiple cases. Each case is given in the following format. n m p0 ... pm-1 Residents request liquor that has been aged for the number of years with an integer value between 1 and n. However, no one asks for m integer pi and its multiples. The end of the input is given with n = 0 and m = 0. Each value meets the following conditions 2 ≤ n ≤ 2,000,000,000 1 ≤ m ≤ 20 Also, pi is guaranteed to be divisible by n. The number of test cases does not exceed 200. Output Output the expected value of her daily income. If there is no possible number of years for the resident's order, output 0 as the expected value of the answer. The correct answer prepared by the judge and an error of 1e-5 or less are allowed as the correct answer. Example Input 12 3 2 3 6 12 4 1 2 3 6 0 0 Output 6.0000000000 0.0000000000 "Correct Solution: ``` # AOJ 1076 Time Manipulation # Python3 2018.7.10 bal4u while True: n, m = map(int, input().split()) if n == 0: break p = list(map(int, input().split())) if 1 in p: n = 0 print(n/2) ```
9,002
Provide a correct Python 3 solution for this coding contest problem. She is an apprentice wizard. She first learned the magic of manipulating time. And she decided to open a liquor store to earn a living. It has to do with the fact that all the inhabitants of the country where she lives love alcohol. Residents especially like sake that has been aged for many years, and its value increases proportionally with the number of years it has been aged. It's easy to imagine that 100-year-old liquor is more valuable than 50-year-old liquor. And it's also difficult to get. She used her magic to instantly make a long-aged liquor and sell it to earn money. She priced the sake according to the number of years it was aged. For example, if you need 5 years of aging, you will have to pay 5 emers, and if you have 100 years of aging, you will have to pay 100 emers. For her, making either one uses the same magic, so there is no difference. A long-term aging order is better for her. Since she is still immature, there are two restrictions on the magic that can be used now. She can't age sake for more than n years now. Also, there are m integers, and it is impossible for her to age sake for those and multiple years. She is worried if she can live only by running a liquor store. She cannot predict how much income she will earn. So I decided to decide if I needed to do another job based on my expected daily income. Fortunately, she can grow vegetables, take care of pets, cook, sew, carpentry, and so on, so even if she has a small income at a liquor store, she doesn't seem to be in trouble. It's your job to calculate the expected value of her daily income. The following three may be assumed when calculating the expected value. Only one order can be accepted per day. Residents do not order liquor for years that she cannot make. The number of years of inhabitants' orders is evenly distributed. Input The input consists of multiple cases. Each case is given in the following format. n m p0 ... pm-1 Residents request liquor that has been aged for the number of years with an integer value between 1 and n. However, no one asks for m integer pi and its multiples. The end of the input is given with n = 0 and m = 0. Each value meets the following conditions 2 ≤ n ≤ 2,000,000,000 1 ≤ m ≤ 20 Also, pi is guaranteed to be divisible by n. The number of test cases does not exceed 200. Output Output the expected value of her daily income. If there is no possible number of years for the resident's order, output 0 as the expected value of the answer. The correct answer prepared by the judge and an error of 1e-5 or less are allowed as the correct answer. Example Input 12 3 2 3 6 12 4 1 2 3 6 0 0 Output 6.0000000000 0.0000000000 "Correct Solution: ``` while True: n, m = map(int, input().split()) if n == 0: break p = sorted(list(map(int, input().split()))) flag = 0 if p[0]==1 else 1 print('{:.10f}'.format(n/2 if flag else 0)) ```
9,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. She is an apprentice wizard. She first learned the magic of manipulating time. And she decided to open a liquor store to earn a living. It has to do with the fact that all the inhabitants of the country where she lives love alcohol. Residents especially like sake that has been aged for many years, and its value increases proportionally with the number of years it has been aged. It's easy to imagine that 100-year-old liquor is more valuable than 50-year-old liquor. And it's also difficult to get. She used her magic to instantly make a long-aged liquor and sell it to earn money. She priced the sake according to the number of years it was aged. For example, if you need 5 years of aging, you will have to pay 5 emers, and if you have 100 years of aging, you will have to pay 100 emers. For her, making either one uses the same magic, so there is no difference. A long-term aging order is better for her. Since she is still immature, there are two restrictions on the magic that can be used now. She can't age sake for more than n years now. Also, there are m integers, and it is impossible for her to age sake for those and multiple years. She is worried if she can live only by running a liquor store. She cannot predict how much income she will earn. So I decided to decide if I needed to do another job based on my expected daily income. Fortunately, she can grow vegetables, take care of pets, cook, sew, carpentry, and so on, so even if she has a small income at a liquor store, she doesn't seem to be in trouble. It's your job to calculate the expected value of her daily income. The following three may be assumed when calculating the expected value. Only one order can be accepted per day. Residents do not order liquor for years that she cannot make. The number of years of inhabitants' orders is evenly distributed. Input The input consists of multiple cases. Each case is given in the following format. n m p0 ... pm-1 Residents request liquor that has been aged for the number of years with an integer value between 1 and n. However, no one asks for m integer pi and its multiples. The end of the input is given with n = 0 and m = 0. Each value meets the following conditions 2 ≤ n ≤ 2,000,000,000 1 ≤ m ≤ 20 Also, pi is guaranteed to be divisible by n. The number of test cases does not exceed 200. Output Output the expected value of her daily income. If there is no possible number of years for the resident's order, output 0 as the expected value of the answer. The correct answer prepared by the judge and an error of 1e-5 or less are allowed as the correct answer. Example Input 12 3 2 3 6 12 4 1 2 3 6 0 0 Output 6.0000000000 0.0000000000 Submitted Solution: ``` while True: n, m = map(int, input().split()) if n == 0: break flag = 1 for i in range(m): if int(input()) == 1: flag = 0 print('{:.10f}'.format(n/2 if flag else 0)) ``` No
9,004
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` n=int(input()) d=''.join(''.join(input().split())for i in[0]*(n//19+(n%19!=0))) i=0 while 1: if d.find(str(i))==-1: print(i) exit() i+=1 ```
9,005
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` import sys def main(): n = int(input()) d = "" while len(d) != n: d += "".join(input().split()) digits = set() for i in range(n): for j in range(i+1,n+1): # print(i,j) seq = d[i:j] if not seq: continue digits.add(int(seq)) digits = sorted(list(digits)) for i in range(digits[-1]+1): if i != digits[i]: print(i) return if __name__ == '__main__': main() ```
9,006
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` n, *d = map(int, open(0).read().split()) d = ''.join(map(str, d)) se = set() for i in range(n): for j in range(i + 1, n + 1): se.add(int(d[i:j])) ans = 0 while ans in se: ans += 1 print(ans) ```
9,007
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` j=''.join n=int(input()) d=j(j(input().split())for i in[0]*(n//19+(n%19!=0))) i=0 while 1: if d.find(str(i))<0: print(i) exit() i+=1 ```
9,008
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n = I() a = [] while len(a) < n: a += LS() s = set() for i in range(n): s.add(int(a[i])) for j in range(i+1,min(i+2,n)): s.add(int(a[i]+a[j])) for k in range(j+1,min(j+2,n)): s.add(int(a[i]+a[j]+a[k])) r = -1 for i in range(1000): if not i in s: r = i break rr.append(r) break return '\n'.join(map(str, rr)) print(main()) ```
9,009
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` n = int(input()) length = int(n / 19) + 1 if n > 19 and n % 19 == 0: length -= 1 d = [] for i in range(length): d += list(map(int, input().split())) start, end = 1, 10 l = [] flag = False for j in range(1, len(d)+1): for i in range(0, len(d)): tmp = ''.join(map(str, d[i:i+j])) if len(tmp) > 1 and tmp[0] == '0': pass elif len(tmp) == j: l.append(int(tmp)) l = sorted(set(l)) if start == 1: for k in range(0, 10): if k not in l: print(k) flag = True break else: for k in range(start, end): if k not in l: print(k) flag = True break if flag: break start, end = start*10, end*10 l = [] ```
9,010
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` n,*d=open(0).read().split() n=int(n) s=set() for i in range(n): for j in range(i+1,n+1): s|={int(''.join(d[i:j]))} for i,j in enumerate(sorted(s)): if i!=j: print(i) break ```
9,011
Provide a correct Python 3 solution for this coding contest problem. Example Input 3 3 0 1 Output 2 "Correct Solution: ``` n=int(input()) d=''.join([''.join(input().split())for i in range(n//19+(n%19!=0))]) i=0 while True: if d.find(str(i))==-1: print(i) exit() i+=1 ```
9,012
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` n = int(input()) d = list() while len(d) < n: d.extend(list(map(int, input().split()))) appear = set() for i in range(n): for j in range(0, min(3, n - i)): appear.add(int("".join(map(str, d[i:i + j + 1])))) for i in range(1000): if not i in appear: print(i) break ``` Yes
9,013
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` j=''.join n=int(input()) d=j(j(input().split())for i in[0]*(n//19+(n%19!=0))) i=0 while 1: if d.find(str(i))==-1: print(i) exit() i+=1 ``` Yes
9,014
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` N = int(input()) src = '' while len(src) < N: src += ''.join(input().split()) n = 0 while True: if str(n) not in src: print(n) break n += 1 ``` Yes
9,015
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` import sys input() a = [] for line in sys.stdin: a.extend([x for x in line.split()]) a = "".join(a) for i in range(int(a)): if str(i) not in a: print(i) break ``` Yes
9,016
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` import sys n = int(input()) length = int(n / 19) + 1 d = [] for i in range(length): d += list(map(int, input().split())) start, end = 1, 10 l = [] flag = False for j in range(1, len(d)+1): for i in range(0, len(d)): tmp = ''.join(map(str, d[i:i+j])) if len(tmp) > 1 and tmp[0] == '0': pass elif len(tmp) == j: l.append(int(tmp)) l = sorted(set(l)) if start == 1: for k in range(0, 10): if k not in l: print(k) flag = True break else: for k in range(start, end): if k not in l: print(k) flag = True break if flag: break start, end = start*10, end*10 l = [] ``` No
9,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` n = int(input()) d = ''.join(input().split()) for i in range(100000): if d.find(str(i)) == -1: print(i) exit() ``` No
9,018
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` n = int(input()) d = ''.join(input().split()) i = 0 while True: if d.find(str(i)) == -1: print(i) exit() i += 1 ``` No
9,019
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Example Input 3 3 0 1 Output 2 Submitted Solution: ``` n = int(input()) length = int(n / 19) + 1 d = [] for i in range(length): d += list(map(int, input().split())) start, end = 1, 10 l = [] flag = False for j in range(1, len(d)+1): for i in range(0, len(d)): tmp = ''.join(map(str, d[i:i+j])) if len(tmp) > 1 and tmp[0] == '0': pass elif len(tmp) == j: l.append(int(tmp)) l = sorted(set(l)) if start == 1: for k in range(0, 10): if k not in l: print(k) flag = True break else: for k in range(start, end): if k not in l: print(k) flag = True break if flag: break start, end = start*10, end*10 l = [] ``` No
9,020
Provide a correct Python 3 solution for this coding contest problem. J - Tree Reconstruction Problem Statement You have a directed graph. Some non-negative value is assigned on each edge of the graph. You know that the value of the graph satisfies the flow conservation law That is, for every node v, the sum of values on edges incoming to v equals to the sum of values of edges outgoing from v. For example, the following directed graph satisfies the flow conservation law. <image> Suppose that you choose a subset of edges in the graph, denoted by E', and then you erase all the values on edges other than E'. Now you want to recover the erased values by seeing only remaining information. Due to the flow conservation law, it may be possible to recover all the erased values. Your task is to calculate the smallest possible size for such E'. For example, the smallest subset E' for the above graph will be green edges in the following figure. By the flow conservation law, we can recover values on gray edges. <image> Input The input consists of multiple test cases. The format of each test case is as follows. N M s_1 t_1 ... s_M t_M The first line contains two integers N (1 \leq N \leq 500) and M (0 \leq M \leq 3,000) that indicate the number of nodes and edges, respectively. Each of the following M lines consists of two integers s_i and t_i (1 \leq s_i, t_i \leq N). This means that there is an edge from s_i to t_i in the graph. You may assume that the given graph is simple: * There are no self-loops: s_i \neq t_i. * There are no multi-edges: for all i < j, \\{s_i, t_i\\} \neq \\{s_j, t_j\\}. Also, it is guaranteed that each component of the given graph is strongly connected. That is, for every pair of nodes v and u, if there exist a path from v to u, then there exists a path from u to v. Note that you are NOT given information of values on edges because it does not effect the answer. Output For each test case, print an answer in one line. Sample Input 1 9 13 1 2 1 3 2 9 3 4 3 5 3 6 4 9 5 7 6 7 6 8 7 9 8 9 9 1 Output for the Sample Input 1 5 Sample Input 2 7 9 1 2 1 3 2 4 3 4 4 5 4 6 5 7 6 7 7 1 Output for the Sample Input 2 3 Sample Input 3 4 4 1 2 2 1 3 4 4 3 Output for the Sample Input 3 2 Example Input 9 13 1 2 1 3 2 9 3 4 3 5 3 6 4 9 5 7 6 7 6 8 7 9 8 9 9 1 Output 5 "Correct Solution: ``` from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): N, M = map(int, readline().split()) G = [[] for i in range(N)] for i in range(M): s, t = map(int, readline().split()) G[s-1].append(t-1) used = [0]*N cnt = 0 ans = 0 for i in range(N): if used[i]: continue que = deque([i]) used[i] = 1 while que: v = que.popleft() ans += len(G[v])-1 for w in G[v]: if used[w]: continue used[w] = 1 que.append(w) ans += 1 write("%d\n" % ans) solve() ```
9,021
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` # -*- coding: utf-8 -*- def assignColor(G): n = len(G) C = [-1 for i in range(n)] for i in range(n): if C[i] == -1: C = BFS(G, i, C) return C #@profile def BFS(G, start, C): n = len(G) Q = [start] C[start] = start while len(Q) != 0: u = Q.pop(0) v = [i for i in G[u] if C[i] == -1] # v := ??£??\???????????????????????¢?´¢?????????????????? for i in v: Q.append(i) C[i] = C[start] return C def main(): n, m = list(map(int, input().split())) G = [[] for i in range(n)] for i in range(m): s, t = list(map(int, input().split())) G[s].append(t) G[t].append(s) C = assignColor(G) q = int(input()) for i in range(q): s, t = list(map(int, input().split())) if C[s] == C[t]: print("yes") else: print("no") if __name__ == "__main__": main() ```
9,022
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat Apr 6 14:09:17 2019 @author: Yamazaki Kenichi """ import sys sys.setrecursionlimit(10**6) n,m = map(int,input().split()) A = [list(map(int,input().split())) for i in range(m)] q = int(input()) Q = [list(map(int,input().split())) for i in range(q)] class UnionFind(object): def __init__(self,size): self.table = [-1 for i in range(size)] def find(self,x): if self.table[x] < 0: return x else: self.table[x] = self.find(self.table[x]) return self.table[x] def union(self,x,y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: if self.table[s1] <= self.table[s2]: self.table[s1] += self.table[s2] self.table[s2] = s1 else: self.table[s2] += self.table[s1] self.table[s1] = s2 return True else: return False def check(self,x,y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: return False else: return True uf = UnionFind(n) for c in A: uf.union(c[0],c[1]) for c in Q: if uf.check(c[0],c[1]): print('yes') else: print('no') ```
9,023
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` from collections import deque def bfs(graph, start): visited = {start: None} unvisited = deque() unvisited.append(start) while unvisited: now = unvisited.popleft() for next in graph[now]: if not (next in visited): unvisited.append(next) visited[next] = now return visited n, m = map(int, input().split()) g = [set() for _ in range(n)] group = [] for i in range(m): a, b = map(int, input().split()) g[a].add(b) g[b].add(a) q = int(input()) for i in range(q): s, t = map(int, input().split()) for j in range(len(group)): if s in group[j] and t in group[j]: print('yes') break elif s in group[j]: print('no') break else: group_tmp = set(bfs(g, s).keys()) if t in group_tmp: print('yes') else: print('no') group.append(group_tmp) ```
9,024
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` n, m = map(int,input().split()) g = {} for i in range(m): s,t = map(int,input().split()) val = g.get(s,[]) val.append(t) g[s] = val val = g.get(t,[]) val.append(s) g[t] = val color = 1 done = [-1] + [-1] * n def dfs(g, s): done[s] = color stack = [s] while stack != []: t = stack.pop() for c in g.get(t,[]): if done[c] == -1: done[c] = color stack.append(c) q = int(input()) for v in range(n): if done[v] == -1: dfs(g,v) color += 1 for j in range(q): s,t = map(int,input().split()) if done[s] == done[t]: print('yes') else: print('no') ```
9,025
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` # coding: UTF-8 from collections import deque n,m = map(int,input().split()) link = [[] for _ in range(n)] part = [-1 for _ in range(n)] for _ in range(m): u,v = map(int,input().split()) link[u].append(v) link[v].append(u) searched = set() queue = deque() pindex = 0 for u in range(n): if u not in searched: queue.append(u) pindex += 1 searched.add(u) while queue: v = queue.popleft() part[v] = pindex for w in link[v]: if w not in searched: part[w] = pindex queue.append(w) searched.add(w) q = int(input()) answers = [] for _ in range(q): s,t = map(int,input().split()) if part[s] == part[t]: answers.append("yes") else: answers.append("no") for answer in answers: print(answer) ```
9,026
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` from collections import deque n,m = [int(i) for i in input().split()] F = [{} for i in range(n)] for _ in range(m): i,j = [int(i) for i in input().split()] F[i][j] = 1 F[j][i] = 1 q = int(input()) DP = [0 for i in range(n)] c = 1 d = deque() for i in range(n): if DP[i]: continue else: d.append(i) while(len(d)>0): tmp = d.pop() DP[tmp] = c for i in F[tmp]: if not DP[i]: d.append(i) c+=1 for i in range(q): s,t = [int(i) for i in input().split()] if DP[s] == DP[t]: print("yes") else: print("no") ```
9,027
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 output: yes yes no """ import sys def assign_color(): v_color = 0 for v in range(v_num): if color[v] == -1: graph_dfs(v, v_color) # use int as color to represent different connected groups v_color += 1 return None def graph_dfs(current, v_color): stack = list() stack.append(current) color[current] = v_color while stack: current = stack.pop() for adj in adj_table[current]: if color[adj] == -1: color[adj] = v_color stack.append(adj) return None def solve(): for edge in edges: source, target = map(int, edge) adj_table[source].append(target) adj_table[target].append(source) assign_color() for question in questions: q_source, q_target = map(int, question) if color[q_source] == color[q_target]: print("yes") else: print("no") return color if __name__ == '__main__': _input = sys.stdin.readlines() v_num, r_num = map(int, _input[0].split()) edges = map(lambda x: x.split(), _input[1:r_num + 1]) q_num = int(_input[r_num + 1]) questions = map(lambda x: x.split(), _input[r_num + 2:]) adj_table = tuple([] for _ in range(v_num)) color = [-1] * v_num ans = solve() ```
9,028
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no "Correct Solution: ``` n, m = map(int, input().split()) # 隣接リストの雛形 D = dict() for i in range(n): D[i] = [] # グラフを入力 for _ in range(m): s, t = map(int, input().split()) D[s].append(t) D[t].append(s) group = [-1]*n cnt = 0 for i in range(n): if group[i] == -1: group[i] = cnt stack = [i] while stack: v = stack.pop() for c in D[v]: if group[c] == -1: group[c] = cnt stack.append(c) cnt += 1 q = int(input()) for _ in range(q): s, t = map(int, input().split()) print('yes' if group[s] == group[t] else 'no') ```
9,029
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` def dfs(r, c): global color, G S = [r] color[r] = c while (0 < len(S)): u = S.pop() for u in G[u]: v = color[u] if (v == None): color[u] = c S.append(u) def assignColor(): global color, G, n c = 1 for i in range(n): if (color[i] == None): dfs(i, c) c += 1 n, m = list(map(int, input().split())) G = [[] for i in range(n)] for i in range(m): s, t = list(map(int, input().split())) G[s].append(t) G[t].append(s) color = [None for i in range(n)] assignColor() q = int(input()) for i in range(q): s, t = list(map(int, input().split())) if (color[s] == color[t]): print("yes") else: print("no") ``` Yes
9,030
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` # ALDS1_11_D Connected Components import sys import queue sys.setrecursionlimit(1000000) def check(adj, foot_print): n = len(foot_print) que = queue.Queue() num = 0 for i in range(n): if foot_print[i] == 0: num += 1 que.put(i) foot_print[i] = num bfs(adj, que, foot_print, num) def bfs(adj, que, foot_print, num): if not que.empty(): now = que.get() for i in adj[now]: if foot_print[i] == 0: foot_print[i] = num que.put(i) bfs(adj, que, foot_print, num) else: return def main(): n, m = map(int, sys.stdin.readline().strip().split()) adj_list = [[] for _ in range(n)] for i in range(m): s, t = map(int, sys.stdin.readline().strip().split()) adj_list[s].append(t) adj_list[t].append(s) foot_print = [0] * n check(adj_list, foot_print) q = int(input()) for i in range(q): s, t = map(int, sys.stdin.readline().strip().split()) if foot_print[s] == foot_print[t]: print('yes') else: print('no') if __name__ == '__main__': main() ``` Yes
9,031
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i in range(self.n) if self.parents[i] < 0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} def __str__(self): return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots()) n,m = map(int,input().split()) tree = UnionFind(n) for _ in range(m): a,b = map(int,input().split()) tree.union(a,b) q = int(input()) for i in range(q): a,b = map(int,input().split()) if tree.same(a,b): print("yes") else: print("no") ``` Yes
9,032
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` import sys input = sys.stdin.readline from operator import itemgetter sys.setrecursionlimit(10000000) from collections import deque def main(): n, m = map(int, input().strip().split()) nei = [[] for _ in range(n)] for _ in range(m): s, t = map(int, input().strip().split()) nei[s].append(t) nei[t].append(s) nodes = [-1] * n color = 1 for i in range(n): if nodes[i] > 0: continue d = deque() d.append(i) # print(nei) while len(d) != 0: k = d.popleft() nodes[k] = color for j in nei[k]: if nodes[j] == -1: d.append(j) # print(len(d)) color += 1 q = int(input().strip()) for _ in range(q): s, t = map(int, input().strip().split()) if nodes[s] == nodes[t]: print('yes') else: print('no') if __name__ == '__main__': main() ``` Yes
9,033
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` from collections import deque def bfs(graph, start, goal): visited = {start: None} unvisited = deque() unvisited.append(start) while unvisited: now = unvisited.popleft() if now == goal or goal in graph[now]: graph[start].add(goal) graph[goal].add(start) return "yes" for next in graph[now]: if not (next in visited): unvisited.append(next) visited[next] = now return "no" n, m = map(int, input().split()) g = [set() for i in range(n)] for i in range(m): a, b = map(int, input().split()) g[a].add(b) g[b].add(a) q = int(input()) for i in range(q): s, t = map(int, input().split()) print(bfs(g, s, t)) ``` No
9,034
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` # -*- coding: utf-8 -*- from collections import deque class LinkedList: def __init__(self, n): self.n = n self.ll = [[] for _ in range(self.n)] def link(self, u, v, direction=False): self.ll[u].append(v) if not direction: self.ll[v].append(u) def check(self, start, goal): states = [0] * self.n queue = deque([start]) while queue: u = queue.popleft() if states[u]: continue else: states[u] = 1 for v in self.ll[u]: if v == goal: print('yes') return if states[v]: continue else: queue.append(v) else: print('no') return if __name__ == '__main__': n, m = map(int, input().split()) ll = LinkedList(n) for _ in range(m): u, v = map(int, input().split()) ll.link(u, v) q = int(input()) for _ in range(q): u, v = map(int, input().split()) ll.check(u, v) ``` No
9,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` n, m = map(int, input().split()) rootList = [-1] * n def getRoot(x): if rootList[x] < 0: return x rootList[x] = getRoot(rootList[x]) return rootList[x] def setSameRoot(x, y): x = getRoot(x) y = getRoot(y) if x != y: G[x], G[y] = min(x, y), min(x, y) for i in range(0, m): s, t = map(int, input().split()) setSameRoot(s, t) q = int(input()) for i in range(0, q): s, t = map(int, input().split()) print("yes" if getRoot(s) == getRoot(t) else "no") ``` No
9,036
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network. Constraints * $2 \leq n \leq 100,000$ * $0 \leq m \leq 100,000$ * $1 \leq q \leq 10,000$ Input In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$. In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other). In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character. Output For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise. Example Input 10 9 0 1 0 2 3 4 5 7 5 6 6 7 6 8 7 8 8 9 3 0 1 5 9 1 3 Output yes yes no Submitted Solution: ``` n, m = map(int, input().split()) friend = [[] for i in range(n)] for i in range(m): s, t = map(int, input().split()) friend[s].append(t) friend[t].append(s) def are_connected(s, t): q = [s] checked = [False] * n checked[s] = True while q: user = q.pop(0) for f in friend[user]: if f == t: return True elif not checked[f]: q.append(f) checked[f] = True return False q = int(input()) for i in range(q): s, t = map(int, input().split()) if are_connected(s, t): print('yes') else: print('no') ``` No
9,037
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` n=int(input()) a=[[int(s)for s in input().split()]for _ in range(n)] def f(): for i in range(n-1): d=a[i][:];d[3],d[4]=d[4],d[3] for j in range(i+1,n): e=a[j][:];e[3],e[4]=e[4],e[3] for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]): f=[d[k]for k in p] if f[0]==e[0]and f[5]==e[5]: f=f[1:5]*2 for k in range(4): if f[k:k+4]==e[1:5]:return'No' return'Yes' print(f()) ```
9,038
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` import sys reverse = { "E":"W", "W":"E", "S":"N", "N":"S" } class dice: def __init__(self,A): self.list = sorted(A) self.max = max(A) self.min = min(A) self.side = { "TOP":A[0], "S":A[1], "E":A[2], "W":A[3], "N":A[4], "BOT":A[5], } def get_list(self): return self.list def get_dict(self): return self.side def check(self): pass def main(self,A): for s in A: var = int(self.side[s]) self.side[s] = self.side["TOP"] self.side["TOP"] = self.side[reverse[s]] self.side[reverse[s]] = self.side["BOT"] self.side["BOT"] = var def rot(self): var = self.side["N"] self.side["N"] = self.side["E"] self.side["E"] = self.side["S"] self.side["S"] = self.side["W"] self.side["W"] = var n = int(input()) data = [list(map(int,input().split())) for _ in range(n)] for i in range(0,n-1): a = dice(data[i]) a_dict = a.get_dict() for j in range(i+1,n): b = dice(data[j]) for do in ["N","N","N","N","E","EE"]: b.main(do) for _ in range(4): b.rot() b_dict = b.get_dict() if a_dict == b_dict: print("No") exit() print("Yes") ```
9,039
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` n=int(input()) a=[list(map(int,input().split()))for _ in range(n)] def f(): for i in range(n-1): d=a[i][:];d[3],d[4]=d[4],d[3] for j in range(i+1,n): e=a[j][:];e[3],e[4]=e[4],e[3] for p in('012345','152043','215304','302541','410352','514320'): f=[d[int(k)]for k in p] g=f[1:5]*2 for k in range(4): if(g[k:k+4]==e[1:5])*(f[0]==e[0])*(f[5]==e[5]):return 'No' return 'Yes' print(f()) ```
9,040
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` number = int(input()) dices = [] for i in range(number): dices.append(input().split()) tf = 0 for i in range(number - 1): for j in range(i + 1, number): dice_1 = dices[i] dice_2 = dices[j] rolls = {' ':[0, 1, 2, 3, 4, 5], 'W':[2, 1, 5, 0, 4, 3], 'N':[1, 5, 2, 3, 0, 4], 'E':[3, 1, 0, 5, 4, 2], 'S':[4, 0, 2, 3, 5, 1] } orders = [' ', 'N', 'W', 'E', 'S', 'NN'] for order in orders: copy_2 = dice_2 for d in order: roll = rolls[d] copy_2 = [copy_2[i] for i in roll] l = [1, 2, 4, 3] a0 = copy_2[0] == dice_1[0] a1 = copy_2[5] == dice_1[5] a2 = ' '.join([copy_2[s]for s in l]) in ' '.join([dice_1[s]for s in l] * 2) if a0 and a1 and a2: tf += 1 if tf > 0: print('No') else: print('Yes') ```
9,041
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` n=int(input()) a=[[s for s in input().split()]for _ in range(n)] def f(): for i in range(n-1): d=a[i][:];d[3],d[4]=d[4],d[3] for j in range(i+1,n): e=a[j][:];e[3],e[4]=e[4],e[3] for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]): f=[d[k]for k in p] if f[0]==e[0]and f[5]==e[5]: f=f[1:5]*2 for k in range(4): if f[k:k+4]==e[1:5]:return'No' return'Yes' print(f()) ```
9,042
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` def f(): for c in range(n): for d in a[c+1:]: for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]): if[d[p[0]],d[p[5]]]==a[c][::5]: f=[d[i]for i in p[1:5]]*2 for k in range(4): if f[k:k+4]==a[c][1:5]:return"No" return"Yes" n=int(input()) a=[input().split()for _ in[0]*n] for b in a:b[3:5]=b[4],b[3] print(f()) ```
9,043
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` class Dice: def __init__(self): self.u=1 self.w=2 self.s=3 self.e=4 self.n=5 self.d=6 self.dic={"W":0,"S":1,"E":2,"N":3} def __init__(self,u,w,s,e,n,d): self.u=u self.w=w self.s=s self.e=e self.n=n self.d=d self.dic={"W":0,"S":1,"E":2,"N":3} def rot(self,way): if isinstance(way,str): way=self.dic[way] if(way==0): c=self.u self.u=self.e self.e=self.d self.d=self.w self.w=c elif way==1: c=self.u self.u=self.n self.n=self.d self.d=self.s self.s=c elif way==2: c=self.u self.u=self.w self.w=self.d self.d=self.e self.e=c def get_nums(self): return {self.u,self.w,self.s,self.e,self.n,self.w,self.d} def mk_dice(): u,s,e,w,n,d=map(int,input().split()) return Dice(u,w,s,e,n,d) import random q=int(input()) dice_col=[] ans=True for j in range(q): dice_b=mk_dice() for dice in dice_col: if(dice.get_nums()==dice_b.get_nums()): for i in range (1000): dice.rot(random.randint(0,2)) if(dice.u==dice_b.u and dice.d==dice_b.d and dice.w==dice_b.w and dice.s==dice_b.s and dice.e==dice_b.e and dice.n==dice_b.n): ans=False if ~ans: break dice_col.append(dice_b) if ans: print('Yes') else: print('No') ```
9,044
Provide a correct Python 3 solution for this coding contest problem. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes "Correct Solution: ``` import random class Dice(object): def __init__(self): self.t = 1 self.s = 2 self.e = 3 self.w = 4 self.n = 5 self.b = 6 def __init__(self, faces): self.t = faces[0] self.s = faces[1] self.e = faces[2] self.w = faces[3] self.n = faces[4] self.b = faces[5] def roll(self, direct): if direct == 0: self.t, self.s, self.b, self.n = self.n, self.t, self.s, self.b elif direct == 1: self.t, self.w, self.b, self.e = self.e, self.t, self.w, self.b elif direct == 2: self.n, self.w, self.s, self.e = self.w, self.s, self.e, self.n def is_equal(self, dice): if self.t == dice.t and self.s == dice.s and self.e == dice.e and self.w == dice.w and self.n == dice.n and self.b == dice.b: return True else: return False def get_nums(self): return {self.t, self.s, self.e, self.w, self.n, self.b} n = int(input()) dice_list = [] flag_list = [] for _ in range(n): flag = False faces = list(map(int, input().split())) dice_list.append(Dice(faces)) if n == 0: continue dice_a = dice_list[-1] for i in range(len(dice_list) - 1): dice_b = dice_list[i] if (dice_a.get_nums() == dice_b.get_nums()): for i in range(1000): dice_b.roll(random.randint(0, 3)) if dice_a.is_equal(dice_b): flag = True break flag_list.append(flag) if any(flag_list): print('No') else: print('Yes') ```
9,045
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` class dice: def __init__(self, X): self.x = [0] * 6 self.x[0] = X[0] self.x[1] = X[1] self.x[2] = X[2] self.x[3] = X[3] self.x[4] = X[4] self.x[5] = X[5] def roll(self, d): if d == 'S': self.x[0], self.x[1], self.x[4], self.x[5] = \ self.x[4], self.x[0], self.x[5], self.x[1] elif d == 'E': self.x[0], self.x[2], self.x[3], self.x[5] = \ self.x[3], self.x[0], self.x[5], self.x[2] elif d == 'W': self.x[0], self.x[2], self.x[3], self.x[5] = \ self.x[2], self.x[5], self.x[0], self.x[3] elif d == 'N': self.x[0], self.x[1], self.x[4], self.x[5] = \ self.x[1], self.x[5], self.x[0], self.x[4] n = int(input()) X = list(map(int, input().split())) dice_b = dice(X) distances = list('NNNNWNNNWNNNENNNENNNWNNN') b = False for _ in range(n-1): X_t = list(map(int, input().split())) dice_t = dice(X_t) for d in distances: dice_t.roll(d) if dice_b.x[0] == dice_t.x[0] and dice_b.x[1] == dice_t.x[1] and dice_b.x[2] == dice_t.x[2] and \ dice_b.x[3] == dice_t.x[3] and dice_b.x[4] == dice_t.x[4] and dice_b.x[5] == dice_t.x[5]: b = True break if b == False: print('Yes') elif b == True: print('No') ``` Yes
9,046
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` import sys def rotate(arr,i): if i=='W': arr=[arr[k-1] for k in [3,2,6,1,5,4]] if i=='E': arr=[arr[k-1] for k in [4,2,1,6,5,3]] if i=='N': arr=[arr[k-1] for k in [2,6,3,4,1,5]] if i=='S': arr=[arr[k-1] for k in [5,1,3,4,6,2]] return arr def isIdentical(arr,arr2): deck=[] for i in 'W'*4: arr=rotate(arr,i) for k in 'NNNN': arr=rotate(arr,k) deck.append(arr) arr=rotate(arr,'N') arr=rotate(arr,'W') for k in 'NNNN': arr=rotate(arr,k) deck.append(arr) arr=rotate(arr,'W') arr=rotate(arr,'W') for k in 'NNNN': arr=rotate(arr,k) deck.append(arr) if arr2 in deck: return True else: return False n=int(input()) allarr=[] for i in range(n): allarr.append(input().split()) for i1,v1 in enumerate(allarr): for v2 in allarr[i1+1:]: if isIdentical(v1,v2): print('No') sys.exit() print('Yes') ``` Yes
9,047
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` from itertools import* n=int(input()) a=[[s for s in input().split()]for _ in range(n)] for k in range(n): b=a[k];b[3],b[4]=b[4],b[3] def f(): for d,e in combinations(a,2): for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]): f=[d[k]for k in p] if f[0]==e[0]and f[5]==e[5]: f=f[1:5]*2 for k in range(4): if f[k:k+4]==e[1:5]:return'No' return'Yes' print(f()) ``` Yes
9,048
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` class dice: def __init__(self,data): self.faces = data def turn(self,direction): if direction == 'N': idx = [1,5,2,3,0,4] self.faces = [self.faces[i] for i in idx] elif direction == 'S': [self.turn('N') for i in range(3)] elif direction == 'E': idx = [3,1,0,5,4,2] self.faces = [self.faces[i] for i in idx] elif direction == 'W': [self.turn('E') for i in range(3)] elif direction == 'c': self.turn('N') self.turn('W') self.turn('S') def isunique(d1,d2): for o in 'cccWcccEcccNcccWcccWccc': if d1.faces == d2.faces: break else: d2.turn(o) if d1.faces == d2.faces: return False else: return True if __name__ == '__main__': from itertools import combinations n = int(input()) dices = [] for i in range(n): dices.append(dice([int(s) for s in input().split()])) ans = True check = [isunique(d1,d2) for d1,d2 in combinations(dices,2)] if all(check): print('Yes') else: print('No') ``` Yes
9,049
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- """ ???????????? 4 ?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°???????????????????????????????????? +--+ ???|???| +--+--+--+--+ |???|???|???|???| +--+--+--+--+ ???|???| +--+ """ class Dice: T = 0 S = 1 E = 2 W = 3 N = 4 B = 5 """ ????????????????????? """ def __init__(self): """ ????????????????????? """ self.side = [1, 2, 3, 4, 5, 6] self.top = 0 def setDice(self, arr): """ ??¢?????????????????? """ self.side = arr[0:6] def Turn(self, dir): """ ?????????????????¢?????? """ s = self.side if dir == "N": # ????????¢?????? # Top 0 South 1 East 2 West 3 North 4 Bottom 5 t = [s[Dice.S],s[Dice.B],s[Dice.E],s[Dice.W],s[Dice.T],s[Dice.N]] elif dir == "S": # ????????¢?????? t = [s[Dice.N],s[Dice.T],s[Dice.E],s[Dice.W],s[Dice.B],s[Dice.S]] elif dir == "E": # ??±?????¢?????? t = [s[Dice.W],s[Dice.S],s[Dice.T],s[Dice.B],s[Dice.N],s[Dice.E]] elif dir == "W": # ?\??????¢?????? t = [s[Dice.E],s[Dice.S],s[Dice.B],s[Dice.T],s[Dice.N],s[Dice.W]] elif dir == "L": # ???????????¢ t = [s[Dice.T],s[Dice.W],s[Dice.S],s[Dice.N],s[Dice.E],s[Dice.B]] elif dir == "R": # ???????????¢ t = [s[Dice.T],s[Dice.E],s[Dice.N],s[Dice.S],s[Dice.W],s[Dice.B]] self.side = t # print("{0} T0:{1} S1:{2} W2:{3} E3:{4} N4:{5} B5:{6} ".format(dir, self.side[0],self.side[1], self.side[2],self.side[3], self.side[4],self.side[5])) def Search(self, tval, sval, dir): """ ?????¢??¨?????¢???????????????????????????????????¢??????????????? """ # print("{0} T0:{1} S1:{2} W2:{3} E3:{4} N4:{5} B5:{6} ".format(dir, self.side[0],self.side[1], self.side[2],self.side[3], self.side[4],self.side[5])) if self.side[Dice.E] == tval: self.Turn("W") elif self.side[Dice.W] == tval: self.Turn("E") elif self.side[Dice.N] == tval: self.Turn("S") elif self.side[Dice.S] == tval: self.Turn("N") elif self.side[Dice.B] == tval: self.Turn("N") self.Turn("N") for i in range(3): # ?????¢?????????????????????????????§???????????? if self.side[Dice.S] == sval: break self.Turn("L") return self.side[dir] # ????????? d = [] num = int(input().strip()) for i in range(num): # ?????????????????? tmp = Dice() tmp.setDice(list(map(int,input().strip().split()))) d.append(tmp) ret = "Yes" for i in range(num-1): for j in range(i + 1,num): v1 = d[i].Search(d[i].side[Dice.T], d[i].side[Dice.S], Dice.E) v2 = d[j].Search(d[i].side[Dice.T], d[i].side[Dice.S], Dice.E) # print("{0}/{1} V1={2} V2={3}".format(i,j,v1,v2)) if v1 == v2: ret = "No" break if ret == "No": break print(ret) ``` No
9,050
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` # Dice I - IV class Dice: def __init__(self, a1, a2, a3, a4, a5, a6): # サイコロを縦横にたどると書いてある数字(index1は真上、index3は真下の数字) self.face = [a1, a2, a3, a4, a5, a6] self.v = [a5, a1, a2, a6] # 縦方向 self.h = [a4, a1, a3, a6] # 横方向 self.det = 1 # print(self.v, self.h) # サイコロの上面の数字を表示 def top(self): return self.v[1] # サイコロを北方向に倒す def north(self): newV = [self.v[1], self.v[2], self.v[3], self.v[0]] self.v = newV self.h[1] = self.v[1] self.h[3] = self.v[3] return self.v, self.h # サイコロを南方向に倒す def south(self): newV = [self.v[3], self.v[0], self.v[1], self.v[2]] self.v = newV self.h[1] = self.v[1] self.h[3] = self.v[3] return self.v, self.h # サイコロを東方向に倒す def east(self): newH = [self.h[3], self.h[0], self.h[1], self.h[2]] self.h = newH self.v[1] = self.h[1] self.v[3] = self.h[3] return self.v, self.h # サイコロを西方向に倒す def west(self): newH = [self.h[1], self.h[2], self.h[3], self.h[0]] self.h = newH self.v[1] = self.h[1] self.v[3] = self.h[3] return self.v, self.h def searchFace(self, a): b = 0 for i in range(6): if a == self.face[i]: # print('一致しました') b = i + 1 return b def detJudge(self, x): # a は 1 から 6 のどれか y = int(7 / 2 - abs(x - 7 / 2)) if x != y: self.det *= -1 # print(self.det) return y def rightSide(self, top, front): r = 0 if top == 1 and front == 2: r = 3 elif top == 2 and front == 3: r = 1 elif top == 3 and front == 1: r = 2 elif top == 1 and front == 3: r = 5 elif top == 3 and front == 2: r = 6 elif top == 2 and front == 1: r = 4 if self.det == -1: r = 7 - r return r diceAmount = int(input()) dices = [] for i in range(diceAmount): d = [int(j) for j in input().rstrip().split()] dice = Dice(d[0], d[1], d[2], d[3], d[4], d[5]) dices.append(dice) # print(dices) # Dice I # command = list(input().rstrip()) # print(command) # Dice II # for i, a in enumerate(command): # if a == 'N': # dice1.north() # elif a == 'S': # dice1.south() # elif a == 'E': # dice1.east() # elif a == 'W': # dice1.west() # print(dice1.top()) # questionAmount = int(input()) # for i in range(questionAmount): # # Initialize det # dice1.det = 1 # question = [int(i) for i in input().rstrip().split()] # a = dice1.searchFace(question[0]) # b = dice1.searchFace(question[1]) # # print(a, b) # top = dice1.detJudge(a) # front = dice1.detJudge(b) # # print(top, front) # position = dice1.rightSide(top, front) # answer = dice1.face[position - 1] # print(answer) # Dice III # import random # # print(dice1.face) # # print(dice2.face) # yesCount = 0 # i = 0 # while yesCount == 0 and i < 1000: # j = random.randint(0, 3) # if j == 0: # dice2.north() # elif j == 1: # dice2.south() # elif j == 2: # dice2.east() # elif j == 3: # dice2.west() # if (dice1.v == dice2.v and dice1.h == dice2.h) or ([dice1.v[2], dice1.v[1], dice1.v[0], dice1.v[3]] == [dice2.v[2], dice2.v[1], dice2.v[0], dice2.v[3]] and [dice1.h[2], dice1.h[1], dice1.h[0], dice1.h[3]] == [dice2.h[2], dice2.h[1], dice2.h[0], dice2.h[3]]): # yesCount += 1 # # print('一致') # i += 1 # if yesCount >= 1: # print('Yes') # else: # print('No') # Dice IV import random match = 0 diceCount = 1 while match == 0 and diceCount < diceAmount: for d2 in range(1, diceAmount): # print(d2) i = 0 while match == 0 and i < 27: j = random.randint(0, 3) if j == 0: dices[d2].north() elif j == 1: dices[d2].south() elif j == 2: dices[d2].east() elif j == 3: dices[d2].west() d1 = 0 while d1 < d2 and match == 0: if dices[d1].v == dices[d2].v and dices[d1].h == dices[d2].h: match += 1 # print('一致') d1 += 1 i += 1 diceCount += 1 if match >= 1: print('No') else: print('Yes') ``` No
9,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` import sys import copy n = int(input()) inp = [[i for i in input().split()] for i in range(n)] a = copy.copy(inp) number = 3 count = 0 kais = n-1 for i in range(n-3): number += 3+i for i in range(number): if i == kais-1: for i3 in range(n-1): if i3 == 0: sika = kais continue kais += n-i3 if i >= sika and i < kais: sika = kais count = i3 for i2 in range(1,33): if (i2<=20 and i2%5==0) or i2==22 or i2==27 or i2==28: s = "N" else: s = "E" if s == "E": for i4 in range(len(inp)): if i4 == count: continue b = [] b.append(a[i4][3]) b.append(a[i4][1]) b.append(a[i4][0]) b.append(a[i4][5]) b.append(a[i4][4]) b.append(a[i4][2]) a[i4][:] = b[:] for x in range(len(inp)): if x == count: continue else: if inp[count][:] == a[x][:]: print("No") sys.exit() elif s == "N": for i4 in range(len(inp)): if i4 == count: continue b = [] b.append(a[i4][1]) b.append(a[i4][5]) b.append(a[i4][2]) b.append(a[i4][3]) b.append(a[i4][0]) b.append(a[i4][4]) a[i4][:] = b[:] for x in range(len(inp)): if x == count: continue else: if inp[count][:] == a[x][:]: print("No") sys.exit() print("Yes") ``` No
9,052
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III. Constraints * $2 \leq n \leq 100$ * $0 \leq $ the integer assigned to a face $ \leq 100$ Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III. Output Print "Yes" if given dices are all different, otherwise "No" in a line. Examples Input 3 1 2 3 4 5 6 6 2 4 3 5 1 6 5 4 3 2 1 Output No Input 3 1 2 3 4 5 6 6 5 4 3 2 1 5 4 3 2 1 6 Output Yes Submitted Solution: ``` import sys IDX_TBL = {1:[2,3,5,4], 2:[3,1,4,6], 3:[1,2,6,5], 4:[1,5,6,2], 5:[1,3,6,4], 6:[2,4,5,3]} OPPSITE_TBL = {1:6, 2:5, 3:4, 4:3, 5:2, 6:1} def is_same(l1, l2): if l1[1] in l2: idx_list = [i for i,x in enumerate(l2) if x==l1[1]] for idx in idx_list: nstr1 = "".join([l1[i] for i in IDX_TBL[1]]) nstr2 = "".join([l2[i] for i in IDX_TBL[idx]]) if nstr2 in nstr1*2: if l1[6] == l2[7-l2.index(l1[1])]: return True return False n=input() nums_list = [] for _ in range(n): nums_list.append(["_"] + raw_input().split(" ")) for i in range(0, n): for j in range(i+1, n): if is_same(nums_list[i], nums_list[j]): print "No" sys.exit(0) print "Yes" ``` No
9,053
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` import math from collections import defaultdict from sys import stdin, stdout input = stdin.readline def main(): n = int(input()) s = input() t = input() res = 0 for i in range(n//2): d = defaultdict(int) for val in (s[i], s[n-i-1], t[i], t[n-i-1]): d[val] += 1 x = sorted(d.values()) if x == [1, 3]: res += 1 elif x == [1, 1, 1, 1]: res += 2 elif x == [1, 1, 2]: if s[i] == s[n-i-1]: res += 2 else: res += 1 if n % 2 == 1 and s[n//2] != t[n//2]: res += 1 print(res) if __name__ == '__main__': main() ```
9,054
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` n=int(input()) a,b=input(),input() k=n//2 c=a[k]!=b[k]and n%2 for u,v,x,y in zip(a[:k],a[::-1],b,b[::-1]):c+=(len({x,y}-{u,v}),u!=v)[x==y] print(c) ```
9,055
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` from math import ceil n = int(input()) word1 = input() word2 = input() combined = [] for i in range(ceil(n / 2)): if i > n / 2 - 1: combined.append([word1[i], word2[i]]) else: combined.append([word1[i], word1[- i - 1], word2[i], word2[- i - 1]]) count = 0 for l in combined: s = set(l) if len(s) == 4: count += 2 elif len(s) == 3: count += 1 if l[0] == l[1]: count += 1 elif len(s) == 2: counter = 0 first_letter = l[0] for letter in l: if letter == first_letter: counter += 1 if counter != 2: count += 1 print(count) ```
9,056
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` n = int(input()) a = input() b = input() ans = 0 for i in range(len(a)//2): ans += 2 flag = False c = [a[i], b[i], a[n-i-1], b[n-i-1]] if c[0] == c[2] and c[1] != c[3] and c[1] != c[0] and c[3] != c[0]: continue for j in range(3): for k in range(j+1, 4): if c[j] == c[k]: ans -= 1 c.pop(k) c.pop(j) flag = True break if flag: break if len(c) == 2 and c[0] == c[1]: ans -= 1 if len(a) % 2 == 1 and a[n//2] != b[n//2]: ans += 1 print(ans) ```
9,057
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` n = int(input()) s1 = list(input()) s2 = list(input()) cnt = 0 for i in range(n): if s2[i] == s2[n-i-1]: s2[i], s1[n-i-1] = s1[n-i-1], s2[i] if s1[i] == s2[n-i-1]: s2[n-i-1], s2[i] = s2[i],s2[n-i-1] for i in range(n): if s1[i] != s2[i]: cnt += 1 print(cnt) ```
9,058
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` from collections import defaultdict n = int(input()) a = input() b = input() ans = 0 for i in range(n//2): tmpdict = defaultdict(int) tmplist = [a[i], b[i], a[n-1-i], b[n-1-i]] for x in tmplist: tmpdict[x] += 1 if len(tmpdict) == 4: ans += 2 elif len(tmpdict) == 3: ans += 1 + (a[i] == a[n-i-1]) elif len(tmpdict) == 2: ans += tmpdict[a[i]] != 2 if n % 2 == 1 and a[n//2] != b[n//2]: ans += 1 print(ans) ```
9,059
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` n=int(input()) a=input() cnt=0 b=input() if True: if n%2==0: maxx=n//2 else: maxx=n//2+1 for i in range(maxx): if i==n-i-1: if a[i]!=b[i]: cnt+=1 continue p1=a[i] p2=a[n-i-1] q1=b[i] q2=b[n-i-1] if p1==p2: if q1==q2: pass else: if p1==q1 or p1==q2: cnt+=1 else: cnt+=2 else: if q1==q2: cnt+=1 else: if (p1==q1 and p2==q2) or (p1==q2 and p2==q1): cnt+=0 elif (p1==q1 or p2==q2) or (p1==q2 or p2==q1): cnt+=1 else: cnt+=2 print(cnt) ```
9,060
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Tags: implementation Correct Solution: ``` n = int(input()) a = input() b = input() result = 0 if n%2==1 and a[n//2]!=b[n//2]: result = 1 for i in range(n//2): s = a[i] + a[n-i-1] + b[i] + b[n-i-1] diff = 0 for j in set(s): count = s.count(j) if count == 4: diff = 0 break; elif count == 3: diff = 1 break elif count == 2: if len(set(s))==2: diff = 0 break else: if s[0]==s[1]: diff = 2 else: diff = 1 elif len(set(s))==4: diff = 2 break result+= diff print(result) ```
9,061
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n=int(input()) a=input() b=input() count=0 for i in range(n//2): d={} for c in [a[i],a[n-i-1],b[i],b[n-i-1]]: d[c]=d.get(c,0)+1 if a[i]==a[n-i-1]==b[i]==b[n-i-1]: continue count+=2 if a[i]==a[n-i-1] and b[i]!=b[n-i-1] and a[i]!=b[i] and a[i]!=b[n-i-1]: continue for v in d.values(): if v>=2:count-=1 if n%2==1 and a[n//2]!=b[n//2]: count+=1 print(count) ``` Yes
9,062
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n = int(input()) a = [i for i in input()] b = [i for i in input()] ans = 0 for i in range(n//2): s_a = {a[i]} s_a |= {a[n-1-i]} s_b = {b[i]} s_b |= {b[n-1-i]} s_u = s_a | s_b cnt = len(s_u) if cnt == 2 and len(s_a) != len(s_b): ans += 1 elif cnt == 3: if len(s_a) == 2: ans += 1 elif len(s_b) == 2: ans += 2 else: ans += 2 elif cnt == 4: ans += 2 if n % 2 == 1: if a[n//2] != b[n//2]: ans += 1 print(ans) ``` Yes
9,063
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n=int(input()) a=input() b=input() x=[] y=[] p=0 for j in range(n): x.append(a[j]) y.append(b[j]) j=0 while(j<=(n//2-1)): e=[x[j],x[n-1-j]] f=[y[j],y[n-1-j]] if len(set(e))==1 and len(set(f))==1: pass elif len(set(f))==1: p+=1 else: if x[j] in f: k = f.index(x[j]) del f[k] pass else: p += 1 if x[n - j - 1] in f: pass else: p += 1 j+=1 if n%2!=0: if x[n//2]==y[n//2]: pass else: p+=1 print(p) ``` Yes
9,064
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` # import collections from functools import cmp_to_key #key=cmp_to_key(lambda x,y: 1 if x not in y else -1 ) import math import sys def getIntList(): return list(map(int, input().split())) import bisect try : import numpy dprint = print dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass def makePair(z): return [(z[i], z[i+1]) for i in range(0,len(z),2) ] def memo(func): cache={} def wrap(*args): if args not in cache: cache[args]=func(*args) return cache[args] return wrap @memo def comb (n,k): if k==0: return 1 if n==k: return 1 return comb(n-1,k-1) + comb(n-1,k) N, = getIntList() s1 = input() s2 = input() t = N//2 res = 0 for i in range(t): j = N-1 -i z = [s1[i], s1[j],s2[i],s2[j]] z.sort() if z[0] ==z[1] and z[2] == z[3]: continue if s2[i] == s2[j] or s1[i] == s2[i] or s1[i] == s2[j] or s1[j] == s2[i] or s1[j] ==s2[j]: res +=1 continue res += 2 if N %2 ==1: if s1[t] != s2[t]: res+=1 print(res) ``` Yes
9,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n=int(input()) a=input() b=input() ans=0 for i in range((n-1)//2+1): if n%2==1 and i==(n-1)//2: if a[i]!=b[i]: ans+=1 else: cost1=0 if a[i]!=b[i]: cost1+=1 if a[n-i-1]!=b[n-i-1]: cost1+=1 cost2=0 if a[i]!=b[n-i-1]: cost2+=1 if a[n-i-1]!=b[i]: cost2+=1 cost3=0 if a[n-i-1]!=b[i]: cost3+=1 if a[i]!=b[n-i-1]: cost3+=1 ans+=min(cost1,cost2,cost3) print(ans) ``` No
9,066
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` #aa # 0: aa, bb # 1: ab, ba # 2: cd #ab # 0: ab, ba # 1: aa, bb, ac, ca, bc, cb # 2: cd n = int(input()) a = input() b = input() m=0 def swap(arr): val = arr[0] arr[0] = arr[1] arr[1] = val aa = [0,0] bb = [0,0] for i in range(n//2): aa[0] = a[i] aa[1] = a[-1-i] bb[0] = b[i] bb[1] = b[-1-i] if aa[0] == aa[1] and bb[0] == bb[1]: continue if bb[0] == aa[1]: swap(bb) for j in range(2): if bb[0] != aa[0]: m += 1 print(m) #if a1 == a2: # if b1 != b2: # if b1 != a1: # m+=1 # if b2 != a1: # m+=1 #else: # if b1 == a2: ``` No
9,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n=int(input()) s=input() v=input() sum=0 for i in range(n//2): if (s[i]==v[i]) and (s[n-1-i]==v[n-1-i]) or (v[i]==v[n-1-i]) and s[i]==s[n-i-1] or v[i]==s[n-1-i] and s[i]==v[n-1-i]: sum=sum elif s[i]==v[i] or s[i]==v[n-1-i] or v[i]==s[n-1-i] or v[n-1-i]==s[n-1-i]: sum=sum+1 else: sum=sum+2 if n%2==1: if s[(n-1)//2]!=v[(n-1)//2]: sum=sum+1 print(sum) ``` No
9,068
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive. You are allowed to do the following changes: * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and b_i; * Choose any index i (1 ≤ i ≤ n) and swap characters a_i and a_{n - i + 1}; * Choose any index i (1 ≤ i ≤ n) and swap characters b_i and b_{n - i + 1}. Note that if n is odd, you are formally allowed to swap a_{⌈n/2⌉} with a_{⌈n/2⌉} (and the same with the string b) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in a with another character. In other words, in a single preprocess move you can choose any index i (1 ≤ i ≤ n), any character c and set a_i := c. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings a and b equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string b or make any preprocess moves after the first change is made. Input The first line of the input contains one integer n (1 ≤ n ≤ 10^5) — the length of strings a and b. The second line contains the string a consisting of exactly n lowercase English letters. The third line contains the string b consisting of exactly n lowercase English letters. Output Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string a equal to string b with a sequence of changes from the list above. Examples Input 7 abacaba bacabaa Output 4 Input 5 zcabd dbacz Output 0 Note In the first example preprocess moves are as follows: a_1 := 'b', a_3 := 'c', a_4 := 'a' and a_5:='b'. Afterwards, a = "bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a_2, b_2) and swap(a_2, a_6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4. In the second example no preprocess moves are required. We can use the following sequence of changes to make a and b equal: swap(b_1, b_5), swap(a_2, a_4). Submitted Solution: ``` n= int(input()) a = list(input()) b = list(input()) check = [] i = 0 j = n-1 res = 0 ugh = 0 while i <= j: if i != j: check.append(a[i]) check.append(a[j]) check.append(b[i]) check.append(b[j]) length = len(set(check)) if length == 4: res += 2 elif length == 3: if check[0] == check[1]: res += 2 else: res += 1 elif length == 2: for k in range(4): if check[k] == a[i]: ugh += 1 if ugh == 1 or ugh == 3: res += 1 check = [] i += 1 j -= 1 elif i == j: if a[i] != b[i]: res += 1 check = [] i += 1 j -= 1 print(res) ``` No
9,069
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` s=input() d={} k=int(input()) for x in s: if x in d: d[x] += 1 else: d[x] = 1 while len(d): x = min(d,key=d.get) if k>=d[x]: k -= d[x] del d[x] else: break ans = ''.join([x for x in s if x in d]) print(len(d),ans,sep='\n') ```
9,070
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` dic = dict({}) n, k = input(), int(input()) for i in n: if i not in dic: dic[i] = 0 dic[i] += 1 dic_sort = sorted(dic.items(), key=lambda x: x[1]) for i, j in dic_sort: if (j <= k): n = n.replace(i, '') k -= j dic.pop(i) else: break print(len(dic), n, sep='\n') ```
9,071
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` I=input s=I() C={} for x in set(s):C[x]=s.count(x) k=int(I()) t=sorted(set(s),key=lambda x:C[x]) while t and C[t[0]]<=k: k-=C[t[0]];s=s.replace(t[0],'') t=t[1:] print(len(set(s))) print(s) # Made By Mostafa_Khaled ```
9,072
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` s=input() k=int(input()) a=[] for x in set(s): a.append([s.count(x),x]) a.sort() for z in a: if z[0]>k:break k-=z[0] s=s.replace(z[1],'') print(len(set(s))) print(s) ```
9,073
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` s = input() n = int(input()) mp = {} for c in s: mp.setdefault(c, 0) mp[c]+=1 arr = [] for k,v in mp.items(): arr.append((k,v)) arr.sort(key = lambda x: x[1]) curr = 0 for i in range(len(arr)): mp[arr[i][0]] -= min(n, arr[i][1]) n -= arr[i][1] if n < 0: print(len(arr)-i) elif n == 0: print(len(arr)-i-1) if n <= 0: break if n > 0: print(0) for c in s: mp[c] -= 1 if mp[c] >= 0: print(c,end="") ```
9,074
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() 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): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) 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) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() # for _ in range(int(ri())): s = ri() k = int(ri()) se = set() dic = {} for i in s: dic[i] = dic.get(i, 0)+1 lis = [(i,dic[i]) for i in dic] lis.sort(key = lambda x : (x[1])) tot = 0 for i in range(len(lis)): if lis[i][1] <= k: k-=lis[i][1] se.add(lis[i][0]) tot+=1 else: # tot = len(lis) - i break ans = [] for i in range(len(s)): if s[i] not in se: ans.append(s[i]) print(len(dic) - len(se)) print("".join(ans)) ```
9,075
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` s=input() ar=sorted([[s.count(c),c] for c in set(s)]) res=len(ar) lef=int(input()) dl=set() for e in ar: if(e[0]<=lef): lef-=e[0] dl.add(e[1]) res-=1 print(res) print(''.join([x for x in s if x not in dl])) ```
9,076
Provide tags and a correct Python 3 solution for this coding contest problem. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Tags: greedy Correct Solution: ``` s=input() n=int(input()) d={} for i in s: d[i]=0 for i in s: d[i]+=1 d={k: v for k, v in sorted(d.items(), key=lambda item: item[1])} for i in d: if(n>=d[i]): n-=d[i] d[i]=0 else: d[i]-=n break if(n==0): break; x=0 for i in d: if(d[i]!=0): x+=1 print(x) for i in range(len(s)): if(d[s[i]]): print(s[i],end='') d[s[i]]-=1 ```
9,077
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` string = input() n = int(input()) alphabet = {} a = set(string) bool_string = {i: True for i in a} for i in a: alphabet[i] = 0 for i in string: alphabet[i] += 1 array = [[alphabet[i], i] for i in alphabet] array.sort() k = len(array) i = 0 while n > 0 and i < k: n -= array[i][0] bool_string[array[i][1]] = False i += 1 if n < 0: bool_string[array[i-1][1]] = True i -= 1 answer = '' print(k-i) for i in string: if bool_string[i]: answer += i print(answer) ``` Yes
9,078
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` s = input() k = int(input()) l = {} distinct = 0 for i in range(len(s)): if ord(s[i])-97 not in l: distinct += 1; l[ord(s[i])-97] = 0 l[ord(s[i])-97] += 1 d = sorted([[l[i],i] for i in l.keys()]) for i in range(distinct): if k>0: k -= d[i][0] if k >= 0: distinct -= 1; d[i][0] = 0; l[d[i][1]] = 0 else:break else:break print(max(distinct,0)) if distinct > 0: for i in range(len(s)): if l[ord(s[i])-97]: print(s[i],end="") else: print() ``` Yes
9,079
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` from collections import deque from typing import Counter def solution(word, k): counter = Counter(word) i, count = 0, 0 least_common = counter.most_common()[::-1] if k >= len(word): return "", 0 for i, (_, n) in enumerate(least_common): count += n if count > k: break result_letters = set() result_count = 0 for char, n in least_common[i:]: result_letters.add(char) result_count += 1 return "".join([c for c in word if c in result_letters]), result_count if __name__ == "__main__": word = input() k = int(input()) result, result_count = solution(word, k) print(result_count) print(result) ``` Yes
9,080
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` from collections import Counter import operator import random a=list(input()) k=int(input()) omap=(Counter(a)) x=sorted(omap.items(), key = operator.itemgetter(1)) i=0 while(k>0 and i<len(x)): if(x[i][1]<=k): k=k-x[i][1] del omap[x[i][0]] else: omap[x[i][0]]=omap[x[i][0]]-k k=0 i+=1 print(len(omap)) ans="" for i in a: if i in omap: ans+=i omap[i]-=1 if omap[i]==0: del omap[i] #print(len(omap)) print(ans) ``` Yes
9,081
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` s=input() n=int(input()) d={} for i in s: d[i]=0 for i in s: d[i]+=1 d={k: v for k, v in sorted(d.items(), key=lambda item: item[1])} for i in d: if(n>=d[i]): n-=d[i] d[i]=0 else: d[i]-=n break if(n==0): break print(len(d)) for i in d: for j in range(d[i]): print(i,end='') ``` No
9,082
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` from collections import deque from typing import Counter def solution(word, k): counter = Counter(word) i, count = 0, 0 least_common = counter.most_common()[::-1] if k > len(word): return "", 0 for i, (_, n) in enumerate(least_common): count += n if count > k: break result_letters = set() result_count = 0 for char, n in least_common[i:]: result_letters.add(char) result_count += 1 return "".join([c for c in word if c in result_letters]), result_count if __name__ == "__main__": word = input() k = int(input()) result, result_count = solution(word, k) print(result_count) print(result) ``` No
9,083
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` s=input() n=int(input()) d={} for i in s: d[i]=0 for i in s: d[i]+=1 d={k: v for k, v in sorted(d.items(), key=lambda item: item[1])} for i in d: if(n>=d[i]): n-=d[i] d[i]=0 else: d[i]-=n break if(n==0): break; for i in d: for j in range(d[i]): print(i,end='') ``` No
9,084
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105). Output Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them. Examples Input aaaaa 4 Output 1 aaaaa Input abacaba 4 Output 1 aaaa Input abcdefgh 10 Output 0 Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string. Submitted Solution: ``` from collections import Counter import operator st=input() k=int(input()) omap=Counter(st) a=sorted(omap.items(),key=operator.itemgetter(1)) i=0 while k>0 and i<len(a): if a[i][1]<=k: k-=a[i][1] del omap[a[i][0]] else: omap[a[i][0]]-=k k=0 i+=1 ans="" for key,val in omap.items(): ans+=key*val print(len(omap)) print(ans) ``` No
9,085
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` from collections import Counter n=int(input()) a=[int(o) for o in input().split()] d=dict(Counter(a)) j=0 reserve=[] resul=[-1]*n for i in range(n): if d[a[i]]==1: if j%2==0: resul[i]="A" else: resul[i]="B" j+=1 elif d[a[i]]>2: resul[i]="A" reserve.append(i) else: resul[i]="A" if j%2==0: print("YES") print("".join(resul)) else: if reserve: resul[reserve[0]]="B" print("YES") print("".join(resul)) else: print("NO") ```
9,086
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` from collections import Counter if __name__ == '__main__': n = int(input()) sarr = list(map(int, input().split())) dct = Counter(sarr) if dct.most_common(1)[0][1] == 1: if n % 2: print('NO') else: print('YES') print(''.join(['A' if i % 2 else 'B' for i in range(n)])) exit(0) occ = list(filter(lambda o: o[1] == 1, dct.most_common())) if not occ: print('YES') print(''.join(['A' for i in range(n)])) exit(0) hn = None ndct = {} if len(occ) % 2: cmn = dct.most_common(1)[0] if cmn[1] < 3: print('NO') exit(0) hn = cmn[0] for i, o in enumerate(occ): if i % 2: ndct[o[0]] = 'B' rez = [None for _ in range(n)] for i, s in enumerate(sarr): if s in ndct: rez[i] = 'B' else: rez[i] = 'A' if hn: rez[sarr.index(hn)] = 'B' print('YES') print(''.join(rez)) ```
9,087
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` from collections import Counter n = int(input()) s = list(map(int, input().split())) cn = Counter(s) ones = set() triple = set() for key in cn.keys(): if cn[key] == 1: ones.add(key) if cn[key] >= 3: triple.add(key) l_1 = len(ones) l_3 = len(triple) if (l_1 % 2 != 0) and (l_3 == 0): print('NO') else: print('YES') st = '' for x in s: if (x in ones) and len(ones) > (l_1 // 2 + l_1 % 2): st += 'B' ones.remove(x) elif (l_1 % 2 != 0) and (x in triple): st += 'B' triple.clear() else: st += 'A' print(st) ```
9,088
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` n=int(input()) l=list(map(int,input().split())) from collections import Counter,defaultdict c=Counter(l) l1=[] for i in c: if c[i]==1: l1.append(i) a=0 d=defaultdict(int) for i in l1: if a==0: d[i]="A" a=1 else: d[i]="B" a=0 if len(l1)%2==0: ans='' for i in l: if c[i]>1: d[i]="A" for i in l: ans+=d[i] print("YES") print(ans) else: s='' ans='' for i in l: if c[i]>2: s=i break if s!='': x=0 for i in l: if c[i]>1: if i==s and x==0: ans+="B" x+=1 else: ans+="A" else: ans+=d[i] print("YES") print(ans) else: print("NO") ```
9,089
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` n=int(input()) s=list(map(int,input().split())) num1=0 b=['A']*n cnt={} fst={} for i in range(len(s)): if(s[i]) in cnt: cnt[s[i]]+=1 else: cnt[s[i]]=1 fst[s[i]]=i for i in range(len(s)): if(cnt[s[i]]==1): if(num1%2==1): b[i]='B'; num1+=1 if(num1%2==1): res=False for i in range(len(s)): if(cnt[s[i]]>2): b[i]='B' res=True break else: res=True if(res): print('YES') s1='' for i in b: s1+=i print(s1) else: print('NO') ```
9,090
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` from collections import deque n = int(input()) arr = list(map(int, input().split(" "))) dic = {} for i in arr: if i in dic: dic[i] += 1 else: dic[i] = 1 seta = set() setb = set() a = 0 b = 0 dic_ans = {} ones = set() twos = set() more = set() for i in dic: if dic[i] == 1: ones.add(i) elif dic[i] == 2: twos.add(i) else: more.add(i) for i in ones: if a < b: a += 1 seta.add(i) dic_ans[i] = deque(["A"]) elif b < a: b += 1 setb.add(i) dic_ans[i] = deque(["B"]) else: a += 1 seta.add(i) dic_ans[i] = deque(["A"]) for i in twos: # if a == b: setb.add(i) dic_ans[i] = deque(["B", "A"]) seta.add(i) a+=1 b += 1 # else: for i in more: if a < b: a += 1 seta.add(i) dic_ans[i] = deque(["A"]) for k in range(dic[i]-1): dic_ans[i].append("B") elif b < a: b += 1 setb.add(i) dic_ans[i] = deque(["B"]) for k in range(dic[i]-1): dic_ans[i].append("A") else: seta.add(i) dic_ans[i] = deque() for k in range(dic[i]): dic_ans[i].append("A") # print(*dic_ans) # print(a,b) if a == b: ans = "" for i in arr: ans += dic_ans[i].pop() print("YES") print(ans) else: print("NO") #100 #9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14 ```
9,091
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` n=int(input()) s=list(map(int,input().split())) ct={} a=[0]*(101) for i in range(n): ct[s[i]]=ct.get(s[i],0)+1 for i in ct: a[ct[i]]+=1 if a[1]%2==0: c='A' print('YES') ans='' for i in range(n): if ct[s[i]]==1: ans=ans+c c='A' if c=='B' else 'B' else: ans=ans+'A' print(ans) else: f=0 for i in range(3,101): if a[i]>0: f=i break if f: p=0 c='A' ans='' print('YES') for i in range(n): if ct[s[i]]==1: ans=ans+c c='A' if c=='B' else 'B' elif ct[s[i]]==f and p==0: ans=ans+'B' p=1 else: ans=ans+'A' print(ans) else: print('NO') ```
9,092
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Tags: brute force, dp, greedy, implementation, math Correct Solution: ``` from collections import Counter n=int(input()) s=list(map(int,input().split())) sm=Counter(s) su=set() for i in sm.keys(): if sm[i]==1: su.add(i) j=-1 i=0 while i<n and j==-1: if sm[s[i]]>=3:j=i i+=1 if len(su)%2==1: if j==-1:print("NO") else: print('YES') cnt=0 ch="" i=0 while i<n: if (s[i] in su and cnt<len(su)//2) or i==j : ch+="A" if i!=j:cnt+=1 else: ch+="B" i+=1 print(ch) else: print('YES') cnt=0 ch="" i=0 while i<n: if s[i] in su and cnt<len(su)//2: ch+="A" cnt+=1 else: ch+="B" i+=1 print(ch) ```
9,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` num = int(input()) multiset = [int(x) for x in input().split(' ')] non_multiset = set(multiset) help_dict = dict() good = True add = False add_num = 0 count = 0 for x in non_multiset: if multiset.count(x) == 1: if good: help_dict[x] = 'A' else: help_dict[x] = 'B' good = not good count += 1 if multiset.count(x) >= 3: add = True add_num = x if count % 2 == 1: if add: first = True print('YES') res = '' for x in multiset: if multiset.count(x) == 1: res += help_dict[x] elif x == add_num and first: res += 'B' first = False else: res += 'A' print(res) else: print('NO') else: print('YES') res = '' for x in multiset: if multiset.count(x) == 1: res += help_dict[x] else: res += 'A' print(res) ``` Yes
9,094
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` from collections import deque n = int(input()) arr = list(map(int, input().split(" "))) dic = {} for i in arr: if i in dic: dic[i] += 1 else: dic[i] = 1 a = 0 b = 0 dic_ans = {} ones = set() twos = set() more = set() for i in dic: if dic[i] == 1: ones.add(i) elif dic[i] == 2: twos.add(i) else: more.add(i) for i in ones: if a < b: a += 1 dic_ans[i] = deque(["A"]) elif b < a: b += 1 dic_ans[i] = deque(["B"]) else: a += 1 dic_ans[i] = deque(["A"]) for i in twos: dic_ans[i] = deque(["B", "A"]) a+=1 b += 1 for i in more: if a < b: a += 1 dic_ans[i] = deque(["A"]) for k in range(dic[i]-1): dic_ans[i].append("B") elif b < a: b += 1 dic_ans[i] = deque(["B"]) for k in range(dic[i]-1): dic_ans[i].append("A") else: dic_ans[i] = deque() for k in range(dic[i]): dic_ans[i].append("A") if a == b: ans = "" for i in arr: ans += dic_ans[i].pop() print("YES") print(ans) else: print("NO") ``` Yes
9,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] cnt = 0 cnt1 = 0 q = list() for i in a: if i not in q: q.append(i) if a.count(i) == 1: cnt += 1 elif a.count(i) > 2: cnt1 += 1 if cnt % 2 == 1 and cnt1 == 0: print('NO') else: nums = list() g = list() ans = '' f = True q = False for i in a: if a.count(i) == 1: if f: ans += 'A' f = False else: ans += 'B' f = True elif a.count(i) == 2: ans += 'A' else: if cnt % 2 == 1: if not q: ans += 'B' q = True else: ans += 'A' else: ans += 'A' print('YES') print(ans) ``` Yes
9,096
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) d={} m=10000000000 count1=0 p=0 t=0 for i in a: d[i]=d.get(i,0)+1 if d[i]<m: m=d[i] if m>=2: print('YES') print('A'*n) else: s='A'*n for i in range(n): if d[a[i]]==1 and count1%2==0: s=s[:i]+'B'+s[i+1:] count1+=1 elif d[a[i]]==1: count1+=1 if count1%2==0: print('YES') print(s) else: for i in range(n): if d[a[i]]>2: k=a[i] s=s[:i]+'A'+s[i+1:] t=1 break if t==1: for p in range(i+1,n): if a[p]==k: s=s[:p]+'B'+s[p+1:] print('YES') print(s) if t==0: print('NO') ``` Yes
9,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` from collections import Counter def main(): _ = int(input()) multiset = [int(c) for c in input().split()] cnt = Counter(multiset) res_a = [] res_b = [] minus1 = [] plus1 = [] for k, v in cnt.items(): if v == 1: plus1.append(k) while cnt[k] >= 2: if cnt[k] == 3: minus1.append(k) res_a.append(k) res_b.append(k) cnt[k] -= 2 count = 0 for i, k in enumerate(minus1): count += 1 if i % 2 == 0: res_a.append(k) else: res_b.append(k) if count % 2 == 0: for i, k in enumerate(plus1): if i % 2 == 0: res_a.append(k) else: res_b.append(k) else: if len(plus1) % 2 == 0: print('NO') else: res_a.append(plus1[0]) for i, k in enumerate(plus1[1:]): if i % 2 == 0: res_a.append(k) else: res_b.append(k) res = [] for e in multiset: if e in res_a: res.append('A') res_a.remove(e) else: res.append('B') res_b.remove(e) print('YES') print(''.join(res)) if __name__ == '__main__': main() ``` No
9,098
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya has a multiset s consisting of n integer numbers. Vasya calls some number x nice if it appears in the multiset exactly once. For example, multiset \{1, 1, 2, 3, 3, 3, 4\} contains nice numbers 2 and 4. Vasya wants to split multiset s into two multisets a and b (one of which may be empty) in such a way that the quantity of nice numbers in multiset a would be the same as the quantity of nice numbers in multiset b (the quantity of numbers to appear exactly once in multiset a and the quantity of numbers to appear exactly once in multiset b). Input The first line contains a single integer n~(2 ≤ n ≤ 100). The second line contains n integers s_1, s_2, ... s_n~(1 ≤ s_i ≤ 100) — the multiset s. Output If there exists no split of s to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of n characters. i-th character should be equal to 'A' if the i-th element of multiset s goes to multiset a and 'B' if if the i-th element of multiset s goes to multiset b. Elements are numbered from 1 to n in the order they are given in the input. If there exist multiple solutions, then print any of them. Examples Input 4 3 5 7 1 Output YES BABA Input 3 3 5 1 Output NO Submitted Solution: ``` n=int(input()) s1=0 s2=0 s=['A']*100 dic={} lis=list(map(int,input().split())) length=len(lis) count=0 flag=0 backup=-1 for i in lis: dic[i]=dic.get(i,0)+1 for i in dic: if dic[i]==1: count+=1 if dic[i]>2: flag=1 backup=i if count%2==1: if flag==0: print("NO") exit() lis2=[] for i in dic: if dic[i]==1: lis2.append(i) if backup!=-1: lis2.append(backup) for i in lis2: if s1==0 or s1==s2: s[i]='A' s1+=1 continue if s2+1==s1: s[i]='B' s2+=1 continue print("YES") flag2=0 for i in lis: if i==backup and flag2==0: print(s[i],end="") flag2=1 if s[i]=='A': s[i]='B' else: s[i]='A' continue print(s[i],end="") ``` No
9,099