description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() a_count = s.count("a") b_count = len(s) - a_count a_counts = [0] * len(s) b_counts = [0] * len(s) for i in range(len(s)): if s[i] == "a": a_counts[i] = 1 else: b_counts[i] = 1 if i > 0: a_counts[i] += a_counts[i - 1] b_counts[i] += b_counts[i - 1] best = 0 count = 0 for i in range(-1, len(s)): for j in range(i + 1, len(s) + 1): count = 0 if i >= 0: count += a_counts[i] count += (b_count if j == len(s) else b_counts[j]) - ( 0 if i < 0 else b_counts[i] ) count += 0 if j == len(s) else a_count - a_counts[j] if best < count: best = count print(best)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
str = input() l = len(str) ok = 0 test1 = 0 test2 = 0 for i in range(l): if str[i] == "a": ok = ok + 1 test2 = max(test1 + 1, test2 + 1) else: test1 = max(test1 + 1, ok + 1) print(max(test1, test2))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) prefa = [0] * n prefb = [0] * n prefa[0] = s[0] == "a" prefb[0] = s[0] == "b" c = 0 for i in range(n): prefa[i] = prefa[i - 1] + (s[i] == "a") prefb[i] = prefb[i - 1] + (s[i] == "b") if i > 0: if s[i] != s[i - 1]: c += 1 if c < 2: print(n) exit() elif c == 2: if s[0] == "a": print(n) exit() ans1, ans = 1, 0 prefa.append(0) prefb.append(0) for i in range(n): for j in range(i, n): ans1 = prefa[n - 1] - prefa[j] + (prefb[j] - prefb[i - 1]) + prefa[i - 1] if ans1 > ans: ans = ans1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
line = input() dp = 3 * [0] for c in line: if c == "a": dp[2] = max(dp) + 1 dp[0] += 1 elif c == "b": dp[1] = max(dp[:2]) + 1 print(max(dp))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER LIST NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
import sys input = sys.stdin.readline inp = input().strip() cur = 0 pre_a = [] for i in range(len(inp)): if inp[i] == "a": cur += 1 pre_a.append(cur) cur = 0 pre_b = [] for i in range(len(inp)): if inp[i] == "b": cur += 1 pre_b.append(cur) ans = 0 for i in range(len(inp)): for j in range(i, len(inp)): re = (pre_b[i - 1] if i > 0 else 0) + pre_b[-1] - pre_b[j] if i != j: if i == 0: re += pre_a[j] else: re += pre_a[j] - pre_a[i - 1] ans = max(ans, len(inp) - re) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
read = lambda: map(int, input().split()) N = 50005 a = [(0) for i in range(N)] b = [(0) for i in range(N)] c = input() l = len(c) for i in range(l): a[i + 1] = a[i] b[i + 1] = b[i] if c[i] == "a": a[i + 1] += 1 else: b[i + 1] += 1 ans = 0 for i in range(l + 1): for j in range(i, l + 1): ans = max(ans, a[i] + b[j] - b[i] + a[l] - a[j]) print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) prefix_a = [0] prefix_b = [0] if s[0] == "a": prefix_a[0] = 1 else: prefix_b[0] = 1 for i in range(1, n): if s[i] == "a": prefix_a.append(1 + prefix_a[i - 1]) prefix_b.append(prefix_b[i - 1]) else: prefix_b.append(1 + prefix_b[i - 1]) prefix_a.append(prefix_a[i - 1]) if prefix_a[-1] == n or prefix_b[-1] == n: print(n) else: ans = -float("inf") for i in range(-1, n): for j in range(i + 1, n): if i == -1: if j != n - 1: ans = max(ans, prefix_b[j] + prefix_a[n - 1] - prefix_a[j]) else: ans = max(ans, prefix_b[j]) elif j != n - 1: ans = max( ans, prefix_a[i] + prefix_b[j] - prefix_b[i] + prefix_a[n - 1] - prefix_a[j], ) else: ans = max(ans, prefix_a[i] + prefix_b[j] - prefix_b[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = str(input()) n = len(s) if s.count("a") == 0 or s.count("b") == 0: print(n) exit() LA = [0] * (n + 1) LB = [0] * (n + 1) RA = [0] * (n + 1) RB = [0] * (n + 1) for i, c in enumerate(s): LA[i + 1] = LA[i] LB[i + 1] = LB[i] if c == "a": LA[i + 1] += 1 else: LB[i + 1] += 1 for i in reversed(range(n)): c = s[i] RA[i] = RA[i + 1] RB[i] = RB[i + 1] if c == "a": RA[i] += 1 else: RB[i] += 1 ans = 0 for i in range(n): if s[i] == "a": continue for j in range(i, n): if s[j] == "a": continue la = LA[i] b = LB[j + 1] - LB[i] ra = RA[j] temp = la + b + ra ans = max(temp, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() f = [([0] * len(s)) for _ in range(3)] for i in range(len(s)): c = s[i] if i == 0: f[0][i] += c != "a" else: f[0][i] = f[0][i - 1] + (c != "a") f[1][i] = f[0][i - 1] + (c != "b") if i > 1: f[1][i] = min(f[1][i - 1] + (c != "b"), f[1][i]) f[2][i] = f[1][i - 1] + (c != "a") if i > 2: f[2][i] = min(f[2][i - 1] + (c != "a"), f[2][i]) ans = f[2][-1] ans = min(ans, s.count("a"), s.count("b")) k = [0] for c in s: k.append(k[-1] + (c == "a") - (c == "b")) ans = min(ans, s.count("a") - max(k), s.count("b") + min(k)) print(len(s) - ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() ans = len(s) a = s.count("a") b = s.count("b") if not b: print(ans) else: s = s[s.find("b") : s.rfind("b") + 1] ans -= len(s) a -= ans ac = bc = 0 right = [0] * (len(s) + 1) left = right[:] left[0] = a - b for i in range(1, len(s) + 1): if s[i - 1] == "a": ac += 1 else: bc += 1 right[i] = ac - bc left[i] = a - ac - b + bc add = 0 n = len(s) + 1 for l in range(n): z = 0 for r in range(l, n): z = max(z, left[r]) add = max(add, b + right[l] + z) print(ans + add)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() ans = [] ab = [0] bb = [0] for i in range(len(s)): if s[i] == "a": ab.append(ab[-1] + 1) bb.append(bb[-1]) if s[i] == "b": bb.append(bb[-1] + 1) ab.append(ab[-1]) maxa = 0 l = 0 r = 0 for l in range(len(ab)): for r in range(l, len(ab)): maxa = max(ab[l] + (ab[-1] - ab[r]) + (bb[r] - bb[l]), maxa) print(maxa)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) ca = [0] * (n + 1) cb = [0] * (n + 1) for i in range(n): if s[i] == "a": ca[i + 1] += 1 else: cb[i + 1] += 1 for i in range(n): ca[i + 1] += ca[i] cb[i + 1] += cb[i] ans = min(ca[-1], cb[-1]) for l in range(1, n + 1): ans = min(ans, ca[l] + (cb[-1] - cb[l])) ans = min(ans, cb[l] + (ca[-1] - ca[l])) for r in range(l, n + 1): ans = min(ans, cb[l] + (ca[r] - ca[l]) + (cb[-1] - cb[r])) print(n - ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
f, s, t = 0, 0, 0 n = input() for i in n: if i == "a": t = max(t, f, s) + 1 f += 1 else: s = max(f, s) + 1 print(max(f, s, t))
ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
from sys import stdin, stdout s = stdin.readline().rstrip() n = len(s) aCount = 0 bCount = 0 aList = [0] bList = [0] for i in range(n): if s[i] == "a": aCount += 1 aList.append(aCount) bList.append(bCount) else: bCount += 1 aList.append(aCount) bList.append(bCount) bestScore = 0 for i in range(n + 1): for j in range(i, n + 1): score = 0 score += aList[i] score += bList[j] - bList[i] score += aCount - aList[j] if score > bestScore: bestScore = score print(bestScore)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
from itertools import accumulate from sys import stdout R = lambda: map(int, input().split()) s = input() n = len(s) ac, bc = ( list(accumulate(int(c == "a") for c in s)) + [0] * 5, list(accumulate(int(c == "b") for c in s)) + [0] * 5, ) res = 0 for i in range(n + 1): for j in range(i, n + 1): r1 = bc[i - 1] r2 = ac[j - 1] - ac[i - 1] r3 = bc[n - 1] - bc[j - 1] res = max(res, n - r1 - r2 - r3) print(res)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() ch = [] ct = [] i = 0 n = len(s) while i < n: t = s[i] ch.append(t) c = 0 while s[i] == t: c = c + 1 i = i + 1 if i == n: break ct.append(c) a, b = [0] * len(ct), [0] * len(ct) for i in range(len(ct)): if ch[i] == "a": a[i] = a[i - 1] + ct[i] b[i] = b[i - 1] if ch[i] == "b": b[i] = b[i - 1] + ct[i] a[i] = a[i - 1] ans = 0 if len(ct) > 1: for i in range(len(ct)): if ch[i] == "b": x, y, z = a[i], ct[i], 0 for j in range(i + 1, len(ct)): if ch[j] == "b": z = z + ct[j] ans = max(ans, x + y + z) elif ch[j] == "a": ans = max(ans, x + y + z + a[-1] - a[j - 1]) if ch[i] == "a": y, z = ct[i], 0 for j in range(i + 1, len(ct)): if ch[j] == "a": z = z + ct[j] ans = max(ans, y + z) elif ch[j] == "b": ans = max(ans, y + z + b[-1] - b[j - 1]) else: ans = ct[0] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
def nikita(s): a, b, result = 0, 0, 0 for elem in s: if elem == "a": a += 1 result += 1 else: b = max(a, b) + 1 result = max(result, b) return result print(nikita(input()))
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
strs = input() totala = 0 totalb = 0 prev = "" prefixa = [] prefixb = [] startingisa = 1 for j, i in enumerate(strs): if j == 0 and i == "a": startingisa = 0 if prev != i: prefixa.append(totala) prefixb.append(totalb) prev = i if i == "a": totala += 1 else: totalb += 1 prefixa.append(totala) prefixb.append(totalb) prefixa.pop(0) prefixb.pop(0) revstrs = strs[::-1] suffixa = [] suffixb = [] totala = 0 totalb = 0 prev = "" for j, i in enumerate(revstrs): if prev != i: suffixa.append(totala) suffixb.append(totalb) prev = i if i == "a": totala += 1 else: totalb += 1 suffixa.append(totala) suffixb.append(totalb) suffixb.pop(0) suffixa.pop(0) suffixa = suffixa[::-1] suffixb = suffixb[::-1] curmaxlen = max(prefixa[-1], prefixb[-1]) getlen = n = len(suffixa) if curmaxlen == 4013: print(curmaxlen) for i in range(1, getlen): curtotal = prefixa[i - 1] + suffixb[i] if curtotal > curmaxlen: curmaxlen = curtotal curtotal = prefixb[i - 1] + suffixa[i] if curtotal > curmaxlen: curmaxlen = curtotal tt = 0 for i in range(startingisa, n - 2, 2): for j in range(i + 2, n, 2): curtotal = prefixa[i] + suffixa[j] curb = prefixb[j] - prefixb[i] curtotal += curb if curtotal > curmaxlen: curmaxlen = curtotal tt += 1 print(curmaxlen)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
x = list(input()) a = 0 b = 0 a2 = 0 for i in range(len(x)): if x[i] == "a": a += 1 a2 += 1 if x[i] == "b": b += 1 a2 = max(a2, b) b = max(b, a) print(a2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = list(input()) n = len(s) lis = [[0, 0] for i in range(n + 2)] for i in range(n): if s[i] == "a": lis[i + 1][0] = lis[i][0] + 1 lis[i + 1][1] = lis[i][1] else: lis[i + 1][0] = lis[i][0] lis[i + 1][1] = lis[i][1] + 1 ans = 0 for i in range(1, n + 1): for j in range(i, n + 2): tmp = 0 tmp += lis[j - 1][1] - lis[i - 1][1] tmp += lis[i - 1][0] tmp += lis[n][0] - lis[j - 1][0] ans = max(ans, tmp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() a = [0] b = [0] n = len(s) for i in range(n): if s[i] == "a": a.append(a[-1] + 1) b.append(b[-1]) else: a.append(a[-1]) b.append(b[-1] + 1) a.append(a[-1]) b.append(b[-1]) maxm = 0 for i in range(1, n + 1): for j in range(i, n + 1): l = a[i] + a[n] - a[j] + b[j + 1] - b[i - 1] maxm = max(l, maxm) print(maxm)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) if not "b" in s: print(n) quit() preb = [1 if s[0] == "b" else 0] prea = [1 if s[0] == "a" else 0] posta = [1 if s[-1] == "a" else 0] for i in range(1, n): if s[i] == "a": prea.append(prea[-1] + 1) preb.append(preb[-1]) else: prea.append(prea[-1]) preb.append(preb[-1] + 1) for i in range(n - 2, -1, -1): if s[i] == "a": posta.append(posta[-1] + 1) else: posta.append(posta[-1]) posta = posta[::-1] res = 0 for i in range(n): for j in range(i, n): res_now = 0 if i > 0: res_now += prea[i - 1] res_now += preb[j] - preb[i - 1] else: res_now += preb[j] if j < n - 1: res_now += posta[j + 1] res = max(res, res_now) print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
x = str(input()) dp, ans, len = [0] * 5002, 0, len(x) for i in range(len): if x[i] == "a": if i == 0: dp[i] = 1 else: dp[i] = dp[i - 1] + 1 elif i > 0: dp[i] = dp[i - 1] for i in range(len): for i2 in range(i, len): if dp[i] + dp[len - 1] - dp[i2] + i2 - i - (dp[i2] - dp[i]) > ans: ans = dp[i] + dp[len - 1] - dp[i2] + i2 - i - (dp[i2] - dp[i]) for i in range(len): if i - dp[i] + dp[len - 1] + 1 - dp[i] > ans: ans = i - dp[i] + dp[len - 1] - dp[i] + 1 if len - dp[len - 1] > ans: ans = len - dp[len - 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = str(input()) a = [(ord(i) - ord("a")) for i in s] n = len(a) if n == 1 or n == 2: print(n) exit() c = [(0) for i in range(n + 1)] for i in range(n): c[i] = c[i - 1] + a[i] ans = 0 for i in range(n): for j in range(i, n): an = n - (c[i - 1] + c[n - 1] - c[j] + (j - i + 1) - c[j] + c[i - 1]) ans = max(ans, an) ans = max(ans, n - c[n - 1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = [i for i in input()] n = len(s) if "b" not in s: print(n) else: a = [0] * n b = [0] * n act = s.count("a") if s[0] == "a": a[0] += 1 else: b[0] += 1 for i in range(1, n): a[i] = a[i - 1] b[i] = b[i - 1] if s[i] == "a": a[i] += 1 else: b[i] += 1 ct = 0 for i in range(n): if s[i] == "b": tct = a[i] for j in range(i, n): if s[j] == "b": tct = max(tct, a[i] + (1 + b[j] - b[i]) + (act - a[j])) ct = max(ct, tct) print(ct)
ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() size = len(s) a = [0] * (size + 1) b = [0] * (size + 1) for i in range(size): a[i + 1] = a[i] + (1 if s[i] == "a" else 0) b[i + 1] = b[i] + (1 if s[i] == "b" else 0) ans = 0 for i in range(size + 1): for j in range(i + 1): val = b[i] - b[j] + a[j] + a[size] - a[i] ans = max(val, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
sl = list(input()) prif_a = [0] prif_b = [0] a = 0 b = 0 for x in sl: if x == "a": a += 1 else: b += 1 prif_a.append(a) prif_b.append(b) ans = 0 for i in range(len(prif_a)): for j in range(i, len(prif_a)): ans = max(ans, prif_a[-1] - prif_a[j] + prif_b[j] - prif_b[i] + prif_a[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
import sys word = sys.stdin.readline() word = word.replace("\n", "") N = len(word) ABA = [0] * (N + 1) BA = [0] * (N + 1) A = [0] * (N + 1) A[N] = 0 BA[N] = 0 ABA[N] = 0 for i in range(N - 1, -1, -1): if word[i] == "a": A[i] = A[i + 1] ABA[i] = ABA[i + 1] BA[i] = min(1 + BA[i + 1], A[i + 1]) else: A[i] = A[i + 1] + 1 ABA[i] = min(BA[i + 1], 1 + ABA[i + 1]) BA[i] = BA[i + 1] print(N - ABA[0])
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() k = 0 z = len(s) l = 0 A = s.count("a") B = z - A for i in range(z): if s[i] == "a": l += 1 A1 = A - l B1 = 0 p = l for j in range(i, z): if s[j] == "b": B1 += 1 else: A1 -= 1 p = max(p, l + B1 + A1) k = max(k, p) print(k)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) pa = [0] pb = [0] a = 0 b = 0 for i in range(n): if s[i] == "a": a += 1 else: b += 1 pa.append(a) pb.append(b) ans = [] for i in range(n + 1): for j in range(i, n + 1): ans.append(pa[i] + pb[j] - pb[i] + pa[n] - pa[j]) print(max(max(ans), s.count("a"), s.count("b")))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) f = [[(0) for i in range(0, 3)] for j in range(0, n)] for i in range(0, n): if i > 0: f[i][0] = f[i - 1][0] f[i][1] = f[i - 1][1] f[i][2] = f[i - 1][2] if s[i] == "a": f[i][0] += 1 f[i][2] += 1 f[i][2] = max(f[i][2], f[i][1] + 1) else: f[i][1] += 1 f[i][1] = max(f[i][1], f[i][0] + 1) print(max(f[n - 1][0], max(f[n - 1][1], f[n - 1][2])))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() a = 0 ab = 0 aba = 0 def max(a, b): if a > b: return a return b for i in range(len(s)): c = s[i] if c == "a": a += 1 aba = max(ab, aba) + 1 else: ab = max(a, ab) + 1 print(max(ab, max(a, aba)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
import sys input = sys.stdin.readline s = list(input().rstrip()) n = len(s) dp = [[(0) for i in range(n + 1)] for j in range(n + 1)] dp[0][0] = int(s[0] == "a") for i in range(1, n): if s[i] == "a": dp[0][i] = dp[0][i - 1] + 1 else: dp[0][i] = dp[0][i - 1] for i in range(n): for j in range(i, n): dp[i][j] = dp[0][j] - dp[0][i - 1] maxi = 0 for i in range(n): for j in range(i + 1, n): maxi = max(maxi, dp[0][i] + (j - i + 1) - dp[i][j] + dp[j][n - 1]) print(maxi if n > 2 else n)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() arr, dp = [], [[0, 0], 0] c = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: arr.append(c) c = 0 c += 1 arr.append(c) if s[0] == "b": arr = [0] + arr for i in range(2, len(arr) + 2): if i & 1: dp.append(max(dp[i - 2], dp[i - 1][0]) + arr[i - 2]) else: dp.append( [dp[i - 2][0] + arr[i - 2], max(dp[i - 1], dp[i - 2][1]) + arr[i - 2]] ) print(max(dp[i] if i & 1 else max(dp[i]) for i in range(len(dp))))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) dp = [[0, 0, 0] for i in range(n)] if s[n - 1] == "a": dp[n - 1][0] = 1 dp[n - 1][1] = 0 dp[n - 1][2] = 1 if s[n - 1] == "b": dp[n - 1][0] = 0 dp[n - 1][1] = 1 dp[n - 1][2] = 0 for i in range(n - 2, -1, -1): if s[i] == "a": dp[i][0] = max(dp[i + 1][0] + 1, dp[i + 1][1] + 1) dp[i][1] = max(dp[i + 1][2], dp[i + 1][1]) dp[i][2] = dp[i + 1][2] + 1 else: dp[i][0] = max(dp[i + 1][0], dp[i + 1][1]) dp[i][1] = max(dp[i + 1][2] + 1, dp[i + 1][1] + 1) dp[i][2] = dp[i + 1][2] print(max(dp[0][0], dp[0][1], dp[0][2]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() startpos = 0 max1 = 0 temp = 0 beststart = [] for i in range(len(s)): if s[i] == "a": temp += 1 if temp > max1: max1 = temp startpos = i + 1 else: temp -= 1 beststart.append((max1, startpos)) beststart.append((0, -1)) max2 = 0 temp = 0 endpos = len(s) for i in range(len(s) - 1, -1, -1): if s[i] == "a": temp += 1 + (beststart[i - 1][0] - beststart[i][0]) if max2 < temp: max2 = temp endpos = i else: temp -= 1 ans = 0 if endpos != len(s): startpos = beststart[endpos][1] if startpos == -1: startpos = 0 for i in range(startpos): if s[i] == "b": ans += 1 for i in range(startpos, endpos): if s[i] == "a": ans += 1 for i in range(endpos, len(s)): if s[i] == "b": ans += 1 print(len(s) - ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() p = [0] * 3 for i in range(len(s)): if s[i] == "a": p[0] += 1 p[2] = max(p[1], p[2]) + 1 else: p[1] = max(p[0], p[1]) + 1 print(max(p))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() le = len(s) mx = 0 a = s.count("a") b = s.count("b") mx = max(mx, a) mx = max(mx, b) cnta = [0] * (le + 1) cntb = [0] * (le + 1) cnta[0] = 1 if s[0] == "a" else 0 cntb[0] = 1 - cnta[0] for i in range(1, le): cnta[i] = cnta[i - 1] cntb[i] = cntb[i - 1] if s[i] == "a": cnta[i] += 1 else: cntb[i] += 1 for i in range(le): for j in range(i + 1, le): mx = max(mx, cnta[i] + b - cntb[i]) mx = max(mx, cntb[i] + a - cnta[i]) mx = max(mx, cnta[i] + cntb[j] - cntb[i] + a - cnta[j]) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) arr = ["."] count_a = [0] count_b = [0] count = 1 for i in range(1, n): if s[i] == s[i - 1]: count += 1 else: if s[i - 1] == "a": count_a.append(count_a[-1] + count) count_b.append(count_b[-1]) else: count_a.append(count_a[-1]) count_b.append(count_b[-1] + count) arr.append(s[i - 1]) count = 1 arr.append(s[-1]) if s[-1] == "a": count_a.append(count_a[-1] + count) count_b.append(count_b[-1]) else: count_a.append(count_a[-1]) count_b.append(count_b[-1] + count) m = len(arr) res = 0 for i in range(0, m): if arr[i] == "a": continue for j in range(i, m): if arr[j] == "a": continue tmp = count_a[i] + (count_a[-1] - count_a[j]) + (count_b[j] - count_b[i - 1]) res = max(res, tmp) print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
max1 = 0 string = input() first = 0 second = 0 third = 0 i = 0 length = len(string) while i < length: if string[i] == "a": if second > third: third = second + 1 else: third += 1 first += 1 if string[i] == "b": if first > second: second = first + 1 else: second += 1 i += 1 print(max(third, first, second))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
a = [0] f = "a" for i in input(): if f != i: a.append(1) f = i else: a[-1] += 1 m1 = [] m2 = [] if len(a) == 1: print(a[0]) elif len(a) == 2: print(a[0] + a[1]) elif len(a) == 3: print(a[0] + a[1] + a[2]) else: m2.append(a[0]) for i in range(1, len(a)): if i % 2 == 1: m2.append(0) else: m2.append(m2[i - 2] + a[i]) m1.append(a[0]) m1.append(a[0] + a[1]) m1.append(a[0] + a[1] + a[2]) for i in range(3, len(a)): if i % 2 != 0: m1.append(a[i] + max(m2[i - 1], m1[i - 2])) else: m1.append(a[i] + max(max(m1[i - 1], m1[i - 2]), m2[i - 2])) print(max(m1))
ASSIGN VAR LIST NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) a, b, aba = 0, 0, 0 for i in range(n): if s[i] == "a": aba = max(a, b, aba) + 1 a += 1 else: b = max(a, b) + 1 print(max(a, b, aba))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = " " + input() dp = [[0, 0, 0] for i in range(len(s) + 5)] i = 0 while i < len(s) - 1: i += 1 if s[i] == "a": dp[i][0] = dp[i - 1][0] + 1 dp[i][1] = dp[i - 1][1] dp[i][2] = max(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + 1 if s[i] == "b": dp[i][0] = dp[i - 1][0] dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + 1 dp[i][2] = max(dp[i - 1][0], dp[i - 1][2]) print(max(dp[i][0], dp[i][1], dp[i][2]))
ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) if "b" not in s or "a" not in s: print(n) exit() a = [(1 if s[i] == "a" else 0) for i in range(n)] b = [(1 if s[i] == "b" else 0) for i in range(n)] for i in range(1, n): a[i] += a[i - 1] b[i] += b[i - 1] ans = 0 for i in range(n): for j in range(i, n): cur = a[n - 1] - a[j] + b[j] if i > 0: cur -= b[i - 1] - a[i - 1] ans = max(ans, cur) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() a_count = 0 dp1 = 0 dp2 = 0 for i in range(len(s)): if s[i] == "a": a_count += 1 dp2 = max(dp1, dp2) + 1 else: dp1 = max(a_count, dp1) + 1 print(max(dp1, dp2))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) dp = [[(10**9) for j in range(4)] for i in range(n + 1)] dp[0][0] = 0 for i in range(n + 1): for j in range(3): cnt = 0 for k in range(i, n + 1): dp[k][j + 1] = min(dp[k][j + 1], dp[i][j] + cnt) if k < n and (j == 1 and s[k] == "a" or j != 1 and s[k] == "b"): cnt += 1 print(n - dp[n][3])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR STRING VAR NUMBER VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
def solve(string): dp = [[], [], [], 0] index = 0 for i in range(0, len(string)): if string[i] == "a": dp[0].append("a") dp[2].append("a") if dp[3] == 1: dp[3] = 2 if len(dp[1]) >= 1 and dp[1][-1] != "b": dp[1].append("a") else: dp[1].append("b") if dp[3] != 2: dp[2].append("b") dp[3] = 1 if len(dp[0]) > len(dp[1]): dp[1] = list(dp[0]) if len(dp[0]) > len(dp[2]): dp[2] = list(dp[0]) dp[3] = 0 if len(dp[1]) > len(dp[2]): dp[2] = list(dp[1]) dp[3] = 1 print(len(dp[2])) string = input().strip() solve(string)
FUNC_DEF ASSIGN VAR LIST LIST LIST LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) prefa = [0] * n prefb = [0] * n prefa[0] = s[0] == "a" prefb[0] = s[0] == "b" c = 0 for i in range(1, n): prefa[i] = prefa[i - 1] + (s[i] == "a") prefb[i] = prefb[i - 1] + (s[i] == "b") ans1, ans = 0, 0 prefa = [0] + prefa prefb = [0] + prefb for i in range(n + 1): for j in range(i, n + 1): ans1 = prefa[n] - prefa[j] + (prefb[j] - prefb[i]) + prefa[i] if ans1 > ans: ans = ans1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) ar = [0] * (n + 1) for i in range(1, n + 1): ar[i] = ar[i - 1] + int(s[i - 1] == "a") ans = 0 for i in range(n): for j in range(i, n): ans = max(ans, ar[i] + ar[-1] - ar[j] + j - i + 1 - ar[j + 1] + ar[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
a = input() n = len(a) prefix_a = [(0) for _ in range(0, n + 2)] prefix_b = [(0) for _ in range(0, n + 2)] for i in range(n): prefix_a[i + 1] = prefix_a[i] + (a[i] == "a") prefix_b[i + 1] = prefix_b[i] + (a[i] == "b") prefix_b[n + 1] = prefix_b[n] prefix_a[n + 1] = prefix_a[n] minimum = 10000000000.0 for i in range(0, n + 1): for j in range(i + 1, n + 2): minimum = min( minimum, prefix_b[i] + prefix_a[j] - prefix_a[i] + prefix_b[-1] - prefix_b[j], ) print(n - minimum)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) dp = [] dp.append((0, 0, 0)) for i in range(n): if s[i] == "a": dp.append((dp[-1][0] + 1, dp[-1][1], max(dp[-1][1:]) + 1)) elif s[i] == "b": dp.append((dp[-1][0], max(dp[-1][1], dp[-1][0]) + 1, dp[-1][-1])) print(max(dp[-1]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
string = input() a_pref = [] current_a = 0 for ch in string: if ch == "a": current_a += 1 a_pref += [current_a] def fa(s): if len(s) == 0: return 0 if len(s) == 1: return 1 if s == "a" else 0 if s[-1] == "a": return fa(s[:-1]) + 1 else: return fa(s[:-1]) def fb(s): if len(s) == 0: return 0 if len(s) == 1: return 1 if s == "b" else 0 sl_a = 1 if s[-1] == "a" else 0 sl_b = 1 if s[-1] == "b" else 0 return max(fa(s[:-1]) + sl_a, fb(s[:-1]) + sl_b) aas = [] bbs = [] for i in range(len(string)): sl_a = 1 if string[i] == "a" else 0 sl_b = 1 if string[i] == "b" else 0 a_pred = 0 if len(aas) == 0 else aas[-1] b_pred = 0 if len(bbs) == 0 else bbs[-1] aas += [a_pred + sl_a] bbs += [max(a_pred + sl_a, b_pred + sl_b)] answer = 0 a_count = string.count("a") for i in range(len(string)): current_answer = bbs[i] + a_count - a_pref[i] if current_answer > answer: answer = current_answer print(answer)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR LIST VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR STRING NUMBER NUMBER IF VAR NUMBER STRING RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR LIST BIN_OP VAR VAR VAR LIST FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) asum = [(0) for i in range(n + 1)] for i in range(n): asum[i + 1] = asum[i] + 1 if s[i] == "a" else asum[i] rmin = 2**31 for i in range(n + 1): for j in range(i, n + 1): c1 = asum[i] c2 = asum[j] - asum[i] c3 = asum[n] - asum[j] r = i - c1 r += c2 r += n - j - c3 rmin = min(rmin, r) res = n - rmin print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") S = input() n = len(S) ans = 0 D = [0, 0, 0] for _ in range(n): if S[_] == "a": D[2] = max(D) + 1 D[0] += 1 else: D[1] = max(D[0], D[1]) + 1 OPS(max(D))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() j = 0 a = 1 b = 0 max1 = 0 if len(s) == 1: print(1) elif len(s) == 2: print(2) else: if s[0] == "a": j += 1 if s[1] == "a": j += 1 if s[1] == "b": b = 1 dp = [1, 2] for i in range(2, len(s), 1): if s[i] == "a": j += 1 dp.append(max(dp[a] + 1, dp[b] + 1)) a = i else: dp.append(max(j + 1, dp[b] + 1)) b = i max1 = max(max1, dp[i]) print(max1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) a = [0] * n b = [0] * n c = [0] * n for i in range(n): a[i] = bool(s[i] == "a") if i > 0: a[i] += a[i - 1] for i in range(n): x = 0 if i > 0: x = b[i - 1] b[i] = max(a[i], x + bool(s[i] == "b")) for i in range(n): x = 0 if i > 0: x = c[i - 1] c[i] = max(b[i], x + bool(s[i] == "a")) print(int(c[n - 1]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR STRING IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
S = input() N = len(S) T = [([0] * (N + 1)) for _ in range(3)] for i in range(1, N + 1): T[0][i] = T[0][i - 1] + int(S[i - 1] == "a") T[1][i] = max(T[1][i - 1] + int(S[i - 1] == "b"), T[0][i]) T[2][i] = max(T[2][i - 1] + int(S[i - 1] == "a"), T[1][i], T[0][i]) print(T[2][N])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() i = 0 a = [0, 0, 0] for i in s: if i == "a": a[2] = max(a) + 1 a[0] += 1 else: a[1] = max(a[0], a[1]) + 1 print(max(a))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
import sys s = input() n = len(s) prefix_a = [0] * n prefix_b = [0] * n if s[0] == "a": prefix_a[0] = 1 else: prefix_b[0] = 1 for i in range(1, n): if s[i] == "a": prefix_a[i] = prefix_a[i - 1] + 1 prefix_b[i] = prefix_b[i - 1] else: prefix_a[i] = prefix_a[i - 1] prefix_b[i] = prefix_b[i - 1] + 1 if prefix_a[-1] == n or prefix_b[-1] == n: print(n) else: ans = -1 * sys.maxsize for i in range(-1, n): for j in range(i + 1, n): if i == -1: ans = max(ans, prefix_b[j] + prefix_a[n - 1] - prefix_a[j]) else: ans = max( ans, prefix_a[i] + prefix_b[j] - prefix_b[i] + prefix_a[n - 1] - prefix_a[j], ) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() a = b = c = 0 for i in range(len(s)): if s[i] == "a": a += 1 c += 1 else: b += 1 a = max(a, b) b = max(b, c) print(a)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() ans = 0 n = len(s) a = [0] * (n + 1) b = [0] * (n + 1) for i in range(n): if s[i] == "a": a[i + 1] = a[i] + 1 b[i + 1] = b[i] elif s[i] == "b": a[i + 1] = a[i] b[i + 1] = b[i] + 1 for j in range(0, n + 1): for i in range(0, j + 1): ans = max(ans, a[n] - a[j] + b[j] - b[i] + a[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
S = input() N = len(S) dp = [[(0) for i in range(3)] for j in range(N + 1)] for i in range(1, N + 1): if S[i - 1] == "b": dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + 1 dp[i][0] = dp[i - 1][0] dp[i][2] = dp[i - 1][2] else: dp[i][0] = dp[i - 1][0] + 1 dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + 1 dp[i][1] = dp[i - 1][1] print(max(dp[-1]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() d = [[(0) for i in range(len(s) + 5)] for j in range(3)] for i in range(1, len(s) + 1): d[0][i] = d[0][i - 1] + (s[i - 1] == "b") d[1][i] = d[1][i - 1] + (s[i - 1] == "a") d[1][i] = min(d[1][i], d[0][i - 1] + (s[i - 1] == "a")) d[2][i] = d[2][i - 1] + (s[i - 1] == "b") d[2][i] = min(d[2][i], d[1][i - 1] + (s[i - 1] == "b")) n = len(s) print(len(s) - min(d[0][n], d[1][n], d[2][n]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = list(input()) n = len(s) a = [] b = [] c = 0 for i in range(n): if s[i] == "a": c += 1 a.append(c) c = 0 for i in range(n): if s[i] == "b": c += 1 b.append(c) m = 1 for i in range(n): for j in range(i + 1, n): l = b[j] - b[i] + a[i] + a[n - 1] - a[j] if s[i] == "b": l += 1 if s[j] == "a": l += 1 if l > m: m = l print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
s = input() n = len(s) d = [([0] * 3) for _ in range(0, 5007)] mx = 0 for i in range(0, n): d[i][0] = (d[i - 1][0] if i > 0 else 0) + (s[i] == "a") d[i][1] = (max(d[i - 1][0], d[i - 1][1]) if i > 0 else 0) + (s[i] == "b") d[i][2] = (max(d[i - 1][1], d[i - 1][2]) if i > 0 else 0) + (s[i] == "a") for j in range(0, 3): mx = max(mx, d[i][j]) print(mx)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? -----Input----- The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". -----Output----- Print a single integer — the maximum possible size of beautiful string Nikita can get. -----Examples----- Input abba Output 4 Input bab Output 2 -----Note----- It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
def solve(dp, i, j): n = len(dp) if i >= 0: a = dp[i][0] else: a = 0 b = dp[j][1] if i >= 0: b -= dp[i][1] a += dp[-1][0] a -= dp[j][0] return a + b def main(): s = input() dp = [] for i in s: if not dp: curr = [0, 0] else: curr = dp[-1][:] if i == "a": curr[0] += 1 else: curr[1] += 1 dp.append(curr) ans = 0 n = len(s) for i in range(-1, n): for j in range(i, n): ans = max(ans, solve(dp, i, j)) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 4 ⋅ 10^6; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Input 787788 100000007 Output 94810539 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys input = sys.stdin.readline n, m = map(int, input().split()) dp = [0] * (n + 1) dp[1] = 1 dp[2] = 2 for i in range(2, n + 1): if i > 2: dp[i] = ((dp[i] + dp[i - 1]) % m + dp[i - 1] + 1) % m for j in range(i + i, n + 1, i): dp[j] = (dp[j] + dp[i] - dp[i - 1]) % m print(dp[n])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 4 ⋅ 10^6; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Input 787788 100000007 Output 94810539 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, mod = map(int, input().split()) dp = [0] * (n + 1) dp[-1] = 1 for i in range(n - 1, 0, -1): dp[i] = (dp[i + 1] + dp[i + 1]) % mod for j in range(2, n // i + 1): dp[i] = (dp[i] + dp[i * j] - (dp[i * j + j] if i * j + j <= n else 0)) % mod print((dp[1] - dp[2]) % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR
Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 4 ⋅ 10^6; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Input 787788 100000007 Output 94810539 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) sumL = [0] * (n + 2) L = 1 sumL[-2] = 1 for i in range(n - 1, 0, -1): L = sumL[i + 1] j = 2 while i * j <= n: L += sumL[i * j] - sumL[min(n, (i + 1) * j - 1) + 1] L %= m j += 1 L %= m sumL[i] = sumL[i + 1] + L sumL[i] %= m print(L)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 4 ⋅ 10^6; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Input 787788 100000007 Output 94810539 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys input = sys.stdin.readline n, m = map(int, input().split()) DP = [0] * (n + 5) CUM = [1] * (n + 5) CUM[1] = 0 DP[1] = 1 S = 1 for i in range(2, n + 1): CUM[i] = (CUM[i - 1] + CUM[i]) % m DP[i] = (S + CUM[i]) % m S += DP[i] S %= m for j in range(i + i, n + 1, i): CUM[j] = (CUM[j] + DP[i] - DP[i - 1]) % m print(DP[n])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
You are given a tree consisting of n nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array a is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array a is the number of pairs of indices (i, j) such that i < j and a_i > a_j. For example, the array [4, 1, 3, 2] contains 4 inversions: (1, 2), (1, 3), (1, 4), (3, 4). Input The first line contains a single integer n (2 ≤ n ≤ 200) — the number of nodes in the tree. The next n - 1 lines each contains two integers x and y (1 ≤ x, y ≤ n; x ≠ y), denoting an edge between node x and y. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Examples Input 3 1 2 1 3 Output 166666669 Input 6 2 1 2 3 6 1 1 4 2 5 Output 500000009 Input 5 1 2 1 3 1 4 2 5 Output 500000007 Note This is the tree from the first sample: <image> For the first sample, the arrays are almost fixed. If node 2 is chosen initially, then the only possible array is [2, 1, 3] (1 inversion). If node 3 is chosen initially, then the only possible array is [3, 1, 2] (2 inversions). If node 1 is chosen initially, the arrays [1, 2, 3] (0 inversions) and [1, 3, 2] (1 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 1/3⋅ 1 + 1/3 ⋅ 2 + 1/3 ⋅ (1/2 ⋅ 0 + 1/2 ⋅ 1) = 7/6. 166666669 ⋅ 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669. This is the tree from the second sample: <image> This is the tree from the third sample: <image>
mod = 10**9 + 7 def power(a, b): if b == 0: return 1 s = power(a, b >> 1) s = s * s % mod if b & 1: return s * a % mod return s n = int(input()) d = [[(10**9) for i in range(n)] for i in range(n)] for i in range(1, n): x, y = map(int, input().split()) x -= 1 y -= 1 d[x][y] = 1 d[y][x] = 1 for i in range(n): d[i][i] = 0 for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j], d[i][k] + d[k][j]) dp = [[(0) for i in range(n)] for i in range(n)] inv2 = power(2, mod - 2) for i in range(n): for j in range(n): if j == 0: continue if i == 0: dp[i][j] = 1 continue dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) * inv2 % mod ans = 0 for r in range(n): for i in range(n): for j in range(i): x, y = d[r][i], d[r][j] dist = int((x + y - d[i][j]) / 2) x -= dist y -= dist ans += dp[x][y] print(ans * power(n, mod - 2) % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
You are given a tree consisting of n nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array a is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array a is the number of pairs of indices (i, j) such that i < j and a_i > a_j. For example, the array [4, 1, 3, 2] contains 4 inversions: (1, 2), (1, 3), (1, 4), (3, 4). Input The first line contains a single integer n (2 ≤ n ≤ 200) — the number of nodes in the tree. The next n - 1 lines each contains two integers x and y (1 ≤ x, y ≤ n; x ≠ y), denoting an edge between node x and y. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Examples Input 3 1 2 1 3 Output 166666669 Input 6 2 1 2 3 6 1 1 4 2 5 Output 500000009 Input 5 1 2 1 3 1 4 2 5 Output 500000007 Note This is the tree from the first sample: <image> For the first sample, the arrays are almost fixed. If node 2 is chosen initially, then the only possible array is [2, 1, 3] (1 inversion). If node 3 is chosen initially, then the only possible array is [3, 1, 2] (2 inversions). If node 1 is chosen initially, the arrays [1, 2, 3] (0 inversions) and [1, 3, 2] (1 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 1/3⋅ 1 + 1/3 ⋅ 2 + 1/3 ⋅ (1/2 ⋅ 0 + 1/2 ⋅ 1) = 7/6. 166666669 ⋅ 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669. This is the tree from the second sample: <image> This is the tree from the third sample: <image>
import sys input = sys.stdin.buffer.readline maxn = 300 mod = 10**9 + 7 fac = [0] * (maxn + 1) inv_fac = [0] * (maxn + 1) fac[0] = 1 for i in range(1, maxn + 1): fac[i] = fac[i - 1] * i % mod inv_fac[maxn] = pow(fac[maxn], mod - 2, mod) for i in range(maxn - 1, -1, -1): inv_fac[i] = inv_fac[i + 1] * (i + 1) % mod def choose(n, k): if k > n: return 0 return fac[n] * inv_fac[k] * inv_fac[n - k] % mod inv_p = [0] * 300 inv_p[1] = inv_fac[2] for i in range(2, 300): inv_p[i] = inv_p[1] * inv_p[i - 1] % mod n = int(input()) adj = [[] for i in range(n + 1)] for i in range(n - 1): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) ans = 0 line_prob = [([0] * (n + 1)) for i in range(n + 1)] for j in range(1, n + 1): tot = 0 for i in range(n + 1 - j): tot += choose(j - 1 + i, i) * inv_p[j - 1 + i + 1] % mod line_prob[i + 1][j] = tot for root in range(1, n + 1): size_of_sub = [0] * (n + 1) s = [root] vis = [0] * (n + 1) par = [0] * (n + 1) while s: c = s[-1] if not vis[c]: vis[c] = 1 for ne in adj[c]: if not vis[ne]: par[ne] = c s.append(ne) else: size_of_sub[c] += 1 size_of_sub[par[c]] += size_of_sub[c] s.pop() s = [root] vis = [0] * (n + 1) path = [] while s: c = s[-1] if not vis[c]: vis[c] = 1 if len(path): path[-1] -= size_of_sub[c] path.append(size_of_sub[c]) if c > root: ln = len(path) - 1 for depth in range(1, ln): ans = ans + path[depth] * line_prob[depth][ln - depth] % mod ans = (ans + size_of_sub[c]) % mod for ne in adj[c]: if not vis[ne]: s.append(ne) else: if len(path) >= 2: path[-2] += path[-1] path.pop() s.pop() print(ans * pow(n, mod - 2, mod) % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
You are given a tree consisting of n nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array a is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array a is the number of pairs of indices (i, j) such that i < j and a_i > a_j. For example, the array [4, 1, 3, 2] contains 4 inversions: (1, 2), (1, 3), (1, 4), (3, 4). Input The first line contains a single integer n (2 ≤ n ≤ 200) — the number of nodes in the tree. The next n - 1 lines each contains two integers x and y (1 ≤ x, y ≤ n; x ≠ y), denoting an edge between node x and y. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Examples Input 3 1 2 1 3 Output 166666669 Input 6 2 1 2 3 6 1 1 4 2 5 Output 500000009 Input 5 1 2 1 3 1 4 2 5 Output 500000007 Note This is the tree from the first sample: <image> For the first sample, the arrays are almost fixed. If node 2 is chosen initially, then the only possible array is [2, 1, 3] (1 inversion). If node 3 is chosen initially, then the only possible array is [3, 1, 2] (2 inversions). If node 1 is chosen initially, the arrays [1, 2, 3] (0 inversions) and [1, 3, 2] (1 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 1/3⋅ 1 + 1/3 ⋅ 2 + 1/3 ⋅ (1/2 ⋅ 0 + 1/2 ⋅ 1) = 7/6. 166666669 ⋅ 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669. This is the tree from the second sample: <image> This is the tree from the third sample: <image>
import sys input = sys.stdin.readline mod = 10**9 + 7 n = int(input()) dist = [([float("inf")] * n) for _ in range(n)] for _ in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 dist[a][b] = dist[b][a] = 1 for i in range(n): dist[i][i] = 0 for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) dp = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(1, n + 1): dp[i][0] = 1 for i in range(1, n + 1): for j in range(1, n + 1): dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) * pow(2, mod - 2, mod) % mod ans = 0 for i in range(n): for j in range(i + 1, n): for k in range(n): x, y = dist[i][k], dist[j][k] d = (x + y - dist[i][j]) // 2 x -= d y -= d ans += dp[x][y] ans %= mod print(ans * pow(n, mod - 2, mod) % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
You are given a tree consisting of n nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array a is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array a is the number of pairs of indices (i, j) such that i < j and a_i > a_j. For example, the array [4, 1, 3, 2] contains 4 inversions: (1, 2), (1, 3), (1, 4), (3, 4). Input The first line contains a single integer n (2 ≤ n ≤ 200) — the number of nodes in the tree. The next n - 1 lines each contains two integers x and y (1 ≤ x, y ≤ n; x ≠ y), denoting an edge between node x and y. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Examples Input 3 1 2 1 3 Output 166666669 Input 6 2 1 2 3 6 1 1 4 2 5 Output 500000009 Input 5 1 2 1 3 1 4 2 5 Output 500000007 Note This is the tree from the first sample: <image> For the first sample, the arrays are almost fixed. If node 2 is chosen initially, then the only possible array is [2, 1, 3] (1 inversion). If node 3 is chosen initially, then the only possible array is [3, 1, 2] (2 inversions). If node 1 is chosen initially, the arrays [1, 2, 3] (0 inversions) and [1, 3, 2] (1 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 1/3⋅ 1 + 1/3 ⋅ 2 + 1/3 ⋅ (1/2 ⋅ 0 + 1/2 ⋅ 1) = 7/6. 166666669 ⋅ 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669. This is the tree from the second sample: <image> This is the tree from the third sample: <image>
import sys def solve(): MOD = 10**9 + 7 inv2 = (MOD + 1) // 2 n = int(input()) dist = [([n] * n) for i in range(n)] for i in range(n): dist[i][i] = 0 for i in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 dist[x][y] = dist[y][x] = 1 for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) dp = [([0] * n) for i in range(n)] dp[0] = [1] * n for i in range(1, n): for j in range(1, n): dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) * inv2 % MOD ans = 0 for i in range(n): for j in range(i): for k in range(n): di, dj, d = dist[i][k], dist[j][k], dist[i][j] ans = (ans + dp[(di + d - dj) // 2][(dj + d - di) // 2]) % MOD return ans * pow(n, MOD - 2, MOD) % MOD input = lambda: sys.stdin.readline().rstrip() print(solve())
IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a tree consisting of n nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array a is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array a is the number of pairs of indices (i, j) such that i < j and a_i > a_j. For example, the array [4, 1, 3, 2] contains 4 inversions: (1, 2), (1, 3), (1, 4), (3, 4). Input The first line contains a single integer n (2 ≤ n ≤ 200) — the number of nodes in the tree. The next n - 1 lines each contains two integers x and y (1 ≤ x, y ≤ n; x ≠ y), denoting an edge between node x and y. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}. Examples Input 3 1 2 1 3 Output 166666669 Input 6 2 1 2 3 6 1 1 4 2 5 Output 500000009 Input 5 1 2 1 3 1 4 2 5 Output 500000007 Note This is the tree from the first sample: <image> For the first sample, the arrays are almost fixed. If node 2 is chosen initially, then the only possible array is [2, 1, 3] (1 inversion). If node 3 is chosen initially, then the only possible array is [3, 1, 2] (2 inversions). If node 1 is chosen initially, the arrays [1, 2, 3] (0 inversions) and [1, 3, 2] (1 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 1/3⋅ 1 + 1/3 ⋅ 2 + 1/3 ⋅ (1/2 ⋅ 0 + 1/2 ⋅ 1) = 7/6. 166666669 ⋅ 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669. This is the tree from the second sample: <image> This is the tree from the third sample: <image>
from sys import stdin, stdout def dfs(cur, pre, d, dic, dep_a, jmp_a): dep_a[cur] = d jmp_a[cur][0] = pre for i in range(1, 10): jmp_a[cur][i] = jmp_a[jmp_a[cur][i - 1]][i - 1] for next in dic[cur]: if next == pre: continue dfs(next, cur, d + 1, dic, dep_a, jmp_a) def lca(a, b, dep_a, jmp_a): da = dep_a[a] db = dep_a[b] if da > db: return lca(b, a, dep_a, jmp_a) df = db - da for i in range(10): if 1 << i & df != 0: b = jmp_a[b][i] while a != b: jmp = -1 for i in range(9, -1, -1): if jmp_a[a][i] != jmp_a[b][i]: jmp = i break if jmp != -1: a = jmp_a[a][jmp] b = jmp_a[b][jmp] else: a = jmp_a[a][0] b = jmp_a[b][0] return a def get_percentage(n): per_a = [[(0) for _ in range(n)] for _ in range(n)] for j in range(1, n): per_a[0][j] = 1 for i in range(1, n): for j in range(1, n): per_a[i][j] = (per_a[i - 1][j] + per_a[i][j - 1]) * bpow(2, MOD - 2) per_a[i][j] %= MOD return per_a def bpow(a, b): if b == 0: return 1 c = b // 2 r = bpow(a, c) r *= r r %= MOD if b % 2 == 1: r *= a r %= MOD return r MOD = 1000000007 dic = {} n = int(stdin.readline()) for _ in range(n - 1): x, y = map(int, stdin.readline().split()) if x not in dic: dic[x] = [] if y not in dic: dic[y] = [] dic[x].append(y) dic[y].append(x) per_a = get_percentage(n) res = 0 for i in range(1, n + 1): dep_a = [0] * (n + 1) jmp_a = [[j for _ in range(10)] for j in range(n + 1)] dfs(i, i, 0, dic, dep_a, jmp_a) for l in range(2, n + 1): for r in range(l - 1, 0, -1): node = lca(l, r, dep_a, jmp_a) res += per_a[dep_a[l] - dep_a[node]][dep_a[r] - dep_a[node]] res %= MOD res *= bpow(n, MOD - 2) res %= MOD stdout.write(str(res) + "\n")
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
import sys n, a, b = map(int, input().split()) h = list(map(int, input().split())) t = h[-1] // b + 1 h[-1] -= t * b h[-2] -= t * a h[-3] -= t * b res = [sys.maxsize, None] c = [0] * n def search(i, cur): if i == n - 1: res[0] = cur c[n - 2] += t res[1] = c[:] return if h[i - 1] >= 0: x = h[i - 1] // b + 1 else: x = 0 while cur + x < res[0]: h[i - 1] -= x * b h[i] -= x * a h[i + 1] -= x * b c[i] = x search(i + 1, cur + x) h[i - 1] += x * b h[i] += x * a h[i + 1] += x * b x += 1 search(1, t) print(res[0]) s = [] for i in range(n): for j in range(res[1][i]): s.append(str(i + 1)) print(" ".join(s))
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST VAR NONE ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
__author__ = "Darren" def solve(): n, a, b = map(int, input().split()) h = [0] h.extend(map(int, input().split())) fires = [] count = h[1] // b + 1 if h[1] >= 0 else 0 fires.extend([(2) for i in range(count)]) h[1] -= b * count h[2] -= a * count h[3] -= b * count count = h[n] // b + 1 if h[n] >= 0 else 0 fires.extend([(n - 1) for i in range(count)]) h[n] -= b * count h[n - 1] -= a * count h[n - 2] -= b * count temp = fires.copy() for i in range(2, n): count = h[i] // a + 1 fires.extend([i for j in range(count)]) def search(pos): nonlocal n, a, b, h, fires, temp if pos == n and h[pos - 1] < 0: if len(fires) > len(temp): fires = temp.copy() return balls = 0 count = h[pos - 1] // b + 1 if h[pos - 1] >= 0 else 0 temp.extend([pos for i in range(count)]) h[pos - 1] -= b * count h[pos] -= a * count h[pos + 1] -= b * count balls += count while h[pos] >= 0: search(pos + 1) temp.append(pos) h[pos - 1] -= b h[pos] -= a h[pos + 1] -= b balls += 1 search(pos + 1) h[pos - 1] += b * balls h[pos] += a * balls h[pos + 1] += b * balls for i in range(balls): temp.pop() search(2) print(len(fires)) print(" ".join(map(str, fires))) solve()
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
n, a, b = map(int, input().split()) f = list(map(int, input().split())) mn = 1 << 64 hit = None def dfs(p, ans, hit2): global mn if ans >= mn: return if p == n - 1: if ans < mn: global hit hit = hit2.copy() mn = ans return if f[p - 1] < 0: dfs(p + 1, ans, hit2) t = max(f[p - 1] // b + 1, f[p] // a + 1) for i in range(t + 1): if f[p - 1] < b * i: f[p - 1] -= b * i f[p] -= a * i f[p + 1] -= b * i for j in range(i): hit2.append(p) dfs(p + 1, ans + i, hit2) f[p - 1] += b * i f[p] += a * i f[p + 1] += b * i for j in range(i): hit2.pop() return hit2 = [] a1 = f[0] // b + 1 f[0] -= b * a1 f[1] -= a * a1 f[2] -= b * a1 ret = a1 for i in range(a1): hit2.append(1) if f[n - 1] >= 0: a1 = f[n - 1] // b + 1 f[n - 1] -= b * a1 f[n - 2] -= a * a1 f[n - 3] -= b * a1 for i in range(a1): hit2.append(n - 2) ret += a1 dfs(1, 0, hit2) mn += ret print(mn) for i in range(mn): print(hit[i] + 1, end=" ") print("")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NONE FUNC_DEF IF VAR VAR RETURN IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
n, a, b = list(map(int, input().split(" "))) h = list(map(lambda x: int(x) + 1, input().split(" "))) dp = [[([-1] * 17) for j in range(17)] for i in range(n)] num = [[([0] * 17) for j in range(17)] for i in range(n)] health = [[([0] * 17) for j in range(17)] for i in range(n)] result = [] def recursion(i, j, k): if i == n - 1: return float("inf") if j > 0 else 0 if dp[i][j][k] != -1: return dp[i][j][k] dp[i][j][k] = float("inf") for s in range(17): if j <= s * b: bt = s + recursion(i + 1, max(0, h[i] - k * b - s * a), s) if bt < dp[i][j][k]: dp[i][j][k] = bt num[i][j][k] = s health[i][j][k] = max(0, h[i] - k * b - s * a) return dp[i][j][k] def compute(i, j, k): global result if i == n - 1: return result += [str(i + 1)] * num[i][j][k] compute(i + 1, health[i][j][k], num[i][j][k]) while h[0] > 0: h[0] = max(h[0] - b, 0) h[1] = max(h[1] - a, 0) h[2] = max(h[2] - b, 0) result.append(str(2)) while h[n - 1] > 0: h[n - 1] = max(h[n - 1] - b, 0) h[n - 2] = max(h[n - 2] - a, 0) h[n - 3] = max(h[n - 3] - b, 0) result.append(str(n - 1)) recursion(1, 0, 0) compute(1, 0, 0) print(len(result)) print(" ".join(result))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR WHILE VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
def main(): n, a, b = map(int, input().split()) health = [0] health.extend(map(int, input().split())) target = [0] * len(health) def need(t, d): nonlocal health p = health[t] return p // d + 1 if p >= 0 else 0 least_target = [] least_thrown = sum(health) last_need = max(need(n - 1, a), need(n, b)) def beat(left, sofar): nonlocal health, target, n, a, b, need nonlocal least_thrown, least_target, last_need if sofar >= least_thrown: return center = left + 1 right = center + 1 if right == n: m = max(need(left, b), last_need) thrown = sofar + m if thrown < least_thrown: least_thrown = thrown least_target = target[:] least_target[center] += m return must = need(left, b) for m in range(must, max(must, need(center, a)) + 1): ma = m * a mb = m * b health[left] -= mb health[center] -= ma health[right] -= mb target[center] += m beat(center, sofar + m) target[center] -= m health[right] += mb health[center] += ma health[left] += mb beat(1, 0) def target_sequence(targets): for t, k in enumerate(targets): if k: s = str(t) yield from (s for _ in range(k)) if least_target: print(least_thrown) print(" ".join(target_sequence(least_target))) else: print(0) print() main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR RETURN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
def dfs(step, left, mx): if step == 0: for i in range(0, n): if h[i] >= 0: return False return True else: if h[left - 1] < 0 and h[left] < 0 and left + 2 != n: if dfs(step, left + 1, mx): return True else: if left >= mx: h[left - 1] -= b h[left] -= a h[left + 1] -= b ans[step] = left if dfs(step - 1, left, left): return True h[left - 1] += b h[left] += a h[left + 1] += b if left + 1 >= mx and left + 1 < n - 1: h[left] -= b h[left + 1] -= a h[left + 2] -= b ans[step] = left + 1 if dfs(step - 1, left, left + 1): return True h[left] += b h[left + 1] += a h[left + 2] += b return False n, a, b = input().split() n = int(n) a = int(a) b = int(b) h = list(map(int, input().split())) ans = list(range(0, 200)) for i in range(0, 200): if dfs(i, 1, 1): print(i) for j in range(1, i): print(ans[j] + 1, end=" ") print(ans[i] + 1) break
FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
import sys def main(): n, a, b = map(int, sys.stdin.readline().strip().split()) archers = list(map(int, sys.stdin.readline().strip().split())) temp = [0] * n shots = [] shots_first = archers[0] // b + 1 archers[0] -= shots_first * b archers[1] -= shots_first * a archers[2] -= shots_first * b shots_last = archers[n - 1] // b + 1 archers[n - 1] -= shots_last * b archers[n - 2] -= shots_last * a archers[n - 3] -= shots_last * b min_shots = 10000 def kill(current_archer, life_current, life_left): nonlocal temp, shots, min_shots next_archer = current_archer + 1 min_shots_kill_left = life_left // b + 1 if life_left >= 0 else 0 min_shots_kill_current = life_current // a + 1 if life_current >= 0 else 0 min_shots_kill_right = ( archers[next_archer] // b + 1 if archers[next_archer] >= 0 else 0 ) min_shots_kill_trio = max( min_shots_kill_left, min_shots_kill_current, min_shots_kill_right ) accum_shots = sum(temp[:current_archer]) if current_archer == n - 2: temp[n - 2] = min_shots_kill_trio accum_shots += min_shots_kill_trio if min_shots > accum_shots: min_shots = accum_shots shots = temp.copy() return life = life_current - min_shots_kill_left * a if (life // a + 1 if life >= 0 else 0) == (life // b + 1 if life >= 0 else 0): temp[current_archer] = min_shots_kill_left kill(next_archer, archers[next_archer] - min_shots_kill_left * b, life) else: for j in range(min_shots_kill_left, min_shots_kill_trio + 1): if accum_shots + j >= min_shots: break temp[current_archer] = j kill(next_archer, archers[next_archer] - j * b, life_current - j * a) kill(1, archers[1], archers[0]) shots[1] += shots_first shots[n - 2] += shots_last min_shots += shots_first + shots_last sys.stdout.write(str(min_shots) + "\n") sys.stdout.write( " ".join(str(pos) for pos, s in enumerate(shots, start=1) for _ in range(s)) + "\n" ) main()
IMPORT FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each. As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has. Output In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order. Examples Input 3 2 1 2 2 2 Output 3 2 2 2 Input 4 3 1 1 4 1 1 Output 4 2 2 3 3
res = 10000 path = [] n, a, b = map(int, input().split()) def solve(cnt, idx, arc, p): global res global path if idx == n - 1: if res > cnt: res = cnt path = p.copy() return if cnt > res: return if arc[idx] < 0 and arc[idx - 1] < 0: solve(cnt, idx + 1, arc, p) else: min_t, max_t = arc[idx - 1] // b + 1, arc[idx] + 2 // a + 1 min_t = max(min_t, 0) max_t = max(min_t, max_t) for i in range(min_t, max_t + 1): arc[idx - 1] -= i * b arc[idx] -= i * a arc[idx + 1] -= i * b p.extend([idx + 1] * i) solve(cnt + i, idx + 1, arc, p) for _ in range(i): p.pop() arc[idx - 1] += i * b arc[idx] += i * a arc[idx + 1] += i * b h = list(map(int, input().split())) t = h[n - 1] // b + 1 h[n - 1] -= t * b h[n - 2] -= t * a h[n - 3] -= t * b tmp = [] tmp.extend([n - 1] * t) solve(t, 1, h, tmp) print(res) print(" ".join(str(i) for i in path))
ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN IF VAR VAR RETURN IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) mat = [] ans = [[(0) for i in range(k)] for j in range(n)] for i in range(n): temp = input() mat.append(temp) for i in range(k): if mat[n - 1][i] == "*": ans[n - 1][i] = 1 for i in range(n - 2, -1, -1): if mat[i][0] == "*": ans[i][0] = 1 if mat[i][k - 1] == "*": ans[i][k - 1] = 1 for j in range(1, k - 1): if mat[i][j] == "*": h = 1 + min(ans[i + 1][j], ans[i + 1][j - 1], ans[i + 1][j + 1]) ans[i][j] = h res = 0 for i in range(n): for j in range(k): res += ans[i][j] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
I = input for _ in [0] * int(I()): n, m = map(int, I().split()) a = [I() for _ in [0] * n] b = [0] * 501 r = 0 for s in a[::-1]: b = 0, *((s[i] < ".") * (min(b[i : i + 3]) + 1) for i in range(m)), 0 r += sum(b) print(r)
ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR STRING BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") for _ in range(INI()): n, m = INL() X = [] for _ in range(n): X.append(list(input())) D = [[(0) for _ in range(m)] for _ in range(n)] for _ in range(n - 1, -1, -1): for __ in range(m - 1, -1, -1): if _ + 1 < n and __ + 1 < m and -1 < __ - 1 and X[_][__] == "*": D[_][__] = min(D[_ + 1][__ - 1], D[_ + 1][__], D[_ + 1][__ + 1]) + 1 elif X[_][__] == "*": D[_][__] = 1 ans = 0 for _ in range(n): ans += sum(D[_]) OPS(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
for _ in range(int(input())): n, m = map(int, input().split()) l = [] res = 0 pre = [] for i in range(n): s = input() v = [0] * m if s[0] == ".": v[0] = 1 else: res += 1 for j in range(1, m): if s[j] == "*": res += 1 v[j] = v[j - 1] else: v[j] = 1 + v[j - 1] l.append(s) pre.append(v) for i in range(n): for j in range(m): if l[i][j] != ".": x = j - 1 y = j + 1 c = i + 1 while c < n and x >= 0 and y < m: if x == 0: cur = pre[c][y] else: cur = pre[c][y] - pre[c][x - 1] if cur >= 1: break else: res += 1 x -= 1 y += 1 c += 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
for _ in range(int(input())): n, m = map(int, input().split()) mat = [] for i in range(n): temp = input() mat.append(temp) c = 0 dp = [[(0) for x in range(m)] for y in range(n)] for i in range(n): temp = 0 for j in range(m): if mat[i][j] == "*": temp += 1 dp[i][j] = temp def isvalid(x1, y1, x2, y2): if x1 < 0 or x1 >= n or y1 < 0 or y1 >= m or y2 < 0 or y2 >= m: return False return True def allstars(x1, y1, x2, y2): if y1 == 0: if dp[x1][y2] == y2 - y1 + 1: return True else: return False elif dp[x1][y2] - dp[x1][y1 - 1] == y2 - y1 + 1: return True return False for i in range(n): for j in range(m): if mat[i][j] == "*": x1 = i y1 = j x2 = i y2 = j while 1: if isvalid(x1, y1, x2, y2): if allstars(x1, y1, x2, y2): c += 1 x1 += 1 y1 -= 1 x2 += 1 y2 += 1 else: break else: break print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) inp = [] for i in range(n): s = input() inp.append(s) ans = 0 R = [] L = [] for i in range(n): low = 0 high = 0 right = [] left = [] while high < m: if inp[i][high] == "*": while high < m and inp[i][high] == "*": high += 1 for j in range(high - low): left.append(j) right.append(high - low - j - 1) low = high else: left.append(0) right.append(0) high += 1 low += 1 R.append(right) L.append(left) main = [[(0) for i in range(m)]] for i in range(n): temp = [] for j in range(m): if inp[i][j] == "*": x = min(main[i][j] + 1, min(L[i][j], R[i][j]) + 1) ans += x temp.append(x) else: temp.append(0) main.append(temp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR STRING WHILE VAR VAR VAR VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
import sys import time def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_int(): return int(sys.stdin.readline()) def get_line(): return sys.stdin.readline().strip() def find(mat): count = 0 for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if mat[i][j] == ".": mat[i][j] = 0 elif j == 0 or j == m - 1 or i == n - 1: mat[i][j] = 1 else: min_val = min(mat[i + 1][j - 1], mat[i + 1][j], mat[i + 1][j + 1]) mat[i][j] = min_val + 1 count += mat[i][j] return count res = [] for _ in range(get_int()): n, m = get_ints() mat = [] for i in range(n): mat.append(list(get_line())) res.append(str(find(mat))) sys.stdout.write("\n".join(res))
IMPORT IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
for _ in range(int(input())): n, m = map(int, input().split()) a = [[*input()] for _ in range(n)] ans = 0 l = [([0] * m) for _ in range(n)] r = [([0] * m) for _ in range(n)] for i in range(n): for j in range(m): l[i][j] = 1 if a[i][j] == "*" else 0 r[i][j] = 1 if a[i][j] == "*" else 0 for i in range(n): for j in range(1, m): if l[i][j]: l[i][j] += l[i][j - 1] for i in range(n): for j in range(m - 2, -1, -1): if r[i][j]: r[i][j] += r[i][j + 1] ans = 0 for i in range(n): for j in range(m): nx = 1 if l[i][j]: for k in range(i, n): if l[k][j] - 1 >= nx // 2 and r[k][j] - 1 >= nx // 2: ans += 1 nx += 2 else: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
import sys t = int(input()) for i in range(t): n, m = map(int, input().split()) a = [] for j in range(n): a.append(sys.stdin.readline().strip()) dp = [[(0) for j in range(m)] for k in range(n)] s = 0 for j in range(n): for k in range(m): if a[j][k] == "*": if j > 0 and k > 1: dp[j][k] = 1 + min(dp[j - 1][k - 1], dp[j][k - 1], dp[j][k - 2]) else: dp[j][k] = 1 s = s + dp[j][k] print(s)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
for _ in range(int(input())): ROW, COL = map(int, input().split()) arr = [] for _ in range(ROW): arr.append(list(input())) sol = [[(0) for i in range(COL)] for j in range(ROW)] for row in range(ROW): for col in range(COL): if arr[row][col] == "*": sol[row][col] = 1 for row in range(ROW - 2, -1, -1): for col in range(COL): if col == 0 or col == COL - 1: pass elif sol[row][col] >= 1: sol[row][col] += min( sol[row + 1][col - 1], sol[row + 1][col], sol[row + 1][col + 1] ) x = 0 for i in sol: x += sum(i) print(x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
def compute_spruce(grid, width, x, y): height = 1 while True: if x + height >= len(grid): break if width[x + height][y] >= height + 1: height += 1 else: break return height t = int(input()) for _ in range(t): n, m = map(int, input().split()) grid = [] for _ in range(n): grid.append(list(input())) width = [[(0) for _ in range(m)] for _ in range(n)] left = [[(0) for _ in range(m)] for _ in range(n)] right = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m - 1, -1, -1): if grid[i][j] == "*": if j == m - 1: right[i][j] = 1 else: right[i][j] = 1 + right[i][j + 1] for i in range(n): for j in range(m): if grid[i][j] == "*": if j == 0: left[i][j] = 1 else: left[i][j] = 1 + left[i][j - 1] for i in range(n): for j in range(m): width[i][j] = min(left[i][j], right[i][j]) total = 0 dp = [[(0) for _ in range(m)] for _ in range(n)] for j in range(m): if grid[0][j] == "*": dp[0][j] = 1 for i in range(1, n): for j in range(m): dp[i][j] = min(dp[i - 1][j], width[i][j] - 1) + 1 for i in range(n): for j in range(m): total += dp[i][j] print(total)
FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
t = int(input()) for _t in range(t): n, m = map(int, input().split()) grid = [] for i in range(n): grid.append(input()) dp = [[(0) for i in range(m)] for j in range(n)] ans = 0 for i in range(n - 1, -1, -1): for j in range(m): if grid[i][j] == "*": if i == n - 1 or j == 0 or j == m - 1: dp[i][j] = 1 else: dp[i][j] = 1 + min(dp[i + 1][j - 1], dp[i + 1][j], dp[i + 1][j + 1]) else: dp[i][j] = 0 ans += dp[i][j] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
def main(): for _ in range(int(input())): n, m = map(int, input().split()) mm = [] ans = 0 for i in range(n): s = input() l = [] for j in s: if j == ".": l.append(0) else: l.append(1) mm.append(l) for i in range(n - 1, -1, -1): if i - 1 >= 0: for j in range(1, m - 1): if mm[i][j] > 0 and mm[i - 1][j] > 0: if mm[i][j - 1] > 0 and mm[i][j + 1] > 0: mm[i - 1][j] += min(mm[i][j - 1], mm[i][j + 1]) ans = 0 for l in mm: ans += sum(l) print(ans) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
t = int(input()) ml = [] for ind in range(t): s = input() s = s.split() for i in range(len(s)): s[i] = int(s[i]) n = s[0] m = s[1] a = [] b = [] for ind in range(m + 2): b.append(0) for ind in range(n + 2): a.append(b.copy()) for i in range(n): s2 = input() for j in range(m): if s2[j] == "*": a[i + 1][j + 1] = -1 ml.append([n, m, a]) def f(a, x, y): if a[y + 1][x + 1] == -1: a[y + 1][x + 1] = 1 + min([a[y + 2][x], a[y + 2][x + 1], a[y + 2][x + 2]]) def sum(a): s = 0 for l in a: for x in l: s = s + x return s def main(): for e in ml: n = e[0] m = e[1] a = e[2] y = n - 1 x = 0 while y > -1: while x < m: f(a, x, y) x = x + 1 x = 0 y = y - 1 print(sum(a)) main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
import sys m = {"*": 1, ".": 0} def sip(): return sys.stdin.readline().rstrip() def gen_test_cases(): num_tests = int(sip()) for i in range(num_tests): height, width = map(int, sip().split(" ")) arr = [ [m[c] for c in line] for line in [sys.stdin.readline().rstrip() for x in range(height)] ] yield height, width, arr for h, w, mc in gen_test_cases(): for y in range(1, h): for x in range(1, w - 1): if mc[y][x] != 0: r = min(mc[y][x - 1], mc[y - 1][x]) if r: mc[y][x] = r + 1 for x in reversed(range(w - 1)): if mc[y][x] != 0: mc[y][x] = min(mc[y][x], mc[y][x + 1] + 1) count = sum([sum(mc[y]) for y in range(h)]) print(count)
IMPORT ASSIGN VAR DICT STRING STRING NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
for _ in range(int(input())): n, m = map(int, input().split()) a = [input() for x in range(n)] ans = 0 for j in range(m): w = 0 for i in range(n): w = min((w, j, m - j - 1)) for k in range(w + 1): if a[i][j + k] == "." or a[i][j - k] == ".": w = k - 1 break ans += 1 + w w += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $n \times m$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $k$ with origin at point $(x, y)$ if: All cells in the set contain an "*". For each $1 \le i \le k$ all cells with the row number $x+i-1$ and columns in range $[y - i + 1, y + i - 1]$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $n \times m$ matrix contains. Help Rick solve this problem. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 500$) — matrix size. Next $n$ lines of each test case contain $m$ characters $c_{i, j}$ — matrix contents. It is guaranteed that $c_{i, j}$ is either a "." or an "*". It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $500^2$ ($\sum n \cdot m \le 500^2$). -----Output----- For each test case, print single integer — the total number of spruces in the matrix. -----Examples----- Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Output 5 3 23 34 -----Note----- In the first test case the first spruce of height $2$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(1, 2)$, the third spruce of height $1$ has its origin at point $(2, 1)$, the fourth spruce of height $1$ has its origin at point $(2, 2)$, the fifth spruce of height $1$ has its origin at point $(2, 3)$. In the second test case the first spruce of height $1$ has its origin at point $(1, 2)$, the second spruce of height $1$ has its origin at point $(2, 1)$, the third spruce of height $1$ has its origin at point $(2, 2)$.
t = int(input()) while t: n, m = map(int, input().split()) x = [] for i in range(n): y = input() x.append(list(y)) dp = [] for i in range(n): dp.append([0] * m) for i in range(n): for j in range(m): if x[i][j] == "*": dp[i][j] = 1 for i in range(n - 2, -1, -1): for j in range(m - 2, 0, -1): if dp[i][j] == 1: dp[i][j] = 1 + min(dp[i + 1][j - 1], dp[i + 1][j], dp[i + 1][j + 1]) ans = 0 for i in range(n): for j in range(m): ans += dp[i][j] print(ans) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER