description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it. How many operations will you need to perform until the next operation does not have any points to delete? -----Input----- Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc. The number of the points is between 1 and 10^6. -----Output----- Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. -----Examples----- Input aabb Output 2 Input aabcaa Output 1 -----Note----- In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to. In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied.
s = input() cur_len = 1 a = [] char = [] for i in range(1, len(s)): if s[i] == s[i - 1]: cur_len += 1 else: a.append(cur_len) char.append(s[i - 1]) cur_len = 1 a.append(cur_len) char.append(s[len(s) - 1]) ans = 0 while len(a) > 1: n = len(a) inner_min = 100000000 for i in range(1, n - 1): if a[i] < inner_min: inner_min = a[i] k = min(a[0], a[n - 1], (inner_min + 1) // 2) b = [] new_char = [] for i in range(n): if i == 0 or i == n - 1: if a[i] > k: b.append(a[i] - k) new_char.append(char[i]) elif a[i] > 2 * k: b.append(a[i] - 2 * k) new_char.append(char[i]) ans += k if len(b) > 1: c = [0] * n newnew_char = [new_char[0]] count = 0 for i in range(0, len(b) - 1): c[count] += b[i] if new_char[i] == new_char[i + 1]: continue else: count += 1 newnew_char.append(new_char[i + 1]) if new_char[len(b) - 2] == new_char[len(b) - 1]: c[count] += b[len(b) - 1] else: newnew_char.append(new_char[i + 1]) c[count] = b[len(b) - 1] a = c[: count + 1] char = newnew_char[:] else: a = b[:] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it. How many operations will you need to perform until the next operation does not have any points to delete? -----Input----- Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc. The number of the points is between 1 and 10^6. -----Output----- Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. -----Examples----- Input aabb Output 2 Input aabcaa Output 1 -----Note----- In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to. In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied.
import sys sys.setrecursionlimit(1000000) read = sys.stdin.readline points = read().strip() lst = [[points[0], 1]] for p in points[1:]: if p == lst[-1][0]: lst[-1][1] += 1 else: lst += [[p, 1]] ans = 0 while len(lst) > 1: ans += 1 tmp = [] if lst[0][1] > 1: tmp.append([lst[0][0], lst[0][1] - 1]) for i in lst[1:-1]: if i[1] > 2: if len(tmp) == 0 or tmp[-1][0] != i[0]: tmp.append([i[0], i[1] - 2]) else: tmp[-1][1] += i[1] - 2 if lst[-1][1] > 1: if len(tmp) == 0 or lst[-1][0] != tmp[-1][0]: tmp.append([lst[-1][0], lst[-1][1] - 1]) else: tmp[-1][1] += lst[-1][1] - 1 lst = tmp print(ans)
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it. How many operations will you need to perform until the next operation does not have any points to delete? -----Input----- Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc. The number of the points is between 1 and 10^6. -----Output----- Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. -----Examples----- Input aabb Output 2 Input aabcaa Output 1 -----Note----- In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to. In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied.
s = input() a = [[s[0], 1]] for i in s[1:]: if a[-1][0] == i: a[-1][1] += 1 else: a.append([i, 1]) turns = 0 while len(a) > 1: turns += 1 temp = [] if a[0][1] > 1: temp.append([a[0][0], a[0][1] - 1]) for i in a[1:-1]: if i[1] > 2: temp.append([i[0], i[1] - 2]) if a[-1][1] > 1: temp.append([a[-1][0], a[-1][1] - 1]) if len(temp) < 2: break a = [temp[0]] for i in temp[1:]: if i[0] != a[-1][0]: a.append(i) else: a[-1][1] += i[1] print(turns)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it. How many operations will you need to perform until the next operation does not have any points to delete? -----Input----- Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc. The number of the points is between 1 and 10^6. -----Output----- Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. -----Examples----- Input aabb Output 2 Input aabcaa Output 1 -----Note----- In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to. In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied.
name = input() blocks = [] now = name[0] counter = 1 for x in range(1, len(name)): if name[x] != now: blocks.append((now, counter)) now = name[x] counter = 1 else: counter += 1 blocks.append((now, counter)) counter = 0 temp = [] while len(blocks) > 1: counter += 1 temp = [] x, y = blocks[0] if y > 1: temp.append((x, y - 1)) for s in range(1, len(blocks) - 1): x, y = blocks[s] if len(temp) > 0: tempx, tempy = temp[-1] if y > 2: if x != tempx: temp.append((x, y - 2)) else: temp[-1] = x, tempy + y - 2 elif y > 2: temp.append((x, y - 2)) x, y = blocks[-1] if len(temp) > 0: tempx, tempy = temp[-1] if y > 1: if x != tempx: temp.append((x, y - 1)) else: temp[-1] = x, tempy + y - 1 elif y > 1: temp.append((x, y - 1)) blocks = temp print(counter)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): s = input() n = len(s) def helper(i): return ( i - 3 >= 0 and (s[i] == "F" or s[i] == "?") and (s[i - 1] == "E" or s[i - 1] == "?") and (s[i - 2] == "H" or s[i - 2] == "?") and (s[i - 3] == "C" or s[i - 3] == "?") ) res = "" i = n - 1 while i >= 0: if helper(i): res = "CHEF" + res i -= 4 continue res = (s[i] if s[i] != "?" else "A") + res i -= 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): s = list(input()) for i in range(len(s) - 4, -1, -1): if ( (s[i] == "C" or s[i] == "?") and (s[i + 1] == "H" or s[i + 1] == "?") and (s[i + 2] == "E" or s[i + 2] == "?") and (s[i + 3] == "F" or s[i + 3] == "?") ): s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" s = str.join("", s) print(s.replace("?", "A"))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) while t: s = list(input()) n = len(s) i = n - 1 while 3 <= i: d = s[i] c = s[i - 1] b = s[i - 2] a = s[i - 3] if ( (a == "?" or a == "C") and (b == "?" or b == "H") and (c == "?" or c == "E") and (d == "?" or d == "F") ): if a == "?": s[i - 3] = "C" if b == "?": s[i - 2] = "H" if c == "?": s[i - 1] = "E" if d == "?": s[i] = "F" i -= 4 continue elif d == "?": s[i] = "A" i -= 1 i = 0 while i < n: if s[i] == "?": s[i] = "A" i += 1 print("".join(s)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
from sys import stdin, stdout input = stdin.readline t = int(input().strip()) for _ in range(t): s = input().strip() string = ["C", "H", "E", "F"] s = [ch for ch in s] n = len(s) count = 0 d = {"C": [0, 3], "H": [1, 2], "E": [2, 1], "F": [3, 0]} i = n - 1 while i >= 0: if s[i] == "?": count += 1 if count == 4: s[i : i + 4] = string count = 0 i -= 1 elif s[i] in string: count = 0 a, b = d[s[i]] flag = 1 sub = s[i - a : i + b + 1] if len(sub) != len(string): flag = 0 else: for k in range(4): if sub[k] != "?" and sub[k] != string[k]: flag = 0 break if flag: s[i - a : i + b + 1] = string i -= a + 1 else: i -= 1 else: count = 0 i -= 1 for i in range(n): if s[i] == "?": s[i] = "A" s = "".join(s) print(s)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def check(arr, l, r): s = "CHEF" flag = 0 for i in range(len(s)): if not (arr[l + i] == s[i] or arr[l + i] == "?"): return False return True n = int(input()) for _ in range(n): s = list(input()) ln = len(s) r = len(s) - 1 l = r - 3 while l >= 0: if check(s, l, r): s[l] = "C" s[l + 1] = "H" s[l + 2] = "E" s[l + 3] = "F" l -= 1 r -= 1 for i in range(ln - 1, -1, -1): if s[i] == "?": s[i] = "A" s = "".join(s) print(s)
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) for _ in range(t): list1 = list(input()) n = len(list1) for i in range(n - 4, 0 - 1, -1): if ( (list1[i] == "C" or list1[i] == "?") and (list1[i + 1] == "H" or list1[i + 1] == "?") and (list1[i + 2] == "E" or list1[i + 2] == "?") and (list1[i + 3] == "F" or list1[i + 3] == "?") ): if list1[i] == "?": list1[i] = "C" if list1[i + 1] == "?": list1[i + 1] = "H" if list1[i + 2] == "?": list1[i + 2] = "E" if list1[i + 3] == "?": list1[i + 3] = "F" for i in range(n): if list1[i] == "?": list1[i] = "A" for val in list1: print(val, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
T = int(input()) for _ in range(T): S = list(input()) for k in range(len(S)): if S[k] >= "A" and S[k] <= "Z" or S[k] == "?": continue else: print("-1") for i in range(len(S) - 4, -1, -1): if not (S[i] == "C" or S[i] == "?"): continue if not (S[i + 1] == "H" or S[i + 1] == "?"): continue if not (S[i + 2] == "E" or S[i + 2] == "?"): continue if not (S[i + 3] == "F" or S[i + 3] == "?"): continue S[i] = "C" S[i + 1] = "H" S[i + 2] = "E" S[i + 3] = "F" for i in range(len(S)): if S[i] == "?": S[i] = "A" S = "".join(S) print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
no_of_test_cases = int(input()) given = "CHEF" def get_pos_for_chef(s, pos): possible = False for n in range(4): start = pos - n end = pos - n + 3 if start < 0 or end >= len(s): continue for i in range(len(given)): if s[start + i] != given[i] and s[start + i] != "?": possible = False break possible = True if possible: return start, end return -1, -1 def get_mod_chef(s): for i in reversed(range(len(s))): if s[i] == "?": start, end = get_pos_for_chef(s, i) if start == -1: s = s[:i] + "A" + s[i + 1 :] else: s = s[:start] + "CHEF" + s[end + 1 :] return s for i in range(no_of_test_cases): given_str = input() print(get_mod_chef(given_str))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR VAR RETURN NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
l = [ "????", "?HEF", "C?EF", "CH?F", "CHE?", "??EF", "?H?F", "?HE?", "C??F", "C?E?", "CH??", "???F", "??E?", "?H??", "C???", "CHEF", ] t = int(input()) for _ in range(t): s = input() n = len(s) temp = list(s) i = n while i - 4 >= 0: test = "".join(temp[i - 4 : i]) if test not in l: i -= 1 else: temp[i - 4 : i] = ["C", "H", "E", "F"] i -= 4 res = "".join(temp) print(res.replace("?", "A"))
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR LIST STRING STRING STRING STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
T = int(input()) output = [] for _ in range(T): s = list(input()) n = len(s) ends = set() for i in range(n): if ( s[i] in ["?", "C"] and i < n - 3 and s[i + 1] in ["?", "H"] and s[i + 2] in ["?", "E"] and s[i + 3] in ["?", "F"] ): ends.add(i + 3) elif ( s[i] in ["?", "H"] and 0 < i < n - 2 and s[i - 1] in ["?", "C"] and s[i + 1] in ["?", "E"] and s[i + 2] in ["?", "F"] ): ends.add(i + 2) elif ( s[i] in ["?", "E"] and 1 < i < n - 1 and s[i - 2] in ["?", "C"] and s[i - 1] in ["?", "H"] and s[i + 1] in ["?", "F"] ): ends.add(i + 1) elif ( s[i] in ["?", "F"] and 2 < i < n and s[i - 3] in ["?", "C"] and s[i - 2] in ["?", "H"] and s[i - 1] in ["?", "E"] ): ends.add(i) ends = list(ends) ends.sort() while len(ends) > 0: end = ends.pop() s[end - 3 : end + 1] = ["C", "H", "E", "F"] while len(ends) > 0 and ends[-1] > end - 4: ends.pop() for i in range(n): if s[i] == "?": s[i] = "A" output.append("".join(s)) for o in output: print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST STRING STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR LIST STRING STRING NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR LIST STRING STRING NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR LIST STRING STRING NUMBER VAR VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER LIST STRING STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING STRING STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def chef(s): string = list(s) length = len(string) if length < 4: for i in range(length): if string[i] == "?": string[i] = "A" return "".join(string) pos = length - 1 while pos >= 3: if ( (string[pos] == "?" or string[pos] == "F") and (string[pos - 1] == "?" or string[pos - 1] == "E") and (string[pos - 2] == "?" or string[pos - 2] == "H") and (string[pos - 3] == "?" or string[pos - 3] == "C") ): string[pos] = "F" string[pos - 1] = "E" string[pos - 2] = "H" string[pos - 3] = "C" pos -= 4 else: pos -= 1 for i in range(length): if string[i] == "?": string[i] = "A" return "".join(string) try: t = int(input()) while t: t -= 1 s = input() ans = chef(s) print(ans) except: pass
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
n = int(input()) while n: n = n - 1 s = list(input()) i = len(s) - 4 while i >= 0: if ( (s[i] == "C" or s[i] == "?") and (s[i + 1] == "H" or s[i + 1] == "?") and (s[i + 2] == "E" or s[i + 2] == "?") and (s[i + 3] == "F" or s[i + 3] == "?") ): s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" i = i - 4 else: i = i - 1 for i in range(len(s)): if s[i] == "?": s[i] = "A" print("".join(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): string = list(input().strip()) N = len(string) i = N - 4 while i >= 0: if ( string[i] in "?C" and string[i + 1] in "?H" and string[i + 2] in "E?" and string[i + 3] in "?F" ): string[i] = "C" string[i + 1] = "H" string[i + 2] = "E" string[i + 3] = "F" i -= 4 else: i -= 1 print("".join(string).replace("?", "A"))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def manychef(s): n = len(s) s = list(s) for i in range(n - 4, -1, -1): if s[i] != "C" and s[i] != "?": continue if s[i + 1] != "H" and s[i + 1] != "?": continue if s[i + 2] != "E" and s[i + 2] != "?": continue if s[i + 3] != "F" and s[i + 3] != "?": continue s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" for i in range(n - 1, -1, -1): if s[i] == "?": s[i] = "A" return "".join(s) t = int(input()) for _ in range(t): s = input() print(manychef(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def check(s, ini): temp = "CHEF" st = ini i = 3 while i >= 0: C = s[st] T = temp[i] if C == T or C == "?": i -= 1 st -= 1 else: return False return True a = int(input()) for i in range(a): d = list(input()) end = len(d) - 1 start = end - 3 while start >= 0: if check(d, end): d[end] = "F" d[end - 1] = "E" d[end - 2] = "H" d[end - 3] = "C" end = end - 4 start = start - 4 else: end -= 1 start -= 1 for k in range(len(d)): if d[k] == "?": d[k] = "A" print(d[k], end="") print()
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def possible_chef(signboard, i): if ( (signboard[i] == "?" or signboard[i] == "C") and (signboard[i + 1] == "?" or signboard[i + 1] == "H") and (signboard[i + 2] == "?" or signboard[i + 2] == "E") and (signboard[i + 3] == "?" or signboard[i + 3] == "F") ): return True return False for _ in range(int(input())): signboard = list(input()) for i in range(len(signboard) - 4, -1, -1): if possible_chef(signboard, i): signboard[i] = "C" signboard[i + 1] = "H" signboard[i + 2] = "E" signboard[i + 3] = "F" for i in range(len(signboard)): if signboard[i] == "?": signboard[i] = "A" print("".join(signboard))
FUNC_DEF IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): s = list(input())[::-1] l = len(s) i = 0 while i < l: if ( i + 3 < l and (s[i] == "F" or s[i] == "?") and (s[i + 1] == "E" or s[i + 1] == "?") and (s[i + 2] == "H" or s[i + 2] == "?") and (s[i + 3] == "C" or s[i + 3] == "?") ): s[i + 3], s[i + 2], s[i + 1], s[i] = "C", "H", "E", "F" i += 3 elif s[i] == "?": s[i] = "A" i += 1 s.reverse() print("".join(s))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING STRING STRING STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): strr = list(input()) tmp = [ "????", "???F", "??E?", "??EF", "?H??", "?H?F", "?HE?", "?HEF", "C???", "C??F", "C?E?", "C?EF", "CH??", "CH?F", "CHE?", "CHEF", ] i = len(strr) - 1 while i - 3 >= 0: dummy = "".join(strr[i - 3 : i + 1]) if dummy in tmp: strr[i - 3], strr[i - 2], strr[i - 1], strr[i] = list("CHEF") i = i - 4 elif strr[i] == "?": strr[i] = "A" i -= 1 else: i -= 1 if i >= 0: for j in range(i + 1): if strr[j] == "?": strr[j] = "A" print("".join(strr))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
possiblity = { "?EHC", "??H?", "?E??", "?EH?", "F?H?", "???C", "?E?C", "FE?C", "F???", "FE??", "????", "FEH?", "??HC", "F??C", "F?HC", } for _ in range(int(input())): st = input() st = st[::-1] ln = len(st) ans = ["0"] * ln i = ln - 1 i = 0 while i + 3 < ln: if st[i : i + 4] in possiblity: ans[i : i + 4] = ["F", "E", "H", "C"] i += 4 else: ans[i] = st[i] i += 1 if i < len(st): ans[i:] = st[i:] for i in range(ln): if ans[i] == "?": ans[i] = "A" print(*ans[::-1], sep="")
ASSIGN VAR STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST STRING STRING STRING STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
T = int(input()) for t in range(T): S = list(input()) for i in range(len(S) - 1, 2, -1): if S[i] in {"F", "?"}: if S[i - 1] in {"E", "?"}: if S[i - 2] in {"H", "?"}: if S[i - 3] in {"C", "?"}: S[i - 3 : i + 1] = ["C", "H", "E", "F"] for i in range(len(S)): if S[i] == "?": S[i] = "A" print("".join(S))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING STRING IF VAR BIN_OP VAR NUMBER STRING STRING IF VAR BIN_OP VAR NUMBER STRING STRING IF VAR BIN_OP VAR NUMBER STRING STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): a = list(input()) i = -1 while i > 0 - (len(a) + 1): if a[i] == "?": try: if ( (a[i - 1] == "?" or a[i - 1] == "E") and (a[i - 2] == "?" or a[i - 2] == "H") and (a[i - 3] == "?" or a[i - 3] == "C") ): a[i] = "F" a[i - 1] = "E" a[i - 2] = "H" a[i - 3] = "C" i -= 3 except: pass elif a[i] == "C": try: if ( (a[i + 1] == "?" or a[i + 1] == "H") and (a[i + 2] == "?" or a[i + 2] == "E") and (a[i + 3] == "?" or a[i + 3] == "F") ): if i + 3 < 0: a[i + 1] = "H" a[i + 2] = "E" a[i + 3] = "F" except: pass elif a[i] == "H": try: if ( (a[i - 1] == "?" or a[i - 1] == "C") and (a[i + 1] == "?" or a[i + 1] == "E") and (a[i + 2] == "?" or a[i + 2] == "F") ): if i + 2 < 0: a[i - 1] = "C" a[i + 1] = "E" a[i + 2] = "F" i -= 1 except: pass elif a[i] == "E": try: if ( (a[i - 2] == "C" or a[i - 2] == "?") and (a[i - 1] == "H" or a[i - 1] == "?") and (a[i + 1] == "?" or a[i + 1] == "F") ): if i + 1 < 0: a[i - 2] = "C" a[i - 1] = "H" a[i + 1] = "F" i -= 2 except: pass elif a[i] == "F": try: if ( (a[i - 3] == "C" or a[i - 3] == "?") and (a[i - 2] == "H" or a[i - 2] == "?") and (a[i - 1] == "E" or a[i - 1] == "?") ): a[i - 3] = "C" a[i - 2] = "H" a[i - 1] = "E" i -= 3 except: pass i -= 1 for i in range(len(a)): if a[i] == "?": print("A", end="") else: print(a[i], end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def solve(): s = list(input()) i = len(s) - 4 while i >= 0: if ( (s[i] == "?" or s[i] == "C") and (s[i + 1] == "?" or s[i + 1] == "H") and (s[i + 2] == "?" or s[i + 2] == "E") and (s[i + 3] == "?" or s[i + 3] == "F") ): s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" i = i - 4 else: i = i - 1 print("".join(s).replace("?", "A")) t = int(input()) while t != 0: solve() t -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
T = int(input()) for i in range(T): s = input() i = 0 i = len(s) a = list(s) while i - 4 >= 0: if ( s[i - 4 : i][0] in ["C", "?"] and s[i - 4 : i][1] in ["H", "?"] and s[i - 4 : i][2] in ["E", "?"] and s[i - 4 : i][3] in ["F", "?"] ): a[i - 4] = "C" a[i - 3] = "H" a[i - 2] = "E" a[i - 1] = "F" i -= 4 else: i -= 1 for i in range(len(s)): if a[i] == "?": a[i] = "A" for i in a: print(i, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER VAR NUMBER LIST STRING STRING VAR BIN_OP VAR NUMBER VAR NUMBER LIST STRING STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def replace(j): if ( S[j] != "?" and S[j] != "C" or S[j + 1] != "?" and S[j + 1] != "H" or S[j + 2] != "?" and S[j + 2] != "E" or S[j + 3] != "?" and S[j + 3] != "F" ): return S[j], S[j + 1], S[j + 2], S[j + 3] = "C", "H", "E", "F" for _ in range(int(input())): S = list(input()) n = len(S) i = n - 4 while i >= 0: replace(i) i -= 1 for i in range(n): if S[i] == "?": S[i] = "A" print("".join(S))
FUNC_DEF IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def chefCanBeFilled(list_s, i): if ( (list_s[i] == "?" or list_s[i] == "C") and (list_s[i + 1] == "?" or list_s[i + 1] == "H") and (list_s[i + 2] == "?" or list_s[i + 2] == "E") and (list_s[i + 3] == "?" or list_s[i + 3] == "F") ): return True return False def greedyChef(s): n = len(s) i = n - 4 list_s = list(s) while i >= 0: if chefCanBeFilled(list_s, i): list_s[i] = "C" list_s[i + 1] = "H" list_s[i + 2] = "E" list_s[i + 3] = "F" i -= 1 i = 0 while i < n: if list_s[i] == "?": list_s[i] = "A" i += 1 return "".join(list_s) t = int(input()) for j in range(t): s = input() print(greedyChef(s))
FUNC_DEF IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) for _t in range(t): S = list(input()) n = len(S) for i in range(n - 1, -1, -1): if i >= 3 and ( S[i - 3] in ("C", "?") and S[i - 2] in ("H", "?") and S[i - 1] in ("E", "?") and S[i - 0] in ("F", "?") ): S[i - 3] = "C" S[i - 2] = "H" S[i - 1] = "E" S[i - 0] = "F" S = "".join(S) S = S.replace("?", "A") print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING STRING VAR BIN_OP VAR NUMBER STRING STRING VAR BIN_OP VAR NUMBER STRING STRING VAR BIN_OP VAR NUMBER STRING STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): W = list(input()) for i in range(len(W) - 1, -1, -1): if W[i] == "?": if ( (i + 1 < len(W) and W[i + 1] == "H") and (i + 2 < len(W) and W[i + 2] == "E") and (i + 3 < len(W) and W[i + 3] == "F") ): W[i] = "C" elif ( (i + 1 < len(W) and W[i + 1] == "E") and (i + 2 < len(W) and W[i + 2] == "F") and (i - 1 >= 0 and (W[i - 1] == "C" or W[i - 1] == "?")) ): W[i] = "H" elif ( (i + 1 < len(W) and W[i + 1] == "F") and (i - 1 >= 0 and (W[i - 1] == "H" or W[i - 1] == "?")) and (i - 2 >= 0 and (W[i - 2] == "C" or W[i - 2] == "?")) ): W[i] = "E" elif ( (i - 1 >= 0 and (W[i - 1] == "E" or W[i - 1] == "?")) and (i - 2 >= 0 and (W[i - 2] == "H" or W[i - 2] == "?")) and (i - 3 >= 0 and (W[i - 3] == "C" or W[i - 3] == "?")) ): W[i] = "F" else: W[i] = "A" print("".join(W))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) for _ in range(t): s = input() if len(s) < 4: for i in range(len(s)): if s[i] == "?": s = s[:i] + "A" + s[i + 1 :] else: for i in range(len(s) - 1, -1, -1): x = s[i - 3 : i + 1] xx = "CHEF" for j in range(len(x)): if x[j] == "?": x = x[:j] + xx[j] + x[j + 1 :] if x == "CHEF": s = s[: i - 3] + "CHEF" + s[i + 1 :] for i in range(len(s)): if s[i] == "?": s = s[:i] + "A" + s[i + 1 :] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def checker(s, e, lst): a = lst[s] b = lst[s + 1] c = lst[s + 2] d = lst[s + 3] if ( (a == "C" or a == "?") and (b == "H" or b == "?") and (c == "E" or c == "?") and (d == "F" or d == "?") ): lst[s] = "C" lst[s + 1] = "H" lst[s + 2] = "E" lst[s + 3] = "F" t = int(input()) for _ in range(t): lst = list(input()) n = len(lst) s = n - 4 e = n - 1 while s >= 0: checker(s, e, lst) s -= 1 e -= 1 for i in range(n): if lst[i] == "?": lst[i] = "A" ans = "".join(lst) print(ans)
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def can_replace(window): word = "CHEF" for i in range(4): if window[i] == "?" or window[i] == word[i]: continue else: return False return True for _ in range(int(input())): string = list(input().strip()) N = len(string) for i in range(N - 4, -1, -1): window = string[i : i + 4] if can_replace(window): string[i : i + 4] = "CHEF" for i in range(N): if string[i] == "?": string[i] = "A" print(*string, sep="")
FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
tests = int(input()) def check(a, b, c, d): return ( (a == "?" or a == "C") and (b == "?" or b == "H") and (c == "?" or c == "E") and (d == "?" or d == "F") ) while tests: s = list(input()) i = len(s) - 1 while i >= 3: if check(s[i - 3], s[i - 2], s[i - 1], s[i]): s[i - 3] = "C" s[i - 2] = "H" s[i - 1] = "E" s[i] = "F" i -= 1 for i in range(len(s)): if s[i] == "?": s[i] = "A" print("".join(s)) tests -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for x in range(int(input())): s = list(input()) chef = [["C", "?"], ["H", "?"], ["E", "?"], ["F", "?"]] ques = ["?"] i = len(s) - 1 for y in range(len(s)): if i < 0: break if i < 3: if s[i] != "?": pass else: s[i] = "A" i -= 1 else: d = s[i] c = s[i - 1] b = s[i - 2] a = s[i - 3] if a in chef[0] and b in chef[1] and c in chef[2] and d in chef[3]: s[i] = "F" s[i - 1] = "E" s[i - 2] = "H" s[i - 3] = "C" i -= 4 else: if s[i] == "?": s[i] = "A" i -= 1 print("".join(s))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST STRING STRING LIST STRING STRING LIST STRING STRING LIST STRING STRING ASSIGN VAR LIST STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
tc = int(input()) for each in range(tc): str1 = str(input()) str2 = str1[::-1] i = len(str1) word = "CHEF" while i >= 4: temp = str1[i - 4 : i] flag = 0 for j, each1 in enumerate(temp): if each1 != word[j] and each1 != "?": flag = 1 break if flag == 0: if i == len(str1): str1 = str1[: i - 4] + "CHEF" else: str1 = str1[: i - 4] + "CHEF" + str1[i:] i -= 4 else: i -= 1 print(str1.replace("?", "A"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def possible_chef(temp): chef = ["F", "E", "H", "C"] for j in range(len(temp)): if temp[j] == "?" or temp[j] == chef[j]: pass else: return False break return True t = int(input()) chef = ["F", "E", "H", "C"] for i in range(t): s = list(input()) sr = s[::-1] j = 0 while j < len(s): if j + 4 > len(s): while j < len(s): if sr[j] == "?": sr[j] = "A" j += 1 else: temp = sr[j : j + 4] ans = possible_chef(temp) if ans: for l in range(4): sr[j + l] = chef[l] j += 4 elif sr[j] == "?": sr[j] = "A" j += 1 else: j += 1 s = sr[::-1] for st in s: print(st, end="") print()
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def isPossible(l): if ( (l[0] == "C" or l[0] == "?") and (l[1] == "H" or l[1] == "?") and (l[2] == "E" or l[2] == "?") and (l[3] == "F" or l[3] == "?") ): return True else: return False T = int(input()) ans = [] for _ in range(T): S = list(input()) N = len(S) for i in range(N - 1, 2, -1): if isPossible(S[i - 3 : i + 1]): S[i] = "F" S[i - 1] = "E" S[i - 2] = "H" S[i - 3] = "C" for i in range(N): if S[i] == "?": S[i] = "A" s = "" for i in S: s += i ans.append(s) for i in ans: print(i)
FUNC_DEF IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
d = int(input()) for k in range(d): str1 = input() str1 = list(str1) i = len(str1) - 4 while i >= 0: if ( (str1[i] == "C" or str1[i] == "?") and (str1[i + 1] == "H" or str1[i + 1] == "?") and (str1[i + 2] == "E" or str1[i + 2] == "?") and (str1[i + 3] == "F" or str1[i + 3] == "?") ): str1[i : i + 4] = list("CHEF") i -= 4 else: i -= 1 print("".join(str1).replace("?", "A"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def check(arr): dic = {(0): "C", (1): "H", (2): "E", (3): "F"} for i in range(4): if arr[i] != "?": if arr[i] != dic[i]: return -1 for i in range(4): if arr[i] == "?": arr[i] = dic[i] return arr t = int(input()) for _ in range(t): arr = input() arr = list(arr) i = len(arr) if i < 4: for k in range(i): if arr[k] == "?": arr[k] = "A" while i > 3: if "?" in arr[i - 4 : i]: if check(arr[i - 4 : i]) == -1: i -= 1 continue else: arr[i - 4 : i] = check(arr[i - 4 : i]) i -= 3 i -= 1 for k in range(len(arr)): if arr[k] == "?": arr[k] = "A" res = "" for ele in arr: res += ele print(res)
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING WHILE VAR NUMBER IF STRING VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def insertChef(text, i): if text[i] != "C" and text[i] != "?": return if text[i + 1] != "H" and text[i + 1] != "?": return if text[i + 2] != "E" and text[i + 2] != "?": return if text[i + 3] != "F" and text[i + 3] != "?": return text[i] = "C" text[i + 1] = "H" text[i + 2] = "E" text[i + 3] = "F" TC = int(input()) for _ in range(TC): text = [c for c in input().strip()] for i in range(len(text) - 4, -1, -1): insertChef(text, i) for i in range(len(text)): if text[i] == "?": text[i] = "A" print("".join(text))
FUNC_DEF IF VAR VAR STRING VAR VAR STRING RETURN IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) while t > 0: a = list(input()) n = len(a) l = [ "????", "??E?", "?H??", "?HE?", "?H?F", "???F", "??EF", "?HEF", "C???", "C?E?", "CH??", "CHE?", "CH?F", "C??F", "C?EF", "CHEF", ] for i in range(16): l[i] = list(l[i]) for i in range(n - 1, -1, -1): if i > 2: if a[i - 3 : i + 1] in l: a[i - 3 : i + 1] = ["C", "H", "E", "F"] for i in range(n): if a[i] == "?": a[i] = "A" print("".join(a)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) for T in range(t): l = list(input()) ind = len(l) ans = 0 while ind > 3: val = l[ind - 4 : ind] if val.count("?") == 0: ind -= 1 continue s1 = "".join(val) if s1 == "CHEF": ind -= 4 continue flag = 0 if val.count("?") > 0: if val[0] != "?" and val[0] != "C": flag = -1 if val[0 + 1] != "?" and val[0 + 1] != "H": flag = -1 if val[0 + 2] != "?" and val[0 + 2] != "E": flag = -1 if val[0 + 3] != "?" and val[0 + 3] != "F": flag = -1 if flag == 0: l[ind - 4] = "C" l[ind - 3] = "H" l[ind - 2] = "E" l[ind - 1] = "F" ind -= 4 continue ind -= 1 n_string = "".join(l) n_string = n_string.replace("?", "A") print(n_string)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER STRING VAR BIN_OP NUMBER NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER STRING VAR BIN_OP NUMBER NUMBER STRING ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER STRING VAR BIN_OP NUMBER NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for i in range(int(input())): s = list(input()) for i in range(len(s) - 4, -1, -1): c = 0 if s[i] == "?": c += 1 elif s[i] == "C": c += 1 else: continue if s[i + 1] == "?": c += 1 elif s[i + 1] == "H": c += 1 else: continue if s[i + 2] == "?": c += 1 elif s[i + 2] == "E": c += 1 else: continue if s[i + 3] == "?": c += 1 elif s[i + 3] == "F": c += 1 else: continue if c == 4: s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" for j in range(len(s) - 1, -1, -1): if s[j] == "?": s[j] = "A" print(*s, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): s = list(input().strip()) i = len(s) - 1 while i >= 0: if ( i - 3 >= 0 and (s[i] == "?" or s[i] == "F") and (s[i - 1] == "?" or s[i - 1] == "E") and (s[i - 2] == "?" or s[i - 2] == "H") and (s[i - 3] == "?" or s[i - 3] == "C") ): s[i] = "F" s[i - 1] = "E" s[i - 2] = "H" s[i - 3] = "C" i -= 4 else: i -= 1 pp = "".join(s) pp = pp.replace("?", "A") print(pp)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
t = int(input()) for i in range(t): s = input() final = "" count = 0 list1 = ["CHE?", "CH?F", "C?EF", "?HEF", "?H?F", "C?E?", "?HE?"] list2 = ["CH??", "C??F", "??EF", "?H??", "??E?"] list3 = ["C???", "???F"] list4 = ["????"] len1 = len(s) rem_len = len1 sub = "" last = "" while rem_len >= 4: start = rem_len - 4 end = rem_len sub = s[start:end] if sub in list1 or sub in list2 or sub in list3 or sub in list4: sub = "CHEF" last = sub + last rem_len -= 4 else: last = s[rem_len - 1 : rem_len] + last rem_len -= 1 last = s[:rem_len] + last last = last.replace("?", "A") print(last)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
def replace_if_possible(i): nonlocal string if ( string[i] != "?" and string[i] != "C" or string[i + 1] != "?" and string[i + 1] != "H" or string[i + 2] != "?" and string[i + 2] != "E" or string[i + 3] != "?" and string[i + 3] != "F" ): return string[i : i + 4] = "CHEF" t = int(input()) for _ in range(t): string = list(input()) i = len(string) - 4 while i >= 0: replace_if_possible(i) i -= 1 for i in range(len(string)): if string[i] == "?": string[i] = "A" print("".join(string))
FUNC_DEF IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN ASSIGN VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
for _ in range(int(input())): arr = list(input()) for i in range(len(arr) - 4, -1, -1): if arr[i] == "C" or arr[i] == "?": if arr[i + 1] == "H" or arr[i + 1] == "?": if arr[i + 2] == "E" or arr[i + 2] == "?": if arr[i + 3] == "F" or arr[i + 3] == "?": arr[i : i + 4] = ["C", "H", "E", "F"] for i in range(len(arr)): if arr[i] == "?": arr[i] = "A" print("".join(arr))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Chef Ciel wants to put a fancy neon signboard over the entrance of her restaurant. She has not enough money to buy the new one so she bought some old neon signboard through the internet. Ciel was quite disappointed when she received her order - some of its letters were broken. But she realized that this is even better - she could replace each broken letter by any letter she wants. So she decided to do such a replacement that the resulting signboard will contain the word "CHEF" as many times as possible. We can model the signboard as a string S having capital letters from 'A' to 'Z', inclusive, and question marks '?'. Letters in the string indicate the intact letters at the signboard, while question marks indicate broken letters. So Ciel will replace each question mark with some capital letter and her goal is to get the string that contains as many substrings equal to "CHEF" as possible. If there exist several such strings, she will choose the lexicographically smallest one. Note 1. The string S = S1...SN has the substring "CHEF" if for some i we have SiSi+1Si+2Si+3 = "CHEF". The number of times "CHEF" is the substring of S is the number of those i for which SiSi+1Si+2Si+3 = "CHEF". Note 2. The string A = A1...AN is called lexicographically smaller than the string B = B1...BN if there exists K from 1 to N, inclusive, such that Ai = Bi for i = 1, ..., K-1, and AK < BK. In particular, A is lexicographically smaller than B if A1 < B1. We compare capital letters by their positions in the English alphabet. So 'A' is the smallest letter, 'B' is the second smallest letter, ..., 'Z' is the largest letter. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a string S. -----Output----- For each test case, output a single line containing the content of the signboard Chef Ciel will come up with. That is you should output the lexicographically smallest string that could be obtained from the input string by replacing all its question marks by some capital letters and having as many substrings equal to "CHEF" as possible. -----Constraints----- - 1 ≤ T ≤ 2013 - 1 ≤ length of S ≤ 2013 - Each character in S is either a capital letter from 'A' to 'Z', inclusive, or the question mark '?'. -----Example----- Input: 5 ????CIELIS???E? ????CIELISOUR???F T?KEITE?SY ???????? ???C??? Output: CHEFCIELISACHEF CHEFCIELISOURCHEF TAKEITEASY CHEFCHEF AAACHEF -----Explanation ----- Example Case 1. Here the resulting string can have at most 2 substrings equal to "CHEF". For example, some possible such strings are: - CHEFCIELISACHEF - CHEFCIELISQCHEF - CHEFCIELISZCHEF However, lexicographically smallest one is the first one. Example Case 3. Here the resulting string cannot have "CHEF" as its substring. Therefore, you must simply output the lexicographically smallest string that can be obtained from the given one by replacing question marks with capital letters.
T = int(input()) for z in range(T): s = list(input()) l = len(s) i = l - 4 while i >= 0: if ( (s[i] == "?" or s[i] == "C") and (s[i + 1] == "?" or s[i + 1] == "H") and (s[i + 2] == "?" or s[i + 2] == "E") and (s[i + 3] == "?" or s[i + 3] == "F") ): s[i] = "C" s[i + 1] = "H" s[i + 2] = "E" s[i + 3] = "F" i -= 4 else: i -= 1 print("".join(s).replace("?", "A"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, V, adj): minimum_weights = [float("inf")] * V minimum_weights[0] = 0 minimum_spanning_tree = [False] * V sum_of_weights = 0 for _ in range(V): vertex = self.get_minimum_vertex(minimum_weights, minimum_spanning_tree, V) minimum_spanning_tree[vertex] = True sum_of_weights += minimum_weights[vertex] for adjacent, weight in adj[vertex]: if ( not minimum_spanning_tree[adjacent] and weight < minimum_weights[adjacent] ): minimum_weights[adjacent] = weight return sum_of_weights def get_minimum_vertex(self, minimum_weights, minimum_spanning_tree, V): min_index = None _min = float("inf") for v in range(V): if not minimum_spanning_tree[v] and ( min_index is None or minimum_weights[v] < _min ): min_index = v _min = minimum_weights[v] return min_index
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, v, graph): key = [999999999] * v key[0] = 0 mst = [False] * v res = 0 for count in range(v): u = -1 for i in range(v): if mst[i] == False and (u == -1 or key[i] < key[u]): u = i res += key[u] mst[u] = True for j, wt in graph[u]: if wt != 0: key[j] = min(key[j], wt) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, V, adj): total_min_weight = 0 min_spanning_tree = [False] * V min_weights = [float("inf")] * V min_weights[0] = 0 for _ in range(V): vertex = self.get_min_vertex(min_spanning_tree, min_weights) min_spanning_tree[vertex] = True total_min_weight += min_weights[vertex] for adjacent, weight in adj[vertex]: if not min_spanning_tree[adjacent] and weight < min_weights[adjacent]: min_weights[adjacent] = weight return total_min_weight def get_min_vertex(self, mst, weights): min_index = None _min = float("inf") for i in range(len(mst)): if not mst[i] and (min_index is None or weights[i] < _min): min_index = i _min = weights[i] return min_index
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class DisjointSet: rank, parent, size = [], [], [] def __init__(self, n): self.rank = [0] * (n + 1) self.parent = [None] * (n + 1) self.size = [None] * (n + 1) for i in range(n + 1): self.parent[i] = i self.size[i] = 1 def findUPar(self, node) -> int: if node == self.parent[node]: return node self.parent[node] = self.findUPar(self.parent[node]) return self.parent[node] def unionByRank(self, u, v): ulp_u, ulp_v = self.findUPar(u), self.findUPar(v) if ulp_u == ulp_v: return if self.rank[ulp_u] < self.rank[ulp_v]: self.parent[ulp_u] = ulp_v elif self.rank[ulp_v] < self.rank[ulp_u]: self.parent[ulp_v] = ulp_u else: self.parent[ulp_v] = ulp_u self.rank[ulp_u] += 1 def unionBySize(self, u, v): ulp_u, ulp_v = self.findUPar(u), self.findUPar(v) if ulp_u == ulp_v: return if self.size[ulp_u] < self.size[ulp_v]: self.parent[ulp_u] = self.parent[ulp_v] self.size[ulp_v] += self.size[ulp_u] else: self.parent[ulp_v] = ulp_u self.size[ulp_u] += self.size[ulp_v] class Solution: def spanningTree(self, V, adj): edges = [] for i in range(V): for it in adj[i]: adjNode = it[0] wt = it[1] node = i edges.append((wt, (node, adjNode))) ds = DisjointSet(V) edges.sort() mstWt = 0 for it in edges: wt, u, v = it[0], it[1][0], it[1][1] if ds.findUPar(u) != ds.findUPar(v): mstWt += wt ds.unionBySize(u, v) return mstWt
CLASS_DEF ASSIGN VAR VAR VAR LIST LIST LIST FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, V, adj): class Node: def __init__(self, value): self.parent = self self.rank = 0 self.value = value def findSet(x): if x.parent != x: x.parent = findSet(x.parent) return x.parent def findUnion(x, y): x = findSet(x) y = findSet(y) if x.rank > y.rank: y.parent = x else: x.parent = y if x.rank == y.rank: y.rank += 1 def Kruskal(E, n): A = [] s = 0 V = [Node(i) for i in range(n)] E.sort(key=lambda x: x[2]) for e in E: u, v, w = e if findSet(V[u]) != findSet(V[v]): findUnion(V[u], V[v]) A += [e] s += w return s E = [] for i in range(len(adj)): for j in range(len(adj[i])): E.append((i, adj[i][j][0], adj[i][j][1])) return Kruskal(E, V)
CLASS_DEF FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR LIST VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Edge: def __init__(self, u, v, w): self.u = u self.v = v self.w = w def __hash__(self): if self.u < self.v: return str(self.u) + "--" + str(self.v) else: return str(self.v) + "--" + str(self.u) def __repr__(self): return str(self.u) + "<-->" + str(self.v) + "=" + str(self.w) def __lt__(self, other): return self.w < other.w class Solution: def find_parent(self, u): p_u = self.parent[u] if p_u == u: return p_u else: self.parent[u] = self.find_parent(p_u) return self.parent[u] def union(self, p_u, p_v): r_u, r_v = self.rank[p_u], self.rank[p_v] if r_u < r_v: self.parent[p_u] = p_v elif r_u > r_v: self.parent[p_v] = p_u else: self.rank[p_u] += 1 self.parent[p_v] = p_u def spanningTree(self, V, adj): all_edges = list() self.parent = [i for i in range(V)] self.rank = [(0) for i in range(V)] for u in range(V): for v, w in adj[u]: if u < v: all_edges.append(Edge(u, v, w)) mst = [] for edge in sorted(all_edges): p_u = self.find_parent(edge.u) p_v = self.find_parent(edge.v) if p_u == p_v: continue else: mst.append(edge) self.union(p_u, p_v) sum = 0 for edge in mst: sum += edge.w return sum
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class DisjointSet: def __init__(self, V): self.rank = [(0) for _ in range(V)] self.size = [(1) for _ in range(V)] self.par = [i for i in range(V)] def findPar(self, node): if node == self.par[node]: return node self.par[node] = self.findPar(self.par[node]) return self.par[node] def unionByRank(self, u, v): ulp_u = self.findPar(u) ulp_v = self.findPar(v) if ulp_v == ulp_u: return if self.rank[ulp_u] < self.rank[ulp_v]: self.par[ulp_u] = ulp_v elif self.rank[ulp_u] > self.rank[ulp_v]: self.par[ulp_v] = ulp_u else: self.par[ulp_v] = ulp_u self.rank[ulp_u] += 1 def unionBySize(self, u, v): ulp_u = self.findPar(u) ulp_v = self.findPar(v) if ulp_v == ulp_u: return if self.size[ulp_u] < self.size[ulp_v]: self.par[ulp_u] = ulp_v self.size[ulp_v] += self.size[ulp_u] else: self.par[ulp_v] = ulp_u self.size[ulp_u] += self.size[ulp_v] class Solution: def spanningTree(self, V, adj): edges = list() for node in range(V): for adjNode, wt in adj[node]: edges.append((wt, node, adjNode)) ob = DisjointSet(V) edges.sort() res = 0 for wt, node, adjNode in edges: if ob.findPar(node) != ob.findPar(adjNode): res += wt ob.unionByRank(node, adjNode) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def find(parent, i): if parent[i] == i: return i return Solution.find(parent, parent[i]) def union(parent, rank, x, y): xroot = Solution.find(parent, x) yroot = Solution.find(parent, y) if rank[xroot] < rank[yroot]: parent[xroot] = yroot elif rank[xroot] > rank[yroot]: parent[yroot] = xroot else: parent[yroot] = xroot rank[xroot] += 1 def spanningTree(self, V, adj): edges = [] for u in range(V): for v, wt in adj[u]: edges.append((u, v, wt)) edges.sort(key=lambda x: x[2]) parent = [i for i in range(V)] rank = [0] * V mst_weight = 0 for u, v, wt in edges: uroot = Solution.find(parent, u) vroot = Solution.find(parent, v) if uroot != vroot: mst_weight += wt Solution.union(parent, rank, uroot, vroot) return mst_weight
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class edge: def __init__(self, v1, v2, wt): self.v1 = v1 self.v2 = v2 self.wt = wt class Solution: def get_parent(self, i, parent): if parent[i] != i: parent[i] = self.get_parent(parent[i], parent) return parent[i] def union(self, x, y, parent, rank): if rank[x] > rank[y]: parent[y] = x elif rank[y] > rank[x]: parent[x] = y else: parent[y] = x rank[x] += 1 def spanningTree(self, V, adj): ans = [] li = [] parent = [i for i in range(V)] rank = [(0) for i in range(V)] for v1 in range(V): for v2, wt in adj[v1]: li.append(edge(v1, v2, wt)) li = sorted(li, key=lambda edge: edge.wt) edges = 0 i = 0 while edges < V - 1: current_edge = li[i] p1 = self.get_parent(current_edge.v1, parent) p2 = self.get_parent(current_edge.v2, parent) if p1 != p2: ans.append(current_edge) self.union(p1, p2, parent, rank) edges += 1 i += 1 mincost = 0 for i in ans: mincost += i.wt return mincost
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
import sys class Solution: def spanningTree(self, V, adj): sys.setrecursionlimit(10**6) arr = [i for i in range(V)] edges = [] for u, nbrs in enumerate(adj): for v, w in nbrs: edges.append((u, v, w)) edges.sort(key=lambda e: e[2]) def find(i): if arr[i] != i: arr[i] = find(arr[i]) return arr[i] ans = 0 for u, v, w in edges: ru, rv = find(u), find(v) if ru != rv: arr[ru] = rv ans += w return ans class DJS: def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [0] * (n + 1) self.size = [1] * (n + 1) def find(self, u): if u != self.par[u]: self.par[u] = self.find(self.par[u]) return self.par[u] return u def union(self, u, v): pu = self.find(u) pv = self.find(v) if pu == pv: return if self.rank[pu] < self.rank[pv]: self.par[pu] = pv else: self.par[pv] = pu if self.rank[pu] == self.rank[pv]: self.rank[pu] += 1 def union_by_size(self, u, v): pu = self.find(u) pv = self.find(v) if pu == pv: return if self.size[pu] < self.size[pv]: self.par[pu] = pv self.size[pv] += self.size[pu] else: self.par[pv] = pu self.size[pu] += self.size[pv]
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class DisjointSet: def __init__(self, n): self.rank = [0] * (n + 1) self.parent = [i for i in range(n + 1)] def find(self, node): if node == self.parent[node]: return node self.parent[node] = self.find(self.parent[node]) return self.parent[node] def union(self, u, v): pu = self.find(u) pv = self.find(v) if pu == pv: return if self.rank[pu] > self.rank[pv]: self.parent[pu] = pv elif self.rank[pv] > self.rank[pu]: self.parent[pv] = pu else: self.parent[pv] = pu self.rank[pu] += 1 class Solution: def spanningTree(self, V, adj): edges = [] djs = DisjointSet(V) mst = 0 for u in range(V): for v, w in adj[u]: edges.append((w, u, v)) edges.sort(key=lambda x: x[0]) for w, u, v in edges: if djs.find(u) != djs.find(v): mst += w djs.union(u, v) return mst
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, V, adj): parent = [(-1) for hm in range(V)] def find(hm): while parent[hm] != -1: hm = parent[hm] return hm rank = [(0) for hm in range(V)] memory = [] cost = 0 t = 0 for hm in range(len(adj)): for gm in adj[hm]: memory.append([hm, gm[0], gm[1]]) memory.sort(key=lambda x: x[2]) for hm in memory: if t == V - 1: return cost if parent[hm[0]] != -1: parent[hm[0]] = find(hm[0]) if parent[hm[1]] != -1: parent[hm[1]] = find(hm[1]) x = find(hm[0]) y = find(hm[1]) if x == y: continue t += 1 cost += hm[2] if rank[x] > rank[y]: parent[y] = x rank[x] += 1 else: parent[x] = y rank[y] += 1 return cost return cost
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def spanningTree(self, V, adj): edges = [] for i in range(V): for it in adj[i]: adjnode = it[0] wt = it[1] node = i edges.append((wt, node, adjnode)) edges.sort() ds = disjoint(V) mwt = 0 for it in edges: wt = it[0] node = it[1] adjnode = it[2] if ds.findUPar(node) != ds.findUPar(adjnode): mwt += wt ds.unionrank(node, adjnode) return mwt class disjoint: def __init__(self, n): self.rank = [0] * (n + 1) self.parent = [0] * (n + 1) for i in range(n): self.parent[i] = i def findUPar(self, node): if node == self.parent[node]: return node return self.findUPar(self.parent[node]) def unionrank(self, u, v): Up_u = self.findUPar(u) Up_v = self.findUPar(v) if Up_u == Up_v: return if self.rank[Up_u] < self.rank[Up_v]: self.parent[Up_u] = Up_v elif self.rank[Up_v] < self.rank[Up_u]: self.parent[Up_v] = Up_u else: self.parent[Up_v] = Up_u self.rank[Up_u] += 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def findParent(self, parent, node): if parent[node] == node: return node parent[node] = self.findParent(parent, parent[node]) return parent[node] def unionSet(self, u, v, parent, rank): u = self.findParent(parent, u) v = self.findParent(parent, v) if rank[u] < rank[v]: parent[u] = v elif rank[v] < rank[u]: parent[v] = u else: parent[v] = u rank[u] += 1 def spanningTree(self, V, adj): adjList = [] for i in range(len(adj)): for j in range(len(adj[i])): adj[i][j].insert(0, i) adjList.append(adj[i][j]) adjList.sort(key=lambda x: x[2]) parent = [i for i in range(V)] rank = [0] * V minCost = 0 for i in range(len(adjList)): u = self.findParent(parent, adjList[i][0]) v = self.findParent(parent, adjList[i][1]) wt = adjList[i][2] if u != v: minCost += wt self.unionSet(u, v, parent, rank) return minCost
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class DSU: def __init__(self, n): self.parent = [i for i in range(n)] self.rank = [0] * n def find(self, x): if self.parent[x] == x: return x self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, src, dest): x_parent, y_parent = self.find(src), self.find(dest) if x_parent != y_parent: if self.rank[x_parent] < self.rank[y_parent]: self.parent[x_parent] = y_parent elif self.rank[x_parent] > self.rank[y_parent]: self.parent[y_parent] = x_parent else: self.parent[x_parent] = y_parent self.rank[y_parent] += 1 return True return False class Solution: def spanningTree(self, n, edges): graph = [] for i in range(n): for j in adj[i]: dest = j[0] cost = j[1] graph.append([cost, i, dest]) graph.sort() ans = 0 build_graph = DSU(n) for cost, src, dest in graph: if build_graph.union(src, dest): ans += cost return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def getminvertex(self, visited, weight): minwt = sys.maxsize for v in range(V): if visited[v] == False and weight[v] < minwt: minwt = weight[v] min_index = v return min_index def spanningTree(self, V, adj): parent = [(-1) for i in range(V)] visited = [(False) for i in range(V)] weight = [sys.maxsize for i in range(V)] weight[0] = 0 mincost = 0 for i in range(V): minvertex = self.getminvertex(visited, weight) visited[minvertex] = True mincost += weight[minvertex] for neighbour_vertex, wt in adj[minvertex]: if visited[neighbour_vertex] == False and weight[neighbour_vertex] > wt: weight[neighbour_vertex] = wt parent[neighbour_vertex] = minvertex return mincost
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
def find_parent(u, parent): if parent[u] != u: parent[u] = find_parent(parent[u], parent) return parent[u] def find_union(dis, u, v, parent, rank): parent_u = find_parent(u, parent) parent_v = find_parent(v, parent) if parent_u == parent_v: return 0 pass else: if rank[parent_u] > rank[parent_v]: rank[parent_u] += rank[parent_v] parent[parent_v] = parent_u if rank[parent_v] > rank[parent_u]: rank[parent_v] += rank[parent_u] parent[parent_u] = parent_v else: rank[parent_u] += rank[parent_v] parent[parent_v] = parent_u return dis class Solution: def spanningTree(self, V, adj): parent = {} rank = {} new_adj = [] d = {} for i in range(V): d[i] = [] parent[i] = i rank[i] = 1 for i in range(len(adj)): for j in range(len(adj[i])): if adj[i][j][0] not in d[i]: d[i].append(adj[i][j][0]) d[adj[i][j][0]].append(i) new_adj.append([adj[i][j][1], i, adj[i][j][0]]) new_adj.sort() total = 0 store = [] for dis, u, v in new_adj: store.append(find_union(dis, u, v, parent, rank)) total += store[-1] return total
FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
import sys class Solution: def spanningTree(self, V, adj): import sys sys.setrecursionlimit(10**6) arr = [i for i in range(V)] edges = [] for u, nbrs in enumerate(adj): for v, w in nbrs: edges.append((u, v, w)) edges.sort(key=lambda e: e[2]) def find(i): if arr[i] != i: arr[i] = find(arr[i]) return arr[i] ans = 0 for u, v, w in edges: ru, rv = find(u), find(v) if ru != rv: arr[ru] = rv ans += w return ans
IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Example 1: Input: 3 3 0 1 5 1 2 3 0 2 1 Output: 4 Explanation: The Spanning Tree resulting in a weight of 4 is shown above. Example 2: Input: 2 1 0 1 5 Output: 5 Explanation: Only one Spanning Tree is possible which has a weight of 5. Your task: Since this is a functional problem you don't have to worry about input, you just have to complete the function spanningTree() which takes a number of vertices V and an adjacency matrix adj as input parameters and returns an integer denoting the sum of weights of the edges of the Minimum Spanning Tree. Here adj[i] contains a list of three integers, first integer and second integer denotes that there is an edge between adj[i][0] and adj[i][1] and the third integer denotes the weight of that edge i.e. adj[i][2] In other words, adj[i][j] is of form { u, v, wt }. So, this denotes that there is an edge between u and v with an edge weight equal to wt. Expected Time Complexity: O(ElogV). Expected Auxiliary Space: O(V^{2}). Constraints: 2 ≤ V ≤ 1000 V-1 ≤ E ≤ (V*(V-1))/2 1 ≤ w ≤ 1000 The graph is connected and doesn't contain self-loops & multiple edges.
class Solution: def parentrec(self, i, parent): if parent[i] == i: return i return self.parentrec(parent[i], parent) def spanningTree(self, V, adj): parent = [] edge = [] s = set() for i in range(V): parent.append(i) for i in range(len(adj)): for j in adj[i]: if (i, j[0]) not in s or (j[0], i) not in s: edge.append([i, j[0], j[1]]) s.add((i, j[0])) s.add((j[0], i)) ss = 0 i = 0 summ = 0 edge.sort(key=lambda a: a[2]) while ss < V - 1: x = edge[i][0] y = edge[i][1] px = self.parentrec(x, parent) py = self.parentrec(y, parent) if px != py: summ = summ + edge[i][2] parent[py] = px ss += 1 i += 1 return summ
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pos = {(0): 0} cumsum = 0 ans = 0 for num in nums: cumsum += num pos[cumsum] = max(ans, pos.get(cumsum - target, -1) + 1) ans = max(ans, pos[cumsum]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: sett = set([0]) ans = 0 cur = 0 for val in nums: cur += val if cur - target in sett: ans += 1 sett = set() sett.add(cur) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) dp = [0] * (n + 1) dic = {} dic[0] = 0 cur = 0 for i in range(1, n + 1): cur += nums[i - 1] if cur - target in dic: dp[i] = 1 + dp[dic[cur - target]] dp[i] = max(dp[i - 1], dp[i]) dic[cur] = i return dp[n]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pref = {} pref[0] = -1 ans = 0 presum = 0 j = -1 for i in range(len(nums)): presum += nums[i] c = presum - target if c in pref and pref[c] >= j: ans += 1 j = i pref[presum] = j return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: N = len(nums) prefix_sum = [(0) for _ in range(N)] prefix_sum[0] = nums[0] for i in range(1, N): prefix_sum[i] = nums[i] + prefix_sum[i - 1] prev_prefix = {} last_index = -1 subarrays = 0 for i in range(N): prefix = prefix_sum[i] to_look = prefix - target if to_look in prev_prefix and prev_prefix[to_look] >= last_index: last_index = i subarrays += 1 elif nums[i] == target: subarrays += 1 last_index = i elif prefix == target and last_index == -1: subarrays += 1 last_index = i prev_prefix[prefix] = i return subarrays
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: dp = [0] * len(nums) prev_sum = {(0): -1} num_nonover = 0 current_sum = 0 for index, num in enumerate(nums): current_sum += num if current_sum - target in prev_sum: gain = ( dp[prev_sum[current_sum - target]] if prev_sum[current_sum - target] >= 0 else 0 ) num_nonover = max(num_nonover, gain + 1) dp[index] = num_nonover prev_sum[current_sum] = index return num_nonover
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pos = {(0): -1} n = len(nums) dp = [0] * (n + 1) s = 0 for i in range(n): s += nums[i] dp[i + 1] = dp[i] t = s - target if t in pos: dp[i + 1] = max(dp[i + 1], dp[pos[t] + 1] + 1) pos[s] = i return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) result = 0 pre_sum = [0] * (n + 1) for i, num in enumerate(nums): pre_sum[i + 1] = pre_sum[i] + num seen = {(0): 0} for i in range(1, n + 1): cur = pre_sum[i] if cur - target in seen: result += 1 seen = {} seen[cur] = i return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: count = 0 total = 0 pastSums = set() for n in nums: total += n if total == target or total - target in pastSums: total = 0 count += 1 pastSums.clear() else: pastSums.add(total) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) for i in range(1, n): nums[i] += nums[i - 1] seen, dp = {(0): -1}, [0] * n for i in range(n): x = nums[i] - target if x in seen: dp[i] = max(dp[i - 1], dp[seen[x]] + 1) else: dp[i] = dp[i - 1] seen[nums[i]] = i return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR DICT NUMBER NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: if len(nums) == 0: return 0 h = {} dp = [] s = nums[0] if s == target: dp.append(1) else: dp.append(0) h[s] = 0 for i in range(1, len(nums)): s = s + nums[i] if s == target: if 0 in h: dp.append(max(dp[h[0]] + 1, dp[-1])) else: dp.append(max(1, dp[-1])) elif s - target in h: dp.append(max(dp[h[s - target]] + 1, dp[-1])) else: dp.append(dp[-1]) h[s] = i return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: d = {} d[0] = -1 N = len(nums) if N == 0: return 0 L = [(0) for i in range(N)] s = nums[0] d[s] = 0 if s == target: L[0] = 1 for i in range(1, N): s += nums[i] L[i] = L[i - 1] if s - target in d: idx = d[s - target] if idx == -1: L[i] = max(L[i], 1) else: L[i] = max(L[i], L[idx] + 1) d[s] = i return L[N - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) presum = 0 last_pos = {(0): 0} dp = [0] * (1 + n) for i in range(1, 1 + n): presum += nums[i - 1] dp[i] = dp[i - 1] if presum - target in last_pos: pos = last_pos[presum - target] dp[i] = max(dp[i], dp[pos] + 1) last_pos[presum] = i return dp[n]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: d = {(0): True} sm = 0 count = 0 for i in nums: sm += i if sm - target in d: count += 1 sm = 0 d = {(0): True} else: d[sm] = True return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: run_sum = 0 count = 0 book_keep = {(0): 1} for num in nums: run_sum += num prev_sum = run_sum - target if prev_sum in book_keep: count += 1 run_sum = 0 book_keep.clear() book_keep = {(0): 1} else: book_keep[run_sum] = 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, A: List[int], T: int) -> int: N = len(A) prefix_sum = [(0) for _ in range(N + 1)] for i in range(1, N + 1): prefix_sum[i] = prefix_sum[i - 1] + A[i - 1] seen = set([0]) res = 0 for i in range(N): curr = prefix_sum[i + 1] want = curr - T if want in seen: res += 1 seen = set() seen.add(curr) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: prefix_sum = count = 0 prev_subarray_end = -1 seen = {(0): -1} for i, num in enumerate(nums): prefix_sum += num if seen.get(prefix_sum - target, -2) >= prev_subarray_end: count += 1 prev_subarray_end = i seen[prefix_sum] = i return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: prefix = [0] for x in nums: prefix.append(prefix[-1] + x) res = 0 seen = set() for x in prefix: if x - target in seen: res += 1 seen = {x} else: seen.add(x) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: pre = [0] for n in nums: pre.append(pre[-1] + n) dp = {(0): 0} counter = [0] * len(pre) for i in range(1, len(pre)): if pre[i] - target in dp: idx = dp[pre[i] - target] counter[i] = max(counter[i - 1], counter[idx] + 1) else: counter[i] = counter[i - 1] dp[pre[i]] = i return counter[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: count, sums, curr = 0, {0}, 0 for num in nums: curr += num if curr - target in sums: count += 1 sums.clear() sums.add(curr) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: for i in range(1, len(nums)): nums[i] += nums[i - 1] res = 0 seen = {} used_i = 0 nums = [0] + nums for i, num in enumerate(nums): if num - target in seen and seen[num - target] >= used_i: res += 1 used_i = i seen[num] = i return res
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) d = {(0): -1} cursum = 0 ans = 0 for i, n in enumerate(nums): cursum += n prev = cursum - target if prev in d: ans += 1 d = {} d[cursum] = i return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: subarrays = [] summ = 0 lookup = set([0]) count = 0 for num in nums: summ += num if summ - target in lookup: count += 1 summ = 0 lookup = set([0]) else: lookup.add(summ) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: N = len(nums) rtv = 0 presum = dict() presum[0] = -1 total = 0 for i, v in enumerate(nums): total += v ne = total - target if ne in presum and presum[ne] < i: rtv += 1 presum = dict() presum[0] = i total = 0 else: presum[total] = i return rtv
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: dp = [0] + [-1] * len(nums) s = 0 dic = {} dic[0] = 0 res = 0 for i in range(len(nums)): s += nums[i] if s - target in dic: dp[i + 1] = max(dp[dic[s - target]] + 1, dp[i]) else: dp[i + 1] = dp[i] dic[s] = i + 1 print(dp) return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: res = 0 s = set() s.add(0) t = 0 for x in nums: t += x if t - target in s: res += 1 s.clear() s.add(t) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) prefix_sum = [0] * n lookup = {} dp = [0] * n dp[0] = 1 if nums[0] == target else 0 prefix_sum[0] = nums[0] lookup[nums[0]] = 0 for i in range(1, len(nums)): prefix_sum[i] = prefix_sum[i - 1] + nums[i] check_for = prefix_sum[i] - target cmp_val = 0 if check_for in lookup: cmp_val = dp[lookup[check_for]] + 1 elif check_for == 0: cmp_val = 1 dp[i] = max(dp[i - 1], cmp_val) lookup[prefix_sum[i]] = i return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: prefixsum = {} prefixsum[0] = True prev = 0 res = 0 for num in nums: curr = prev + num if curr - target in prefixsum: res += 1 prefixsum = {} prefixsum[0] = True prev = 0 else: prefixsum[curr] = True prev = curr print(res) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: P = [0] for e in nums: P.append(P[-1] + e) seen = set() ans = 0 for psum in P: if psum - target in seen: seen = {psum} ans += 1 else: seen.add(psum) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: n = len(nums) pref_sum = [(0) for _ in range(n + 1)] for i in range(n): pref_sum[i + 1] = pref_sum[i] + nums[i] dp = collections.defaultdict(lambda: -1) cur_max = 0 dp[0] = 0 for pref in range(1, n + 1): cur_max = max(cur_max, dp[pref_sum[pref] - target] + 1) dp[pref_sum[pref]] = cur_max return cur_max
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR
Given an array nums and an integer target. Return the maximum number of non-empty non-overlapping subarrays such that the sum of values in each subarray is equal to target.   Example 1: Input: nums = [1,1,1,1,1], target = 2 Output: 2 Explanation: There are 2 non-overlapping subarrays [1,1,1,1,1] with sum equals to target(2). Example 2: Input: nums = [-1,3,5,1,4,2,-9], target = 6 Output: 2 Explanation: There are 3 subarrays with sum equal to 6. ([5,1], [4,2], [3,5,1,4,2,-9]) but only the first 2 are non-overlapping. Example 3: Input: nums = [-2,6,6,3,5,4,1,2,8], target = 10 Output: 3 Example 4: Input: nums = [0,0,0], target = 0 Output: 3   Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 0 <= target <= 10^6
class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: sm = 0 hsh = {} dp = {} dp[-1] = 0 ans = 0 for i in range(len(nums)): dp[i] = dp[i - 1] sm += nums[i] if sm - target in hsh: dp[i] = max(dp[i], dp[hsh[sm - target]] + 1) hsh[sm] = i if sm == target: dp[i] = max(dp[i], 1) ans = max(ans, dp[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR