description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): ans = [] ptr1 = head ptr2 = head prev = None hashString = [0] * 26 for i in s: hashString[ord(i) - ord("a")] += 1 hashAnagram = [0] * 26 length = len(s) while length and ptr2: hashAnagram[ord(ptr2.data) - ord("a")] += 1 prev = ptr2 ptr2 = ptr2.next length -= 1 ptr2 = prev while ptr2: if hashString == hashAnagram: ans.append(ptr1) ptr1 = ptr2.next ptr2.next = None ptr2 = ptr1 hashAnagram = [0] * 26 length = len(s) prev = None while length and ptr2: hashAnagram[ord(ptr2.data) - ord("a")] += 1 prev = ptr2 ptr2 = ptr2.next length -= 1 ptr2 = prev else: hashAnagram[ord(ptr1.data) - ord("a")] -= 1 ptr1 = ptr1.next prev = ptr2 ptr2 = ptr2.next if ptr2: hashAnagram[ord(ptr2.data) - ord("a")] += 1 if hashString == hashAnagram: ans.append(ptr1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): lengthS = len(s) s = sorted(s) data = [] while head != None: data.append(head.data) head = head.next lengthH = len(data) res = [] i = 0 while i <= lengthH - lengthS: if sorted(data[i : i + lengthS]) == s: join = " ".join(data[i : i + lengthS]) jn = Node() jn.data = join res.append(jn) i += lengthS continue i += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): sublists = [] lowercase = "abcdefghijklmnopqrstuvwxyz" character_counts = {character: (0) for character in lowercase} character_counts_desired = {character: (0) for character in lowercase} for character in s: character_counts_desired[character] += 1 index = anagram_head = 0 current_node = head for next_node in range(len(s) - 1): if not current_node: break character_counts[current_node.data] += 1 current_node = current_node.next shift = 0 previous_character = "" current_start = head while current_node: character_counts[current_node.data] += 1 if previous_character: character_counts[previous_character] -= 1 if shift: shift -= 1 elif character_counts == character_counts_desired: sublists.append(current_start) shift = len(s) - 1 previous_character = current_start.data current_start = current_start.next current_node = current_node.next for node in sublists: current_node = node for next_nodes in range(len(s) - 1): current_node = current_node.next current_node.next = None return sublists
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): anagrams = [] node = False num = 0 l = [] temp_annagram = False skip_ahead = 0 while head is not None: temp_s = [i for i in s] if head.data in s: l.append(head.data) temp_s.remove(head.data) current_node = Node() current_node.data = head.data current_node.next = head.next temp_annagram = current_node next_node = current_node.next for _ in range(len(s)): if next_node is not None: new_node = Node() new_node.data = next_node.data new_node.next = next_node.next current_node.next = new_node current_node = new_node if next_node.data not in temp_s: l = [] temp_annagram = False break else: l.append(next_node.data) temp_s.remove(next_node.data) else: l = [] break if len(s) == len(l): current_node.next = None anagrams.append(temp_annagram) skip_ahead = len(s) - 1 l = [] break next_node = next_node.next if skip_ahead: while skip_ahead > 0: head = head.next skip_ahead -= 1 head = head.next return anagrams
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR IF VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): output = [] n = len(s) s = list(s) s.sort() while head != None: if head.data in s: cur = head i = 0 ss = [] pre = cur while i < n and cur != None: ss.append(cur.data) pre = cur cur = cur.next i += 1 if sorted(ss) == s: output.append(head) head = cur pre.next = None else: head = head.next else: head = head.next return output
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): mem = {} for letter in s: if letter not in mem: mem[letter] = 0 mem[letter] += 1 node = head ans = [] while node is not None: currHead = node anchor = node prev = node newMem = mem.copy() i = 0 nonLetter = False while currHead is not None and i < len(s): if currHead.data in newMem: newMem[currHead.data] -= 1 else: nonLetter = True break prev = currHead currHead = currHead.next i += 1 if nonLetter: node = currHead.next elif self.isAnagram(newMem): ans.append(anchor) prev.next = None node = currHead else: node = node.next return ans def isAnagram(self, dic): for value in dic.values(): if value != 0: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NONE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left. Example 1: Input: a -> b -> c -> a -> d -> b -> c -> a S = bac Output: [a -> b -> c, b -> c -> a] Explanation: In the given linked list, there are three anagrams: 1. a -> b -> c -> a -> d -> b -> c -> a 2. a -> b -> c -> a -> d -> b -> c -> a 3. a -> b -> c -> a -> d -> b -> c -> a But in 1 and 2, a -> b -> c and b -> c-> a are ovelapping.So we take a -> b -> c as it comes first from left.So the output is: [a->b->c,b->c->a] Example 2: Input: a -> b -> d -> c -> a S = bac Output: -1 Explanation: There is no anagrams, so output is -1 Your Task: You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty. Expected Time Complexity: O(N), where N is length of LinkedList Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= |S| <= 10^{5}
class Solution: def findAnagrams(self, head, s): def isanagram(curr, need): for k, v in need.items(): if k not in curr or curr[k] != need[k]: return False return True curr = {} need = {} n = len(s) for ch in s: need[ch] = 1 + need.get(ch, 0) p1 = p2 = head prev = None i = 0 while i < n and p2 != None: curr[p2.data] = 1 + curr.get(p2.data, 0) i += 1 prev = p2 p2 = p2.next ans = [] p2 = prev while p2.next != None: if isanagram(curr, need): ans.append(p1) t = p2 p2 = p2.next t.next = None p1 = p2 prev = None curr.clear() i = 0 while i < n and p2 != None: curr[p2.data] = 1 + curr.get(p2.data, 0) i += 1 prev = p2 p2 = p2.next p2 = prev else: curr[p1.data] -= 1 p1 = p1.next p2 = p2.next curr[p2.data] = 1 + curr.get(p2.data, 0) if isanagram(curr, need): ans.append(p1) return ans
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = 0 S = list(S) dic = {} for i in S: if i not in dic.keys(): dic[i] = 1 else: dic[i] += 1 s = 0 e = len(S) - 1 while s < e: if d == 0: if dic[S[s]] == 1: s += 1 else: d = 1 dic[S[s]] -= 1 S[s] = 0 s += 1 elif dic[S[e]] == 1: e -= 1 else: d = 0 dic[S[e]] -= 1 S[e] = 0 e -= 1 re = "" a = "" for i in S: if i != 0: re += i if d == 1: re = list(re) n = len(re) for i in range(n // 2): re[i], re[n - i - 1] = re[n - 1 - i], re[i] for i in re: a += i re = a return re
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dict = {} for i in S: if i in dict: dict[i] += 1 else: dict[i] = 1 s = list(S) res = set(s) flag = 0 index = 0 while len(s) != len(res): if flag == 0: if dict[s[index]] > 1: dict[s[index]] -= 1 del s[index] index = -1 flag = 1 else: index += 1 elif dict[s[index]] > 1: dict[s[index]] -= 1 del s[index] index = 0 flag = 0 else: index -= 1 result = "".join(s) if index < 0: return result[::-1] return result
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER RETURN VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dic = {} n = len(S) for i in S: if i in dic: dic[i] += 1 else: dic[i] = 1 start = 0 stop = n - 1 rev = False st1 = "" st2 = "" while start <= stop: if rev == False: if dic[S[start]] == 1: st1 += S[start] start += 1 else: dic[S[start]] -= 1 start += 1 rev = True elif dic[S[stop]] == 1: st2 = S[stop] + st2 stop -= 1 else: dic[S[stop]] -= 1 stop -= 1 rev = False st1 = st1 + st2 if rev == True: st1 = reversed(st1) ans = "" for i in st1: if i != "@": ans = ans + i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, x): def dmax(d): m = 0 for p in d: if d[p] > m: m = d[p] return m S = [p for p in x] d = {} for i in range(len(S)): if S[i] not in d: d[S[i]] = 1 else: d[S[i]] += 1 k = 0 while 1: for i in range(len(S)): if d[S[i]] > 1: d[S[i]] -= 1 S.pop(i) k += 1 break if dmax(d) == 1: if k % 2: return "".join(S)[::-1] else: return "".join(S) for i in range(len(S) - 1, -1, -1): if d[S[i]] > 1: d[S[i]] -= 1 S.pop(i) k += 1 break if dmax(d) == 1: if k % 2: return "".join(S)[::-1] else: return "".join(S)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR NUMBER RETURN FUNC_CALL STRING VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): K = 26 N = len(S) dp = [(0) for i in range(K)] mark = [] for s in S: dp[ord(s) - ord("a")] += 1 mark.append(1) c = len([i for i in dp if i > 1]) i, j = 0, len(S) - 1 op = 1 while c > 0: if op == 1: while i < j and dp[ord(S[i]) - ord("a")] <= 1: i += 1 dp[ord(S[i]) - ord("a")] -= 1 if dp[ord(S[i]) - ord("a")] == 1: c -= 1 mark[i] = 0 i += 1 op = 0 else: while i < j and dp[ord(S[j]) - ord("a")] <= 1: j -= 1 dp[ord(S[j]) - ord("a")] -= 1 if dp[ord(S[j]) - ord("a")] == 1: c -= 1 mark[j] = 0 j -= 1 op = 1 s = "" if op: for x in range(N): if mark[x]: s += S[x] else: for x in range(N - 1, -1, -1): if mark[x]: s += S[x] return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): ans = "" n = len(S) q = set() m = {} p = {} for i in range(n): if S[i] not in m: m[S[i]] = 0 m[S[i]] += 1 p[i] = 1 i = 0 j = n - 1 k = i r = 0 while i < n and j >= 0 and i <= j: if m[S[k]] > 1: m[S[k]] -= 1 p[k] = 0 if r: j -= 1 k = i r = 0 else: i += 1 k = j r = 1 elif r: k -= 1 j -= 1 else: i += 1 k += 1 for i in range(n): if p[i] > 0: ans += S[i] if r: ans = ans[::-1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = {} repeated = [] for i, c in enumerate(S): if c not in d: d[c] = 0 d[c] += 1 for i, c in enumerate(S): if d[c] > 1: repeated.append(i) i, j = 0, len(repeated) - 1 f = 0 removed = [(0) for i in range(len(S))] while i <= j: if f == 0: idx = repeated[i] if d[S[idx]] > 1: d[S[idx]] -= 1 removed[idx] = 1 f = 1 - f i += 1 else: idx = repeated[j] if d[S[idx]] > 1: d[S[idx]] -= 1 removed[idx] = 1 f = 1 - f j -= 1 ans = [S[i] for i in range(len(S)) if removed[i] == 0] if f: ans = ans[::-1] return "".join(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL STRING VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, s): r = [] l = [0] * 26 for i in s: l[ord(i) % 97] = l[ord(i) % 97] + 1 end = len(s) - 1 start = 0 d = 0 while end != start: if d == 0: if l[ord(s[start]) % 97] > 1: r.append(start) l[ord(s[start]) % 97] = l[ord(s[start]) % 97] - 1 d = 1 start = start + 1 else: if l[ord(s[end]) % 97] > 1: r.append(end) l[ord(s[end]) % 97] = l[ord(s[end]) % 97] - 1 d = 0 end = end - 1 s = list(s) r = sorted(r)[::-1] for i in r: s.pop(i) st = "" for i in s: st = st + i if d == 0: return st return st[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): i, j = 0, len(S) - 1 _map = [0] * 26 flag = True cnt = 0 for ch in S: _map[ord(ch) - ord("a")] += 1 begin, end = "", "" while i <= j: if flag: ch = S[i] i += 1 else: ch = S[j] j -= 1 if _map[ord(ch) - ord("a")] > 1: _map[ord(ch) - ord("a")] -= 1 cnt += 1 flag = not flag elif flag: begin += ch else: end = ch + end ans = begin + end if cnt % 2 == 1: ans = ans[::-1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR STRING STRING WHILE VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, s): n = len(s) count = dict() for i in range(len(s)): count[s[i]] = count.get(s[i], 0) + 1 chars_size = len(count) sens = 1 while chars_size < n: for i in range( (n - 1) * (1 - sens) // 2, n * (1 + sens) // 2 - (1 - sens) // 2, sens ): if count[s[i]] > 1: count[s[i]] = count[s[i]] - 1 s = s[:i] + s[i + 1 :] n = n - 1 sens = -sens break final_chars = "" for i in range( (n - 1) * (1 - sens) // 2, n * (1 + sens) // 2 - (1 - sens) // 2, sens ): final_chars = final_chars + s[i] return final_chars
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dic = {} n = len(S) s = list(S) for i in S: if i in dic: dic[i] += 1 else: dic[i] = 1 i = 0 j = n - 1 dir = 1 while i <= j: if dir == 1: if dic[S[i]] == 1: i += 1 else: dic[S[i]] -= 1 s[i] = "@" i += 1 dir = -1 elif dic[S[j]] == 1: j -= 1 else: dic[S[j]] -= 1 s[j] = "@" j -= 1 dir = 1 ans = "" for i in s: if i != "@": ans = ans + i if dir == -1: return ans[::-1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): s = [] s[:0] = S freq = [0] * 26 for i in s: freq[ord(i) - ord("a")] += 1 l = 0 r = len(s) - 1 f = 0 while l <= r: if f == 0: if freq[ord(s[l]) - ord("a")] == 1: l += 1 else: freq[ord(s[l]) - ord("a")] -= 1 s[l] = "#" l += 1 f ^= 1 elif freq[ord(s[r]) - ord("a")] == 1: r -= 1 else: freq[ord(s[r]) - ord("a")] -= 1 s[r] = "#" r -= 1 f ^= 1 if f == 1: s.reverse() ans = "" for i in s: if i != "#": ans += i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = {} for i in S: if i in d: d[i] += 1 else: d[i] = 1 return forward(S, 0, len(S) - 1, d) def forward(S, i, j, d): cnt = 0 beg = "" end = "" ans = "" flag = True i, j = 0, len(S) - 1 while i <= j: if flag: ch = S[i] else: ch = S[j] if d[ch] > 1: d[ch] -= 1 cnt += 1 if flag: flag = False i += 1 else: flag = True j -= 1 elif flag: beg += S[i] i += 1 else: end = S[j] + end j -= 1 ans = beg + end if cnt % 2 == 1: return ans[::-1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): freq_dict = self.findFreaquency(S) string_len = len(S) iterator_fwd = 0 iterator_bkd = string_len - 1 reverse_direction = "fwd" while iterator_fwd != iterator_bkd: if reverse_direction == "fwd": iterator = iterator_fwd else: iterator = iterator_bkd ch = S[iterator] if freq_dict[ch]["freq"] > 1: S = S[:iterator] + S[iterator + 1 :] freq_dict[ch]["freq"] -= 1 iterator_bkd -= 1 reverse_direction = "bkd" if reverse_direction == "fwd" else "fwd" elif reverse_direction == "fwd": iterator_fwd += 1 else: iterator_bkd -= 1 return S if reverse_direction == "fwd" else self.reverse_string(S) def findFreaquency(self, S): freq_object = {} for index, char in enumerate(S): if char in freq_object.keys(): freq_object[char]["freq"] += 1 freq_object[char]["index_list"].append(index) else: freq_object[char] = {"freq": 1, "index_list": [index]} return freq_object def reverse_string(self, S): return S[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING NUMBER VAR NUMBER ASSIGN VAR VAR STRING STRING STRING IF VAR STRING VAR NUMBER VAR NUMBER RETURN VAR STRING VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR VAR DICT STRING STRING NUMBER LIST VAR RETURN VAR FUNC_DEF RETURN VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, s): l = len(s) s = list(s) m = {} for c in s: if c not in m: m[c] = 1 else: m[c] += 1 i, j = 0, l - 1 while i <= j: while i < len(s) and m[s[i]] == 1: i += 1 if i < len(s): m[s[i]] -= 1 s.pop(i) j -= 1 while j >= 0 and m[s[j]] == 1: j -= 1 if j >= 0: m[s[j]] -= 1 s.pop(j) j -= 1 if (l - len(s)) % 2 == 0: return "".join(s) else: return "".join(s[::-1])
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL STRING VAR RETURN FUNC_CALL STRING VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): mapVal = {} for letter in S: mapVal[letter] = mapVal.get(letter, 0) + 1 stringar = list(S) rev = False i = 0 j = len(stringar) - 1 while i < j: if rev == False: if mapVal[stringar[i]] > 1: mapVal[stringar[i]] -= 1 stringar.pop(i) rev = not rev j = len(stringar) - 1 else: i += 1 elif mapVal[stringar[j]] > 1: mapVal[stringar[j]] -= 1 stringar.pop(j) rev = not rev else: j -= 1 if rev: stringar = stringar[::-1] return "".join(stringar)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL STRING VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = {} for ch in S: if ch not in d: d[ch] = 1 else: d[ch] += 1 flag = 0 st = 0 ed = len(S) - 1 while st <= ed: if flag == 0: if d[S[st]] == 1: st += 1 else: d[S[st]] -= 1 S = S[:st] + "#" + S[st + 1 :] st += 1 flag = 1 elif d[S[ed]] == 1: ed -= 1 else: d[S[ed]] -= 1 S = S[:ed] + "#" + S[ed + 1 :] ed -= 1 flag = 0 if flag == 1: S = S[::-1] S = S.replace("#", "") return S
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): rem = dict() h_map = dict() n = len(S) for i in range(0, n): if S[i] not in h_map: h_map[S[i]] = 0 h_map[S[i]] += 1 start = 0 end = n - 1 front = True while start < end: if front and h_map[S[start]] > 1: front = False rem[start] = 1 h_map[S[start]] -= 1 start += 1 elif not front and h_map[S[end]] > 1: front = True rem[end] = 1 h_map[S[end]] -= 1 end -= 1 elif front: start += 1 else: end -= 1 new_str = "" for i in range(0, n): if i not in rem: new_str += S[i] if front: return new_str else: return new_str[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR IF VAR RETURN VAR RETURN VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): S = list(S) dict1 = dict() i = 0 j = len(S) - 1 nd = "" nd2 = "" beg = True for i in S: dict1.setdefault(i, 0) dict1[i] += 1 i = 0 j = len(S) - 1 cnt = 0 while i <= j: if beg: if dict1[S[i]] > 1: dict1[S[i]] -= 1 i += 1 beg = False cnt += 1 else: nd += S[i] i += 1 elif dict1[S[j]] > 1: dict1[S[j]] -= 1 j -= 1 beg = True cnt += 1 else: nd2 = S[j] + nd2 j -= 1 return (nd + nd2)[::-1] if cnt % 2 else nd + nd2
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = {} for i in S: d[i] = d.get(i, 0) + 1 n = len(S) l = list(S) i, j = 0, n - 1 flag = True while i <= j: if flag: if d[l[i]] > 1: d[l[i]] -= 1 l[i] = "" flag = False i += 1 else: if d[l[j]] > 1: d[l[j]] -= 1 l[j] = "" flag = True j -= 1 if flag: ans = "".join(l) else: l.reverse() ans = "".join(l) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): L = len(S) D = True start = True while D: newstr = self.remRev(S, L, start) S = newstr[0] D = newstr[1] start = not start L -= 1 if start: return S[::-1] return S def remRev(self, S, L, start): if start: for i in range(L): for j in range(L - 1, i, -1): if S[i] == S[j]: return S[:i] + S[i + 1 :], True return S, False else: for i in range(L - 1, -1, -1): for j in range(i): if S[i] == S[j]: return S[:i] + S[i + 1 :], True return S, False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR RETURN VAR NUMBER RETURN VAR FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): count = [0] * 26 for i in S: count[ord(i) - 97] += 1 left = 0 right = len(S) - 1 rev = False while left <= right: if rev: if count[ord(S[right]) - 97] > 1: count[ord(S[right]) - 97] -= 1 rev = False S = S[:right] + S[right + 1 :] right -= 1 else: if count[ord(S[left]) - 97] > 1: count[ord(S[left]) - 97] -= 1 rev = True S = S[:left] + S[left + 1 :] left -= 1 right -= 1 left += 1 if rev: return S[::-1] else: return S
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR RETURN VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): mp = {} for i in S: if i not in mp: mp[i] = 1 else: mp[i] += 1 S = list(S) flag = True i, j = 0, len(S) - 1 while i <= j: if flag: if mp[S[i]] > 1: mp[S[i]] -= 1 S[i] = "0" flag = False i += 1 else: if mp[S[j]] > 1: mp[S[j]] -= 1 S[j] = "0" flag = True j -= 1 ans = "" for i in S: if i != "0": ans += i if flag: return ans else: return ans[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR IF VAR RETURN VAR RETURN VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, s): d = {} n = len(s) for i in s: if i in d: d[i] += 1 else: d[i] = 1 i = 0 j = n - 1 k = -1 while i < j: if n == 1: break while i < n and i < j and d[s[i]] == 1: i += 1 if i >= n - 1 or i >= j: break else: d[s[i]] -= 1 s = s[0:i] + s[i + 1 :] j -= 1 n -= 1 while j > i and j >= 0 and d[s[j]] == 1: j -= 1 if j <= 0 or j <= i: k = 0 break else: k = -1 d[s[j]] -= 1 s = s[0:j] + s[j + 1 :] n -= 1 j -= 1 if k == 0: s = s[-1::-1] return s
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): D = {} S = list(S) for i in range(len(S)): if S[i] not in D.keys(): D[S[i]] = 1 else: D[S[i]] += 1 l = 0 r = len(S) - 1 flag = 0 while l <= r: if flag == 0: if D[S[l]] == 1: l += 1 else: D[S[l]] -= 1 S[l] = "#" l += 1 flag = 1 elif D[S[r]] == 1: r -= 1 else: D[S[r]] -= 1 S[r] = "#" r -= 1 flag = 0 D = "" if flag == 1: S = S[::-1] for i in range(len(S)): if S[i] != "#": D += S[i] return D
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dic = {} for c in S: if c not in dic: dic[c] = 1 else: dic[c] += 1 i = 0 d = 1 while 0 <= i < len(S): if dic[S[i]] > 1: dic[S[i]] -= 1 S = S[:i] + S[i + 1 :] if d == 1: i = len(S) - 1 else: i = 0 d *= -1 else: i += d return S if d > 0 else S[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR NUMBER VAR VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): n = len(S) freq = {} for i in range(n): if S[i] in freq.keys(): freq[S[i]] += 1 else: freq[S[i]] = 1 left, right = 0, n - 1 flag = True result = "" while left < right: if flag == True: while left < len(S) and freq[S[left]] == 1: result += S[left] left += 1 if left >= right: break freq[S[left]] -= 1 S = S[:left] + S[left + 1 :] right -= 1 flag = False else: while right >= 0 and freq[S[right]] == 1: result += S[right] right -= 1 if left >= right: break freq[S[right]] -= 1 S = S[:right] + S[right + 1 :] right -= 1 flag = True if flag == False: return S[::-1] return S
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, s): h, d = [0] * 26, 1 for i in range(len(s)): h[ord(s[i]) - 97] += 1 i = 0 while 0 <= i < len(s): if h[ord(s[i]) - 97] > 1: h[ord(s[i]) - 97] -= 1 s = s[:i] + s[i + 1 :] i = len(s) - 1 if d == 1 else 0 d *= -1 else: i += d return s if d > 0 else s[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR RETURN VAR NUMBER VAR VAR NUMBER
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dic = {} for i in S: if i in dic: dic[i] += 1 else: dic[i] = 1 i = 0 j = len(S) - 1 d = 0 S = list(S) while i < j: if d == 0: if dic[S[i]] == 1: i += 1 else: dic[S[i]] -= 1 S[i] = "@" d = 1 i += 1 if d == 1: if dic[S[j]] == 1: j -= 1 else: dic[S[j]] -= 1 S[j] = "@" d = 0 j -= 1 ans = "" for i in S: if i != "@": ans += i if d == 1: c = "" for i in reversed(ans): c += i return c return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR VAR IF VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): start = 0 last = len(S) - 1 dct = {} dct1 = {} front = 1 for i in S: dct[i] = dct.get(i, 0) + 1 while start <= last: if front: if dct[S[start]] > 1: dct[S[start]] -= 1 front = 0 else: dct1[start] = S[start] start += 1 else: if dct[S[last]] > 1: dct[S[last]] -= 1 front = 1 else: dct1[last] = S[last] last -= 1 ans = "" if front: for i in sorted(dct1.keys()): ans += dct1[i] else: for i in reversed(sorted(dct1.keys())): ans += dct1[i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): Y = [] for i in S: Y.append(i) mp1 = {} for i in S: mp1[i] = mp1.get(i, 0) + 1 i = 0 j = len(Y) - 1 d = 0 while i <= j: if d == 0: if mp1[Y[i]] == 1: i += 1 else: mp1[Y[i]] -= 1 Y[i] = "5" i += 1 d = 1 elif mp1[Y[j]] == 1: j -= 1 else: mp1[Y[j]] -= 1 Y[j] = "5" j -= 1 d = 0 xy = "" for i in range(len(S)): if Y[i] != "5": xy += Y[i] if d == 1: xy = xy[::-1] return xy
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): d = {} l = len(S) - 1 for i in S: if i in d: d[i] += 1 else: d[i] = 1 lft = 0 rht = l flag = False s1 = "" s2 = "" while lft <= rht: if flag == False: if d[S[lft]] > 1: d[S[lft]] -= 1 s1 += "*" lft += 1 flag = True else: s1 += S[lft] lft += 1 elif d[S[rht]] > 1: d[S[rht]] -= 1 s2 = "*" + s2 rht -= 1 flag = False else: s2 = S[rht] + s2 rht -= 1 s1 = s1 + s2 ans = "" if flag == True: s1 = reversed(s1) for i in s1: if i != "*": ans += i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR VAR RETURN VAR
Given a string S which consists of only lowercase English alphabets, you have to perform the below operations: If the string S contains any repeating character, remove the first repeating character and reverse the string and again perform the above operation on the modified string, otherwise, you stop. You have to find the final string. Example 1: Input: S = "abab" Output: ba Explanation: In 1st operation: The first repeating character is a. After Removing the first character, S = "bab". After Reversing the string, S = "bab". In 2nd operation: The first repeating character is b. After Removing the first character, S = "ab". After Reversing the string, S = "ba". Now the string S does not contain any repeating character. Example 2: Input: S = "dddd" Output: d Explanation: In 1st operation: The first repeating character is d. After Removing the first character, S = "ddd". After Reversing the string, S = "ddd". In 2nd operation: Similarly, S="dd". In 3rd operation: Similarly, S="d". Now the string S does not contain any repeating character. Your Task: You don't need to read input or print anything. Your task is to complete the function removeReverse( ) which accepts a string S input parameter and returns the modified string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(K), K <= 26. Constraints: The string contains only lowercase English alphabets. 1 < |S| < 10^{5} |S| denotes the length of the string S.
class Solution: def removeReverse(self, S): dic = {} for s in S: dic[s] = dic.get(s, 0) + 1 i = 0 j = len(S) - 1 cnt = 0 con = True while j >= i: if con: if dic[S[i]] > 1: dic[S[i]] -= 1 S = S[:i] + "A" + S[i + 1 :] i += 1 con = not con else: i += 1 elif dic[S[j]] > 1: dic[S[j]] -= 1 S = S[:j] + "A" + S[j + 1 :] j -= 1 con = not con else: j -= 1 S = S.replace("A", "") if not con: S = S[::-1] return S
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING IF VAR ASSIGN VAR VAR NUMBER RETURN VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input().strip(" ")) for i in range(t): n = int(input().strip(" ")) a = input().strip(" ") b = input().strip(" ") if a == b: print(0) continue s = [] x = 0 for i in range(n - 1): if a[i] != a[i + 1]: s.append(str(i + 1)) x = x + 1 i = n - 2 curr = a[-1] if a[-1] != b[-1]: s.append(str(n)) x = x + 1 while i >= 0: if b[i] != b[i + 1]: s.append(str(i + 1)) x = x + 1 i = i - 1 print(str(x) + " " + " ".join(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) while t != 0: n = int(input()) a = list(input()) b = list(input()) ans = [] st = 0 en = n - 1 inv_f = drn_f = 0 count = 0 j = n - 1 while count < n: count += 1 if inv_f == 0: if drn_f == 0: if a[en] == b[j]: en -= 1 j -= 1 pass elif a[st] != b[j]: ans.append(j) st += 1 inv_f = 1 drn_f = 1 j -= 1 else: ans.append(0) ans.append(j) st += 1 inv_f = 1 drn_f = 1 j -= 1 elif a[st] == b[j]: st += 1 j -= 1 elif a[en] != b[j]: ans.append(j) en -= 1 j -= 1 inv_f = 1 drn_f = 0 else: ans.append(0) ans.append(j) en -= 1 j -= 1 inv_f = 1 drn_f = 0 elif drn_f == 0: if a[en] != b[j]: en -= 1 j -= 1 pass elif a[st] == b[j]: ans.append(j) st += 1 inv_f = 0 drn_f = 1 j -= 1 else: ans.append(0) ans.append(j) st += 1 inv_f = 0 drn_f = 1 j -= 1 elif a[st] != b[j]: st += 1 j -= 1 elif a[en] == b[j]: ans.append(j) en -= 1 j -= 1 inv_f = 0 drn_f = 0 else: ans.append(0) ans.append(j) en -= 1 j -= 1 inv_f = 0 drn_f = 0 print(len(ans), end=" ") for i in range(len(ans)): print(ans[i] + 1, end=" ") print() 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in list(input())[:n]] b = [int(i) for i in list(input())[:n]] ans = [] c = [] for i in range(1, n): if a[i] != a[i - 1]: ans.append(i) if a[-1] != b[-1]: ans.append(n) for i in range(1, n): if b[i] != b[i - 1]: c.append(i) ans = ans + c[::-1] print(len(ans), " ".join([str(i) for i in ans]))
IMPORT ASSIGN VAR VAR 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 VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): l = int(input()) a = input().rstrip() b = input().rstrip() ans = [] c = 0 if a[0] == "1": c += 1 for i in range(len(a) - 1): if a[i + 1] != a[i]: ans.append(i + 1) c += 1 bns = [] d = 0 if b[0] == "1": d += 1 for i in range(len(b) - 1): if b[i + 1] != b[i]: bns.append(i + 1) d += 1 if c % 2 != d % 2: ans += [l] f = ans + bns[::-1] print(len(f), *f)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR LIST VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 for _ in range(INT()): N = INT() S = list(map(int, input())) T = list(map(int, input())) ans = [] l = 0 r = N - 1 flip = 0 rev = 0 for i in range(N - 1, -1, -1): if not rev: if S[l] ^ flip != T[i]: ans.append(i + 1) flip ^= 1 rev ^= 1 l += 1 else: if l >= r: break ans.append(1) S[l] ^= 1 ans.append(i + 1) flip ^= 1 rev ^= 1 l += 1 elif S[r] ^ flip != T[i]: ans.append(i + 1) flip ^= 1 rev ^= 1 r -= 1 else: if l >= r: break ans.append(1) S[r] ^= 1 ans.append(i + 1) flip ^= 1 rev ^= 1 r -= 1 ans.insert(0, len(ans)) print(*ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for t in range(int(input())): n = int(input()) a = list(map(int, list(input()))) b = list(map(int, list(input()))) index = n - 1 move = 0 hist = [] flipped, reverse = False, False start, end = 0, n - 1 while index > -1: if index == 0: if a[end] == b[index] and not flipped or a[end] != b[index] and flipped: pass else: move = move + 1 hist.append(1) index = index - 1 elif a[end] == b[index] and not flipped or a[end] != b[index] and flipped: index = index - 1 if start < end: end = end - 1 else: end = end + 1 elif a[start] == b[index] and not flipped or a[start] != b[index] and flipped: move = move + 1 hist.append(1) a[start] = 1 - a[start] else: move = move + 1 hist.append(abs(start - end) + 1) if start < end: start, end = end, start + 1 else: start, end = end, start - 1 if flipped: flipped = False else: flipped = True index = index - 1 print(move, " ".join(list(map(str, hist))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() opa, opb = [], [] for i in range(1, n): if a[i] != a[i - 1]: opa.append(i) if a[-1] != b[-1]: opa.append(n) for i in range(1, n): if b[i] != b[i - 1]: opb.append(i) print(len(opa) + len(opb), *(opa + opb[::-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() ans = [] f = [0] * n g = [0] * n k1 = k2 = 0 for i in range(1, n): if a[i] != a[i - 1]: f[k1] = i k1 += 1 for i in range(1, n): if b[i] != b[i - 1]: g[k2] = i k2 += 1 if a[n - 1] != b[n - 1]: f[k1] = n k1 += 1 print(k1 + k2, end=" ") for i in range(k1): print(f[i], end=" ") for i in range(k2 - 1, -1, -1): print(g[i], end=" ") print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def gift(): for _ in range(t): n = int(input()) s1 = list(input()) s2 = list(input()) res = [] last = s1[0] for i in range(1, n): if s1[i] != last: res.append(i) last = s1[i] for i in range(n - 1, -1, -1): if s2[i] != last: res.append(i + 1) last = s2[i] yield " ".join(str(x) for x in [len(res)] + res) t = int(input()) ans = gift() print(*ans, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP LIST FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys t = int(input()) for _ in range(t): n = int(input()) tempa = input() tempb = input() a = [] for i in tempa: a.append(int(i)) b = [] for i in tempb: b.append(int(i)) count = 0 anslist = [] start = 1 rev = 0 for i in range(n - 1, -1, -1): if rev == 0: pos = start + i else: pos = start - i curr = a[pos - 1] if rev == 1: curr = curr ^ 1 if curr != b[i]: first = a[start - 1] if rev == 1: first = first ^ 1 if first != b[i]: anslist.append(i + 1) count += 1 else: anslist.append(1) anslist.append(i + 1) count += 2 rev = rev ^ 1 start = pos print(count, end=" ") for i in anslist: sys.stdout.write(str(i) + " ") print()
IMPORT 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for i2 in range(t): n = int(input()) l1 = input() l2 = input() res = [] pr = l1[0] d_pr = 1 for i in range(1, n): if l1[i] != pr: res += [d_pr] pr = l1[i] d_pr += 1 for i in range(n - 1, -1, -1): if l2[i] != pr: res += [d_pr] pr = l2[i] d_pr -= 1 print(len(res), *res)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) A = input() B = input() A = [int(i) for i in A] B = [int(i) for i in B] k = 0 ans = [] cnt = n - 1 fp = False for i in range(n - 1, -1, -1): if fp: if A[cnt - i] != B[i]: continue elif A[cnt] == A[cnt - i]: k += 1 ans.append(i) fp = False cnt -= 1 else: k += 1 ans.append(0) if i > 0: k += 1 ans.append(i) fp = False cnt -= 1 elif A[cnt] == B[i]: cnt -= 1 continue elif A[cnt] == A[cnt - i]: k += 1 ans.append(i) fp = True else: k += 1 ans.append(0) if i > 0: k += 1 ans.append(i) fp = True print(k, end=" ") for i in ans: print(i + 1, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR IF VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = input() b = input() pos1 = 0 ans = [] pos2 = n - 1 rev = 0 for i in range(n - 1, -1, -1): if rev == 0: if b[i] == a[pos1]: ans.append(1) ans.append(i + 1) rev = 1 pos1 += 1 else: if b[i] != a[pos2]: ans.append(1) ans.append(i + 1) rev = 0 pos2 -= 1 print(len(ans), end=" ") for num in ans: print(num, 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input())) b = list(map(int, input())) ops = [] inv = False for i in range(n): if i % 2 == 0: first = a[i // 2] else: first = a[n - 1 - i // 2] if first ^ inv == b[n - 1 - i]: ops.append(1) ops.append(n - i) inv = 1 - inv print(len(ops), *ops)
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 FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) while t: n = int(input()) s1 = input() s2 = input() dif = [] flag = False for i in range(n - 1): if s1[i] != s1[i + 1]: dif.append(i) if s1[n - 1] == "1": dif.append(n - 1) flag = False for i in range(n - 1, -1, -1): if s2[i] == "1" and flag == False: flag = True dif.append(i) elif s2[i] == "0" and flag == True: flag = False dif.append(i) print(len(dif), end=" ") for i in dif: print(i + 1, end=" ") print() 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = input().rstrip() b = input().rstrip() out = [] for i in range(n - 1): if i % 2 == 0: if b[n - i - 1] == a[i // 2]: out.append(1) out.append(n - i) else: out.append(n - i) elif b[n - i - 1] != a[n - i // 2 - 1]: out.append(1) out.append(n - i) else: out.append(n - i) if (n - 1) % 2 == 0: if a[(n - 1) // 2] != b[0]: out.append(1) elif a[n - (n - 1) // 2 - 1] == b[0]: out.append(1) print(len(out), *out)
ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
cases = int(input()) for t in range(cases): n = int(input()) a = input() b = input() s = 0 out = [] temp = [] for i in range(n - 1): if a[i] != a[i + 1]: out.append(i + 1) s += 1 if b[i] != b[i + 1]: temp.append(i + 1) s += 1 if a[-1] == "1": out.append(n) s += 1 if b[-1] == "1": temp.append(n) s += 1 print(s, *(out + temp[::-1]))
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() A = list(a) ans = [] for i in range(n - 1): if a[i] != a[i + 1]: ans.append(i + 1) if A[0] == "1": A[0] = "0" else: A[0] = "1" for j in range(n - 1, -1, -1): if b[j] != A[0]: ans.append(j + 1) if A[0] == "1": A[0] = "0" else: A[0] = "1" print(len(ans), *ans, sep=" ")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) l = [] for i in range(t): n = int(input()) a = list(map(int, list(input()))) b = list(map(int, list(input()))) s = [] k = n - 1 p = 0 d = a[0] count = 0 while k != -1: if k == 0: if d == b[0]: break else: s.append(1) p = p + 1 k = k - 1 elif d == b[k]: d = (b[k] + 1) % 2 s.append(1) p = p + 1 count = count + 1 if count % 2 == 0: d = (a[count // 2] + count) % 2 else: d = (a[n - count // 2 - 1] + count) % 2 s.append(k + 1) k = k - 1 else: count = count + 1 if count % 2 == 0: d = (a[count // 2] + count) % 2 else: d = (a[n - count // 2 - 1] + count) % 2 s.append(k + 1) k = k - 1 s.insert(0, count + p) l.append(s) for i in l: for j in i: print(j, end=" ") print()
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 FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = input() b = input() c = 0 va = [] vb = [] if a == b: print(0) else: for i in range(1, n): if a[i] != a[i - 1]: c += 1 va.append(str(i)) if a[n - 1] != b[n - 1]: c += 1 va.append(str(n)) for i in range(1, n): if b[i] != b[i - 1]: c += 1 vb.append(str(i)) print(" ".join([str(len(va) + len(vb))] + va + vb[::-1]))
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() ans1 = [] for i in range(n - 1): if a[i] != a[i + 1]: ans1.append(i + 1) if a[-1] == "1": ans1.append(n) ans2 = [] for i in range(n - 1): if b[i] != b[i + 1]: ans2.append(i + 1) if b[-1] == "1": ans2.append(n) ans1 += ans2[::-1] print(len(ans1), *ans1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def trans1(v): q, w = 0, [] prev = None for i, p in enumerate(v): if prev is not None and prev != p: w.append(i) q += 1 prev = p if v[-1] == "1": w.append(len(v)) q += 1 return q, w def trans2(v): q, w = 0, [] c = 0 for i, p in enumerate(v[::-1]): if c != int(p): w.append(len(v) - i) q += 1 c = 1 - c return q, w for t in range(int(input())): n = int(input()) a, b = input(), input() q1, w1 = trans1(a) q2, w2 = trans2(b) for x in [q1 + q2] + w1 + w2: print(x, end=" ") print()
FUNC_DEF ASSIGN VAR VAR NUMBER LIST ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR BIN_OP BIN_OP LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = list(input()) b = list(input()) order = [] i = 0 current = a[0] while i < n: if a[i] != current: order.append(i) current = a[i] i += 1 if a[-1] == "1": order.append(n) i = n - 1 current = b[-1] if current == "1": order.append(n) while i >= 0: if b[i] != current: order.append(i + 1) current = b[i] i -= 1 print(len(order), *order)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def flip(a, size): res = [] for i in range(0, size): if a[i] == "1": res.append("0") else: res.append("1") return res for _ in range(0, int(input())): n = int(input()) a = list(input().strip()) b = list(input().strip()) resa = [] resb = [] size = n for i in range(0, n - 1): if a[i] == "1" and a[i + 1] == "0" or a[i] == "0" and a[i + 1] == "1": resa.append(i + 1) if a[-1] == "1": resa.append(n) for i in range(0, n - 1): if b[i] == "1" and b[i + 1] == "0" or b[i] == "0" and b[i + 1] == "1": resb.append(i + 1) if b[-1] == "1": resb.append(n) resb.reverse() print(len(resa + resb), " ".join(str(x) for x in resa + resb))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() a = a + "0" ans = list() for i in range(1, n + 1): if a[i] != a[i - 1]: ans.append(i - 1) a = "" last = "0" for i in range(n - 1, -1, -1): if b[i] != last: ans.append(i) if last == "0": last = "1" else: last = "0" print(len(ans), end=" ") for i in ans: print(i + 1, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() + "0" b = input() + "0" ans = [] for i in range(n): if a[i] != a[i + 1]: ans.append(i + 1) for i in range(n - 1, -1, -1): if b[i] != b[i + 1]: ans.append(i + 1) print(len(ans), *ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def helper(s): ans = [] for i in range(1, len(s)): if s[i] != s[i - 1]: ans.append(i - 1) if s[-1] == "1": ans.append(len(s) - 1) return ans t = int(input()) for l in range(t): n = int(input()) a = input() b = input() a1 = helper(a) a2 = helper(b) a = a1 + a2[::-1] print(len(a), end=" ") for i in a: print(i + 1, end=" ") print()
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in input()] + [0] b = [int(i) for i in input()] + [0] op1 = [] op2 = [] for i in range(1, n + 1): if a[i] != a[i - 1]: op1.append(i) if b[i] != b[i - 1]: op2.append(i) op = op1 + op2[::-1] print(len(op), *op)
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 VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = list(input()) b = list(input()) k = 0 l = [] c = a[n - 1] for i in range(n - 1): if a[i] != a[i + 1]: k += 1 l.append(i + 1) for i in range(n - 1, -1, -1): if c != b[i]: c = b[i] k += 1 l.append(i + 1) print(k, end=" ") for i in range(len(l)): print(l[i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys fast_reader = sys.stdin.readline fast_writer = sys.stdout.write def input(): return fast_reader().strip() def print(*argv): fast_writer(" ".join(str(i) for i in argv)) fast_writer("\n") for _ in range(int(input())): n = int(input()) a = list(input()) b = list(input()) if a == b: print(0) continue l = a[:] curr = 0 m = [] for i in range(n): if l[i] == "0": if curr != 0: if i == curr: m.append(i) else: m.append(i - curr) m.append(i) curr = 0 else: curr += 1 if curr != 0: if n == curr: m.append(n) else: m.append(n - curr) m.append(n) temp = ["0" for i in range(n)] i = n - 1 while i >= 0: if temp[i] != b[i]: j = i curr = 0 while j >= 0: if temp[j] != b[j]: curr += 1 j -= 1 else: break if j == -1: m.append(curr) else: m.append(i + 1) m.append(j + 1) i = j i -= 1 print(len(m), *m)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) ans = [] a, b = list(map(int, list(input()))), list(map(int, list(input()))) i, j = 0, n - 1 f = 1 while n > 0: n -= 1 f ^= 1 if f == 0: if b[n] == a[i]: ans.append(1) ans.append(n + 1) i += 1 else: if b[n] == a[j] ^ 1: ans.append(1) ans.append(n + 1) j -= 1 print(len(ans), *ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) while t: n = int(input()) s1 = input() s2 = input() ans = [] check = 0 b = 0 f = 0 count = 0 for i in range(n - 1, -1, -1): if check == 0: if s2[i] == s1[n - 1 - b]: b = b + 1 elif s2[i] != s1[0 + f]: check = 1 f = f + 1 ans.append(i + 1) count = count + 1 else: check = 1 f = f + 1 ans.append(1) ans.append(i + 1) count = count + 2 elif s2[i] != s1[0 + f]: f = f + 1 elif s2[i] == s1[n - 1 - b]: check = 0 b = b + 1 ans.append(i + 1) count = count + 1 else: check = 0 b = b + 1 ans.append(1) ans.append(i + 1) count = count + 2 print(count, end=" ") print(*ans) t = 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") Read = lambda: list(map(int, input().split())) Num = lambda: int(input()) exec( """try:sys.stdin=open('in.txt','r');sys.stdout=open('out.txt','w') except:pass""" ) def solve(): n = Read()[0] a = input() b = input() ans = [] for i in range(n - 1): if a[i] != a[i + 1]: ans.append(i + 1) if a[-1] != b[-1]: ans.append(n) ans2 = [] for i in range(n - 1): if b[i] != b[i + 1]: ans2.append(i + 1) ans = ans + ans2[::-1] print(len(ans), *ans) for _ in range(Num()): solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) s = input() t = input() a = [] b = [] for i in range(n - 1): if s[i] != s[i + 1]: a.append(i + 1) if t[i] != t[i + 1]: b.append(i + 1) if s[-1] != t[-1]: a.append(n) a.extend(b[::-1]) else: a.extend(b[::-1]) print(len(a), end=" ") print(*a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def tc(): n = int(input()) a = input() b = input() segs = [] prev = a[0] for i, ch in enumerate(a[1:]): if ch != prev: segs.append(i + 1) prev = ch bsegs = [] bprev = b[0] for i, ch in enumerate(b[1:]): if ch != bprev: bsegs.append(i + 1) bprev = ch if a[-1] != b[-1]: segs.append(n) segs.extend(bsegs[::-1]) print(len(segs), " ".join(map(str, segs))) T = int(input()) for _ in range(T): tc()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def cal(n, flip): if flip & 1: return n ^ 1 else: return n def cal_first(): global p1, p2 if p1 > p2: return first_arr2[p2] else: return first_arr[p1] def inc_first(): global p1, p2 if p1 == p2: p1 += 1 elif p1 < p2: p1 += 1 else: p2 += 1 for i in range(int(input())): n = int(input()) s1 = [int(i) for i in input()] s2 = [int(i) for i in input()] flip = 0 ans = [] first_arr = list(range(n)) first_arr2 = list(range(n))[::-1] p1 = 0 p2 = 0 for ind in range(len(s2) - 1, -1, -1): first = cal_first() if cal(s1[first], flip) == s2[ind]: ans.append(1) s1[first] ^= 1 ans.append(ind + 1) else: ans.append(ind + 1) flip += 1 inc_first() if ans: print(len(ans), end=" ") print(*ans) else: print(0)
FUNC_DEF IF BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER 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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def solution(): for _ in range(int(input())): n = int(input()) a = list(input()) b = list(input()) a += ["0"] b += ["0"] ans = [] rev = [] x = "0" for i in range(1, n + 1): if a[i] != a[i - 1]: ans.append(i) for i in range(1, n + 1): if b[i] != b[i - 1]: rev.append(i) rev.reverse() print(*([len(ans) + len(rev)] + ans + rev)) solution()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST STRING VAR LIST STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for you in range(t): n = int(input()) s = input() a = [i for i in s] b = input() arr1 = [] arr2 = [] count = 0 for i in range(n - 1): if a[i] != a[i + 1]: arr1.append(i + 1) count += 1 if a[-1] == "1": arr1.append(n) for i in range(n - 1): if b[i] != b[i + 1]: arr2.append(i + 1) count += 1 if b[-1] == "1": arr2.append(n) arr2.reverse() print(len(arr1) + len(arr2), end=" ") for i in arr1: print(i, end=" ") for i in arr2: print(i, 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 ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
from sys import stdin input = stdin.readline def answer(): ans = [] i, j = 0, n - 1 ind = n - 1 while i <= j: if a[i] == b[ind]: ans.append(1) ans.append(ind + 1) else: ans.append(ind + 1) if i == j: break ind -= 1 if a[j] ^ 1 == b[ind]: ans.append(1) ans.append(ind + 1) else: ans.append(ind + 1) ind -= 1 i, j = i + 1, j - 1 print(len(ans), *ans) for T in range(int(input())): n = int(input()) a = [int(i) for i in input().strip()] b = [int(i) for i in input().strip()] answer()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def f(a, b): if b & 1: return a ^ 1 return a for _ in range(int(input())): n = int(input()) a = list(map(int, input())) b = list(map(int, input())) s = 0 l = n - 1 c = [] i = n - 1 while i + 1: if f(a[s], n - i - 1) == b[i]: c += (1,) c += (i + 1,) sn = l ln = s + [1, -1][s > n // 2] s = sn l = ln i -= 1 print(len(c)) print(*c)
FUNC_DEF IF BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = list(str(input())) b = list(str(input())) zero = 0 one = 0 A = [] B = [] if "".join(a) == "".join(b): print(0) else: for i in range(n): if a[i] == "0": zero += 1 elif a[i] == "1": one += 1 if b[i] == "0": zero += 1 elif b[i] == "1": one += 1 if zero > one: for ii in range(n): if a[ii] == "1": if ii != 0: A.append(ii) A.append(ii + 1) if b[ii] == "1": if ii != 0: B.append(ii) B.append(ii + 1) B.reverse() A.extend(B) print(len(A), *A) else: for ii in range(n): if a[ii] == "0": if ii != 0: A.append(ii) A.append(ii + 1) if b[ii] == "0": if ii != 0: B.append(ii) B.append(ii + 1) B.reverse() A.extend(B) print(len(A), *A)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL STRING VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for i in range(t): n = int(input()) a = [int(val) for val in list(input())] b = [int(val) for val in list(input())] if a == b: print(0) else: count = 0 res = [] is_flip = False left = 0 right = n - 1 j = n - 1 a_zero = a[0] while left < right: if is_flip: curr_j = 1 - a[left] else: curr_j = a[right] if curr_j != b[j]: if is_flip: a_zero = 1 - a[right] else: a_zero = a[left] if curr_j == a_zero: count += 1 res.append(str(j + 1)) is_flip = not is_flip else: count += 2 res.append("1") if is_flip: a[right] *= -1 else: a[left] *= -1 res.append(str(j + 1)) is_flip = not is_flip if is_flip: left += 1 else: right -= 1 j -= 1 if is_flip: a_zero = 1 - a[right] else: a_zero = a[left] if a_zero != b[0]: count += 1 res.append("1") print(count, " ".join(res))
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 VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = list(input()) b = list(input()) la = [] flgA = int(a[0]) for i in range(n - 1): if a[i] != a[i + 1]: la.append(i + 1) flgA = (flgA - 1) % 2 lb = [] flgB = int(b[0]) for i in range(n - 1): if b[i] != b[i + 1]: lb.append(i + 1) flgB = (flgB - 1) % 2 if flgA != flgB: la.append(n) ans = la + list(reversed(lb)) print(len(ans), " ".join(str(n) for n in ans))
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for you in range(t): n = int(input()) a = input() ai = [i for i in a] bi = input() k = [] flips = 0 sumi = 0 z = list(ai) for i in range(n - 1, -1, -1): if i == 0: if z[0] != bi[0]: k.append(1) continue if flips % 2: curr = ai[sumi - i] if curr == "0": curr = "1" else: curr = "0" else: curr = ai[i + sumi] if curr != bi[i]: if z[0] == bi[i]: k.append(1) k.append(i + 1) if flips % 2 == 0: sumi += i else: sumi -= i flips += 1 z[0] = curr if curr == "0": z[0] = "1" else: z[0] = "0" else: k.append(i + 1) if flips % 2 == 0: sumi += i else: sumi -= i flips += 1 z[0] = curr if curr == "0": z[0] = "1" else: z[0] = "0" print(len(k), end=" ") for i in k: print(i, 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 ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
import sys input = sys.stdin.readline def pathLst(n, s): path = [] for i in range(n - 1): if s[i] != s[i + 1]: path.append(str(i + 1)) if s[-1] == "1": path.append(str(n)) return path def main(n, a, b): path = pathLst(n, a) + pathLst(n, b)[::-1] return str(len(path)) + " " + " ".join(path) for _ in range(int(input())): n = int(input()) a = input()[:-1] b = input()[:-1] print(main(n, a, b))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def f(c): if c == "1": return "0" else: return "1" for _ in range(int(input())): n = int(input()) a = list(input()) b = list(input()) l = [] i = n - 1 j = n - 1 am = True while j >= 0: if am: if a[i] != b[j]: if a[i - j] == b[j]: l.append(1) a[i - j] = f(a[i - j]) l.append(j + 1) am = False i -= j i += 1 else: i -= 1 elif a[i] == b[j]: if a[i + j] != b[j]: l.append(1) a[i + j] = f(a[i + j]) l.append(j + 1) am = True i += j i -= 1 else: i += 1 j -= 1 l1 = [len(l)] + l print(*l1)
FUNC_DEF IF VAR STRING RETURN STRING RETURN 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
T = int(input()) for _ in range(T): N = int(input()) a = [int(x) for x in input()] b = [int(x) for x in input()] ans = [] for i in range(N): x = a[N - (i + 1) // 2] ^ 1 if i % 2 else a[i // 2] if x == b[N - 1 - i]: ans.append(1) ans.append(N - i) print(len(ans), end=" ") print(" ".join(str(x) for x in ans))
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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def flip(x): if x == "1": return "0" else: return "1" t = int(input()) for i in range(t): n = int(input()) a = list(input()) b = list(input()) lst = [] k = 0 b.reverse() for j in range(1, n): if a[j] != a[j - 1]: a[0] = flip(a[0]) k = k + 1 lst.append(j) for j in range(n): if b[j] != a[0]: a[0] = flip(a[0]) k = k + 1 lst.append(n - j) print(k, end=" ") for j in lst: print(j, end=" ") print("")
FUNC_DEF IF VAR STRING RETURN STRING RETURN 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
from sys import stdin def iinput(): return int(stdin.readline()) def minput(): return map(int, stdin.readline().split()) def linput(): return list(map(int, stdin.readline().split())) t = iinput() while t: t -= 1 n = iinput() a = input() b = input() op1, op2 = [], [] for i in range(1, n): if a[i] != a[i - 1]: op1.append(i) if b[i] != b[i - 1]: op2.append(i) if a[-1] != b[-1]: op1.append(n) op1.extend(reversed(op2)) print(len(op1), *op1)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input().split()[0]) for case in range(t): n = int(input().split()[0]) a = [int(x) for x in input().split()[0]] b = [int(x) for x in input().split()[0]] l = 0 r = n - 1 last = n - 1 while r > -1: if a[r] == b[r]: r -= 1 last -= 1 else: break ans = [] flips = 0 ltor = True while l - 1 < r: if ltor: if (a[l] + flips) % 2 == b[last]: ans.append(1) ans.append(last + 1) else: ans.append(last + 1) flips += 1 l += 1 last -= 1 while l < n and (a[l] + flips) % 2 == b[last]: l += 1 last -= 1 else: if (a[r] + flips) % 2 == b[last]: ans.append(1) ans.append(last + 1) else: ans.append(last + 1) flips += 1 r -= 1 last -= 1 while r > -1 and (a[r] + flips) % 2 == b[last]: r -= 1 last -= 1 ltor = not ltor print(str(len(ans)) + " ", end="") print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) la = str(input()) lb = str(input()) ans = n l = [] a = list(la) b = list(lb) left = 0 right = n - 1 for i in range(n - 1, -1, -1): temp = n - i + 1 if temp % 2 == 0: if a[left] == b[i]: l.append(1) left += 1 l.append(i + 1) else: if a[right] != b[i]: l.append(1) right -= 1 l.append(i + 1) print(len(l), end=" ") for i in range(len(l)): print(l[i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) cnt = 0 while cnt < t: cnt += 1 n = int(input()) a = [int(i) for i in input()] b = [int(i) for i in input()] res = [] for i in range(n): if i % 2 == 0: flag = 1 else: flag = 0 if flag: if a[i // 2] == b[n - 1 - i]: res.append(1) res.append(n - i) else: if (a[n - 1 - i // 2] + 1) % 2 == b[n - 1 - i]: res.append(1) res.append(n - i) print(len(res), end=" ") if res: print(" ".join(str(q) for q in res)) else: print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() arr = [] ct = 0 pt = 0 pt2 = n - 1 for i in range(n - 1, -1, -1): if ct % 2 == 0: if b[i] != a[pt]: arr += [i + 1] else: arr += [1, i + 1] pt += 1 ct += 1 else: if b[i] == a[pt2]: arr += [i + 1] else: arr += [1, i + 1] pt2 -= 1 ct += 1 print(len(arr), *arr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def solve(s): new = "" for i in s: if i == "1": new += "0" else: new += "1" return new[::-1] for nt in range(int(input())): n = int(input()) a = input() b = input() ans = [] i = 1 count = 0 if a[0] != b[0]: ans.append(1) while i < n: if i + 1 < n: if a[i] != b[i] and a[i + 1] != b[i + 1]: if a[i] == a[i + 1]: ans.append(i + 2) ans.append(2) ans.append(i + 2) i += 2 else: if i + 2 >= n or a[i + 2] == b[i + 2]: ans.append(i + 2) ans.append(1) ans.append(2) ans.append(1) ans.append(i + 2) i += 3 continue if a[i + 2] == a[i]: ans.append(i + 3) ans.append(3) ans.append(i + 3) i += 3 else: ans.append(i + 3) ans.append(1) ans.append(3) ans.append(1) ans.append(i + 3) i += 3 elif a[i] == b[i] and a[i + 1] != b[i + 1]: i += 1 elif a[i] != b[i] and a[i + 1] == b[i + 1]: ans.append(i + 1) ans.append(1) ans.append(i + 1) i += 2 else: i += 2 else: if a[i] != b[i]: ans.append(i + 1) ans.append(1) ans.append(i + 1) break print(len(ans), end=" ") print(*ans)
FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def reverse_num(number, reversed): answer = number if reversed else (int(number) + 1) % 2 return str(answer) def main(): _ = input() a = list(input()) b = list(input()) ops = [] l, r = 0, len(a) - 1 rev = True for i in range(len(a) - 1, -1, -1): if b[i] != reverse_num(a[r], rev): if i == 0: ops.append(1) else: if reverse_num(a[r], rev) != reverse_num(a[l], rev): ops.append(1) ops.append(i + 1) if rev: l, r = r, l + 1 else: l, r = r, l - 1 rev = not rev else: r = r + (-1 if rev else 1) print(len(ops), end=" ") for op in ops: print(op, end=" ") print() t = int(input()) for _ in range(t): main()
FUNC_DEF ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
t = int(input()) for _ in range(t): n = int(input()) s = [int(i) for i in input()] t = [int(i) for i in input()] i = n - 1 res = [] a, b, d = 0, i, 1 def get(j): if d == 1: if j != 0: return s[b] else: return s[a] if d == -1: if j != 0: return s[b] ^ 1 else: return s[a] ^ 1 while i > 0: if get(i) != t[i]: if get(i) != get(0): res.append(1) res.append(i + 1) d = d * -1 a, b = b, a if d == 1: b -= 1 else: b += 1 i -= 1 if get(0) != t[0]: res.append(1) print(len(res), end=" ") for i in res: print(i, 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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() p = [] j1 = 0 j2 = n - 1 inc = -1 flip = False for i in range(n - 1, -1, -1): val1 = a[j1] val2 = a[j2] if flip: if val1 == "0": val1 = "1" else: val1 = "0" if val2 == "0": val2 = "1" else: val2 = "0" if b[i] == val2: j2 += inc continue if val1 != val2: p.append(1) p.append(i + 1) j1 -= inc j1, j2 = j2, j1 if inc == -1: inc = 1 else: inc = -1 flip = not flip s = str(len(p)) if len(p) > 0: s += " " s += " ".join(map(str, p)) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR STRING VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
for _ in range(int(input())): n = int(input()) ss = input() sss = input() s1 = [] s2 = [] for j in ss: s1.append(int(j)) for j in sss: s2.append(int(j)) ans = [] ans2 = [] for i in range(len(s1) - 1): if s1[i] != s1[i + 1]: ans.append(i + 1) if s1[-1] == 1: ans.append(n) ans2 = [] for i in range(len(s2) - 1): if s2[i] != s2[i + 1]: ans2.append(i + 1) if s2[-1] == 1: ans2.append(n) for i in range(len(ans2) - 1, -1, -1): ans.append(ans2[i]) print(len(ans), end=" ") print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def prefixFlip(s1, s2, n): operations = [] rev_ops = [] k = 0 for i in range(n - 1): if s1[i] != s1[i + 1]: operations.append(str(i + 1)) k += 1 for i in range(n - 1): if s2[i] != s2[i + 1]: rev_ops.append(str(i + 1)) k += 1 if s2[-1] != s1[-1]: rev_ops.append(str(n)) k += 1 operations += rev_ops[::-1] return [str(k)] + operations t = int(input()) for g in range(t): n = int(input()) s1 = input() s2 = input() ans = prefixFlip(s1, s2, n) print(" ".join(ans))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP LIST FUNC_CALL VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix. For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001. Your task is to transform the string a into b in at most 2n operations. It can be proved that it is always possible. Input The first line contains a single integer t (1≀ t≀ 1000) β€” the number of test cases. Next 3t lines contain descriptions of test cases. The first line of each test case contains a single integer n (1≀ n≀ 10^5) β€” the length of the binary strings. The next two lines contain two binary strings a and b of length n. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, output an integer k (0≀ k≀ 2n), followed by k integers p_1,…,p_k (1≀ p_i≀ n). Here k is the number of operations you use and p_i is the length of the prefix you flip in the i-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have 01β†’ 11β†’ 00β†’ 10. In the second test case, we have 01011β†’ 00101β†’ 11101β†’ 01000β†’ 10100β†’ 00100β†’ 11100. In the third test case, the strings are already the same. Another solution is to flip the prefix of length 2, which will leave a unchanged.
def rp(s): c = "" for i in range(len(s)): if s[i] == "0": c += "1" else: c += "0" return c[::-1] q = int(input()) for t in range(q): n = int(input()) a = input() b = input() j = n - 1 i = 0 inv = 0 ninv = 1 p = [] while j >= 0: if abs(inv - int(a[i])) == int(b[j]): p.append(1) p.append(j + 1) else: p.append(j + 1) inv, ninv = ninv, inv if inv == 1: i = n - 1 - i else: i = n - i j -= 1 print(len(p), end=" ") print(*p)
FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR STRING VAR STRING RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR