ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def min_cut(s):
n = len(s)
pal = [[False] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i, n):
pal[i][j] = s[i] == s[j] and (j - i < 2 or pal[i + 1][j - 1])
cut = [0] * n
for j in range(n):
if pal[0][j]:
cut[j] = 0
else:
cut[j] = min(cut[i - 1] + 1 for i in range(1, j + 1) if pal[i][j])
return cut[-1]
print(min_cut('aab'), min_cut('abccba'))- A. 2 0
- B. 1 1
- C. 1 0
- D. 0 0
Correct answer: C. 1 0
The table pal marks palindromic substrings, and the DP adds one cut per extra piece. aab needs one cut to give aa and b, while abccba is already a palindrome.
More Python Programming questions
- What is printed? python def triecount(words, pref): root = {} for w in words: node = root for ch in w: node = node.setdefault(ch, {})…
- What is printed? python M = 109 + 7 N = 10 fact = [1] (N + 1) for i in range(1, N + 1): fact[i] = fact[i - 1] i % M def nCr(n, r): return…
- What is printed? python def summin(a): MOD = 109 + 7 st = [] total = 0 cur = 0 for x in a: cnt = 1 while st and st[-1][0] >= x: v, c =…
- What is printed? python print(6 & 3 == 2, 5 | 2 == 7)
- What is printed? python import heapq def medians(xs): lo, hi = [], [] out = [] for x in xs: heapq.heappush(lo, -x) heapq.heappush(hi…
- What is printed? python def sieve(n): s = bytearray([1]) (n + 1) s[0:2] = bytes(2) for i in range(2, int(n 0.5) + 1): if s[i]: s[i i::i] =…