description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) while t > 0: n, r = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) maxi = b[0] temp = b[0] for i in range(1, n): maxi = maxi - (a[i] - a[i - 1]) * r if maxi < 0: maxi = 0 maxi = maxi + b[i] temp = max(temp, maxi) print(temp) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
T = int(input()) for i in range(T): n, r = map(int, input().split()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) i = m = x = 0 while i < n: x = x + l2[i] if x > m: m = x i = i + 1 if i < n: x = x - (l1[i] - l1[i - 1]) * r if x - (l1[i] - l1[i - 1]) * r > 0 else 0 print(m)
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 FUNC_CALL 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 VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for _ in range(int(input())): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c, d = [b[0]], b[0] for i in range(1, n): d = max(0, d - (a[i] - a[i - 1]) * r) d += b[i] c += [d] print(max(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 FUNC_CALL 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 VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
def solve(): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.append(0) x = 0 result = 0 for i in range(0, n): x += b[i] result = max(result, x) x = max(0, x - r * (a[i + 1] - a[i])) print(result) def main(): for _ in range(int(input())): solve() main()
FUNC_DEF ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
def integer_list(): return list(map(int, input().split())) def string_list(): return list(map(str, input().split())) def hetro_list(): return list(input().split()) t = int(input()) for _ in range(t): n, r = integer_list() a_lst = integer_list() b_lst = integer_list() tension = b_lst[0] time = a_lst[0] ans = b_lst[0] for i in range(1, n): t = a_lst[i] - time tension = max(0, tension - r * t) time = a_lst[i] tension += b_lst[i] ans = max(ans, tension) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for _ in range(int(input())): n, r = map(int, input().split()) list1 = list(map(int, input().split())) list2 = list(map(int, input().split())) if n == 1: print(list2[0]) continue summ = list2[0] maxx = list2[0] for i in range(1, n): if r * (list1[i] - list1[i - 1]) >= summ: summ = list2[i] else: summ += list2[i] - r * (list1[i] - list1[i - 1]) if summ > maxx: maxx = summ print(maxx)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for i in range(int(input())): n, r = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) maxt = b[0] t = b[0] x = b[0] for j in range(1, n): prev = a[j - 1] curr = a[j] x = max(b[j], x + b[j] - (curr - prev) * r) if x > maxt: maxt = x print(maxt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for _ in range(int(input())): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) tot, m = 0, 0 for i in range(n): tot += b[i] if tot > m: m = tot if i < n - 1: tot -= (a[i + 1] - a[i]) * r if tot < 0: tot = 0 print(m)
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 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 VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for _ in range(int(input())): N, R = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] ans = B[0] x = B[0] for i in range(1, N, 1): dec = (A[i] - A[i - 1]) * R x = max(x - dec, 0) x += B[i] ans = max(ans, x) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for i in range(t): n, k = map(int, input().split()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) l1.append(l1[-1]) pump = 0 ans = 0 for i in range(len(l1)): try: pump += l2[i] except IndexError: pass if ans < pump: ans = pump try: diff = l1[i + 1] - l1[i] diff = diff * k pump = pump - diff if pump < 0: pump = 0 except IndexError: pass if ans < pump: ans = pump 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) m = 0 current = 0 for i in range(n): current = current + b[i] m = max(current, m) if i < n - 1: current = max(0, current - (a[i + 1] - a[i]) * k) print(m)
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 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
def build(n, r, time, tension): if n == 1: return tension[0] t = tension[0] max_tension = -1 for i in range(1, n): value = time[i] - time[i - 1] lost = value * r t -= lost if t < 0: t = 0 t += tension[i] if t > max_tension: max_tension = t return max_tension t = int(input()) for i in range(t): l = list(map(int, input().split())) n, r = l[0], l[1] time = list(map(int, input().split())) tension = list(map(int, input().split())) print(build(n, r, time, tension))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for _ in range(t): n, r = input().split() n = int(n) r = int(r) A = list(int(x) for x in input().split()) B = list(int(x) for x in input().split()) x = 0 t_max = 0 for i in range(n): x += B[i] if x > t_max: t_max = x if i <= n - 2: x = max(0, x - r * (A[i + 1] - A[i])) print(t_max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for i in range(t): n, r = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ma = -999 res = 0 for i in range(n - 1): res += b[i] ma = max(ma, res) res -= abs(a[i + 1] - a[i]) * r if res < 0: res = 0 ma = max(ma, res + b[n - 1]) print(ma)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for t in range(int(input())): n, r = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) max_t = b[0] updated_t = b[0] for i in range(1, len(b)): new_r = (a[i] - a[i - 1]) * r if updated_t - new_r < 0: updated_t = 0 + b[i] else: updated_t = updated_t + b[i] - new_r if updated_t > max_t: max_t = updated_t print(max_t)
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 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 VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for t in range(int(input())): n, r = map(int, input().split()) time = [int(x) for x in input().split()] tension = [int(x) for x in input().split()] max = 0 temp_t = 0 i = 0 while i < n: if n == 1: max = tension[0] break elif i > 0: if temp_t >= (time[i] - time[i - 1]) * r: temp_t -= (time[i] - time[i - 1]) * r else: temp_t = 0 temp_t += tension[i] if temp_t > max: max = temp_t i = i + 1 print(max)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
def solve(): n, r = list(map(int, input().split())) alst = list(map(int, input().split())) blst = list(map(int, input().split())) tension = blst[0] maxtension = tension t = alst[0] for a, b in zip(alst[1:], blst[1:]): tension = max(0, tension - (a - t) * r) t = a tension += b maxtension = max(tension, maxtension) return maxtension t = int(input()) for _ in range(t): ans = solve() print(ans)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
for i in range(int(input())): a, b = map(int, input().split()) l = list(map(int, input().split())) l1 = list(map(int, input().split())) k = [] su = 0 for i in range(len(l1)): if i != len(l1) - 1: su += l1[i] c = b * (l[i + 1] - l[i]) k.append(su) su -= c if su < 0: su = 0 else: su += l1[i] k.append(su) print(max(k))
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 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises. On any regular day, Chef does $N$ exercises at times $A_{1}, A_{2}, \ldots, A_{N}$ (in minutes, all distinct) and each exercise provides a tension of $B_{1}, B_{2}, \ldots, B_{N}$ units. In the period between two consecutive exercises, his muscles relax $R$ units of tension per minute. More formally, Chef's tension is described by a number $x$. Before any workouts, $x=0$. When he does a workout at time $A_{i}$, the tension $x$ instantly increases by $B_{i}$. Between workouts, the number $x$ decreases by $R$ units per minute, maximized with $0$. Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $3$ lines of input. The first line will contain $2$ space-separated integers $N, R$, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \ldots, B_{N}$. ------ Output: ------ For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 5\cdot 10^{4}$ $1 ≤ R, B_{i} ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $A_{i - 1} < A_{i}$, for all $2≤ i≤ N$ ----- Sample Input 1 ------ 3 1 2 10 10 2 2 10 11 10 10 3 1 1 2 3 1 2 3 ----- Sample Output 1 ------ 10 18 4 ----- explanation 1 ------ Test Case 1: Since there is only $1$ exercise, the maximum tension is equal to the tension provided by that exercise, i.e, $10$ units. Test Case 2: At time $t = 10$, Chef has $10$ units of tension. From $t = 10$ to $t = 11$, his muscles releases $2$ unit of tension and at $t = 11$, he further gains $10$ units of tension to have total of $10 - 2 + 10 = 18$ units of tension. So the maximum tension Chef feels in his muscles is $18$ units. Test Case 3: At time $t = 1$, Chef has $1$ unit of tension. From $t = 1$ to $t = 2$, his muscles releases $1$ unit of tension and at $t = 2$, he further gains $2$ units of tension to have total of $1 - 1 + 2 = 2$ units of tension. From $t = 2$ to $t = 3$, his muscles releases $1$ unit of tension and at $t = 3$, he further gains $3$ units of tension to have total of $2 - 1 + 3 = 4$ units of tension. So the maximum tension Chef feels in his muscles is $4$ units.
t = int(input()) for i in range(t): c = 0 cc = [] x, y = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(0, len(a)): if i != len(a) - 1: c += b[i] cc.append(c) di = abs(a[i] - a[i + 1]) di = di * y c -= di if c < 0: c = 0 else: c += b[i] cc.append(c) print(max(cc))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him! Input The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters. Output Output the only number — the amount of different substrings of t that start with sbegin and end with send. Examples Input round ro ou Output 1 Input codeforces code forca Output 0 Input abababab a b Output 4 Input aba ab ba Output 1 Note In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab. In the fourth sample identificators intersect.
def calc_lcp(s, sa): n = len(s) rank = [(0) for _ in range(n)] for i in range(n): rank[sa[i]] = i lcp = [(0) for _ in range(n - 1)] h = 0 for i in range(n): if rank[i] < n - 1: while s[i + h] == s[sa[rank[i] + 1] + h]: h += 1 lcp[rank[i]] = h if h > 0: h -= 1 return lcp, rank def index_sort(r): n = len(r) mask = (1 << 32) - 1 ls = [(r[i] << 32 | i) for i in range(n)] ls.sort() res = [(i & mask) for i in ls] return res def suffix_array(s): n = len(s) - 1 rank = [ord(c) for c in s] sa = index_sort(rank) a = [(0) for _ in range(n + 1)] b = [(0) for _ in range(n + 1)] h = 0 while True: for i in range(n): x, y = sa[i + 1], sa[i] b[i + 1] = b[i] if rank[x] > rank[y] or rank[x + h] > rank[y + h]: b[i + 1] += 1 for i in range(n + 1): rank[sa[i]] = b[i] if b[n] == n: break h = max(1, h << 1) for k in range(h, -1, -h): b = [(0) for _ in range(n + 1)] b[0] = k for i in range(k, n + 1): b[rank[i]] += 1 for i in range(n): b[i + 1] += b[i] for i in range(n, -1, -1): r = 0 if sa[i] + k > n else rank[sa[i] + k] b[r] -= 1 a[b[r]] = sa[i] sa, a = a, sa return sa def kmp(s, p): m = len(p) pi = [(0) for _ in range(m)] k = 0 for i in range(1, m): while k > 0 and p[k] != p[i]: k = pi[k - 1] if p[k] == p[i]: k += 1 pi[i] = k k = 0 n = len(s) resp = [] for i in range(n): while k > 0 and p[k] != s[i]: k = pi[k - 1] if p[k] == s[i]: k += 1 if k == m: resp.append(i - m + 1) k = pi[k - 1] return resp def lower_bound(list, value): left = 0 right = len(list) while left < right: mid = int((left + right) / 2) if list[mid] < value: left = mid + 1 else: right = mid return left s = input() start = input() end = input() indStart = kmp(s, start) indEnd = kmp(s, end) if len(indStart) == 0 or len(indEnd) == 0: print(0) else: s += chr(0) sa = suffix_array(s) lcp, rank = calc_lcp(s, sa) ind = rank[indStart[0]] for st in indStart: ind = min(ind, rank[st]) resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end))) while ind < len(lcp) and lcp[ind] >= len(start): ind += 1 resp += len(indEnd) - lower_bound( indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)) ) print(resp)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him! Input The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters. Output Output the only number — the amount of different substrings of t that start with sbegin and end with send. Examples Input round ro ou Output 1 Input codeforces code forca Output 0 Input abababab a b Output 4 Input aba ab ba Output 1 Note In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab. In the fourth sample identificators intersect.
def calc_lcp(s, sa): rank = [(0) for _ in range(len(s))] for i in range(len(s)): rank[sa[i]] = i lcp = [(0) for _ in range(len(s) - 1)] h = 0 for i in range(len(s)): if rank[i] < len(s) - 1: while ( max(i, sa[rank[i] + 1]) + h < len(s) and s[i + h] == s[sa[rank[i] + 1] + h] ): h += 1 lcp[rank[i]] = h if h > 0: h -= 1 return lcp, rank def countinSort(array, key): max_val = max(key) cnt = [(0) for _ in range(max_val + 1)] for i in key: cnt[i] += 1 for i in range(1, len(cnt)): cnt[i] += cnt[i - 1] resp = [(0) for _ in array] for i in range(len(array) - 1, -1, -1): cnt[key[array[i]]] -= 1 resp[cnt[key[array[i]]]] = array[i] return resp def suffix_array(s): s += "\x00" sa = [i for i in range(len(s))] sa = countinSort(sa, [ord(c) for c in s]) rank = 0 ranks = [(0) for _ in range(len(s))] for i in range(1, len(s)): if s[sa[i - 1]] != s[sa[i]]: rank += 1 ranks[sa[i]] = rank k = 1 while k < len(s): sa = countinSort( sa, [(ranks[i + k] if i + k < len(s) else 0) for i in range(len(s))] ) sa = countinSort(sa, ranks) rank = 0 rank_new = [(0) for _ in range(len(s))] for i in range(1, len(s)): if ( ranks[sa[i - 1]] != ranks[sa[i]] or ranks[sa[i - 1] + k] != ranks[sa[i] + k] ): rank += 1 rank_new[sa[i]] = rank ranks = rank_new k *= 2 return sa[1:] def kmp(s, p): pi = [(0) for _ in range(len(p))] k = 0 for i in range(1, len(p)): while k > 0 and p[k] != p[i]: k = pi[k - 1] if p[k] == p[i]: k += 1 pi[i] = k k = 0 resp = [] for i in range(len(s)): while k > 0 and p[k] != s[i]: k = pi[k - 1] if p[k] == s[i]: k += 1 if k == len(p): resp.append(i - len(p) + 1) k = pi[k - 1] return resp def lower_bound(list, value): left = 0 right = len(list) while left < right: mid = int((left + right) / 2) if list[mid] < value: left = mid + 1 else: right = mid return left s = input() start = input() end = input() indStart = kmp(s, start) indEnd = kmp(s, end) if len(indStart) == 0 or len(indEnd) == 0: print(0) else: sa = suffix_array(s) lcp, rank = calc_lcp(s, sa) ind = rank[indStart[0]] for st in indStart: ind = min(ind, rank[st]) resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end))) while ind < len(lcp) and lcp[ind] >= len(start): ind += 1 resp += len(indEnd) - lower_bound( indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)) ) print(resp)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him! Input The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and send identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters. Output Output the only number — the amount of different substrings of t that start with sbegin and end with send. Examples Input round ro ou Output 1 Input codeforces code forca Output 0 Input abababab a b Output 4 Input aba ab ba Output 1 Note In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab. In the fourth sample identificators intersect.
def calc_lcp(s, sa): rank = [(0) for _ in range(len(s))] for i in range(len(s)): rank[sa[i]] = i lcp = [(0) for _ in range(len(s) - 1)] h = 0 for i in range(len(s)): if rank[i] < len(s) - 1: while s[i + h] == s[sa[rank[i] + 1] + h]: h += 1 lcp[rank[i]] = h if h > 0: h -= 1 return lcp, rank def suffix_array(s): n = len(s) na = max(n, 256) sa = [(0) for _ in range(n)] top = [(0) for _ in range(na)] rank = [(0) for _ in range(n)] sa_new = [(0) for _ in range(n)] rank_new = [(0) for _ in range(n)] for i in range(n): rank[i] = ord(s[i]) top[rank[i]] += 1 for i in range(1, na): top[i] += top[i - 1] for i in range(n): top[rank[i]] -= 1 sa[top[rank[i]]] = i k = 1 while k < n: for i in range(n): j = sa[i] - k if j < 0: j += n sa_new[top[rank[j]]] = j top[rank[j]] += 1 rank_new[sa_new[0]] = 0 top[0] = 0 cnt = 0 for i in range(1, n): if ( rank[sa_new[i]] != rank[sa_new[i - 1]] or rank[sa_new[i] + k] != rank[sa_new[i - 1] + k] ): cnt += 1 top[cnt] = i rank_new[sa_new[i]] = cnt sa, sa_new = sa_new, sa rank, rank_new = rank_new, rank if cnt == n - 1: break k *= 2 return sa def kmp(s, p): pi = [(0) for _ in range(len(p))] k = 0 for i in range(1, len(p)): while k > 0 and p[k] != p[i]: k = pi[k - 1] if p[k] == p[i]: k += 1 pi[i] = k k = 0 resp = [] for i in range(len(s)): while k > 0 and p[k] != s[i]: k = pi[k - 1] if p[k] == s[i]: k += 1 if k == len(p): resp.append(i - len(p) + 1) k = pi[k - 1] return resp def lower_bound(list, value): left = 0 right = len(list) while left < right: mid = int((left + right) / 2) if list[mid] < value: left = mid + 1 else: right = mid return left s = input() start = input() end = input() indStart = kmp(s, start) indEnd = kmp(s, end) if len(indStart) == 0 or len(indEnd) == 0: print(0) else: s += chr(0) sa = suffix_array(s) lcp, rank = calc_lcp(s, sa) ind = rank[indStart[0]] for st in indStart: ind = min(ind, rank[st]) resp = len(indEnd) - lower_bound(indEnd, sa[ind] + max(0, len(start) - len(end))) while ind < len(lcp) and lcp[ind] >= len(start): ind += 1 resp += len(indEnd) - lower_bound( indEnd, sa[ind] + max(0, max(lcp[ind - 1] + 1, len(start)) - len(end)) ) print(resp)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, word): def make(i, j, string, f=1, visited=None): if not visited: visited = [] visited = [[k, l] for k, l in visited if board[k][l] in string] if [i, j] in visited or not word.startswith(string): return False if not f: if 0 <= i < len(board) and 0 <= j < len(board): string += board[i][j] else: return string == word if string == word: return True visited.append([i, j]) return any( [ make(i, j + 1, string, 0, visited), make(i, j - 1, string, 0, visited), make(i - 1, j, string, 0, visited), make(i + 1, j, string, 0, visited), make(i - 1, j + 1, string, 0, visited), make(i - 1, j - 1, string, 0, visited), make(i + 1, j + 1, string, 0, visited), make(i + 1, j - 1, string, 0, visited), ] ) return any( make(i, k, l) for i, j in enumerate(board) for k, l in enumerate(j) if l == word[0] )
FUNC_DEF FUNC_DEF NUMBER NONE IF VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR IF LIST VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, word, trail=None): if not word: return True if not trail: board = { (row, col): item for row, line in enumerate(board) for col, item in enumerate(line) } valid_first = [k for k, v in list(board.items()) if v == word[0]] return any(find_word(board, word[1:], [coord]) for coord in valid_first) else: x, y = trail[-1] neighbors = [ (x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1), ] valid_next = [ p for p in neighbors if p in board and p not in trail and board[p] == word[0] ] return any(find_word(board, word[1:], trail + [coord]) for coord in valid_next)
FUNC_DEF NONE IF VAR RETURN NUMBER IF VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR LIST VAR VAR VAR
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, word): P = {(i, j): e for j, r in enumerate(board) for i, e in enumerate(r)} chains, word = [[p] for p in P if P[p] == word[0]], word[1:] while word and chains: c, word, newchains = word[0], word[1:], [] for chain in chains: i, j = chain[-1] neighbours = [(i + a, j + b) for a in range(-1, 2) for b in range(-1, 2)] newchains += [ (chain + [p]) for p in neighbours if c == P.get(p, None) and p not in chain ] chains = newchains return len(chains) > 0
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR NONE VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, word): grid = [(l + [""]) for l in board] + [[""] * (len(board[0]) + 1)] def rc(x, y, i): if i == len(word): return True if grid[x][y] != word[i]: return False grid[x][y] = "" r = any(rc(x + u, y + v, i + 1) for u in range(-1, 2) for v in range(-1, 2)) grid[x][y] = word[i] return r return any(rc(x, y, 0) for x in range(len(board)) for y in range(len(board[x])))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR LIST STRING VAR VAR LIST BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_neighbors(row_c, col_c, board, word, path): if len(path) == len(word): return True, path y_d = [i for i in range(max(0, row_c - 1), min(len(board), row_c + 2))] x_d = [i for i in range(max(0, col_c - 1), min(len(board), col_c + 2))] neighbors = [(y, x) for y in y_d for x in x_d] for index in neighbors: if index not in path and board[index[0]][index[1]] == word[len(path)]: path.append(index) result = find_neighbors(index[0], index[1], board, word, path) if result[0]: return True, path else: path = result[1] path = path[:-1] return False, path def find_word(board, word): for row_c, row in enumerate(board): for col_c, l in enumerate(row): path = [] if l in word and find_neighbors(row_c, col_c, board, word, path)[0]: return True return False
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def check_valid(letter_locations, board, word): if len(letter_locations) == len(word): return True x, y = letter_locations[-1] next_char = word[len(letter_locations)] valid = False for i in range(-1, 2): for j in range(-1, 2): if i == 0 and j == 0: continue if y + i < 0 or x + j < 0 or y + i >= len(board) or x + j >= len(board[0]): continue if ( board[y + i][x + j] == next_char and (x + j, y + i) not in letter_locations ): letter_locations.append((x + j, y + i)) valid = valid or check_valid(letter_locations, board, word) letter_locations.pop() return valid def find_word(board, word): valid = False if word == "": return True for y, row in enumerate(board): for x, letter in enumerate(row): if letter == word[0]: valid = valid or check_valid([(x, y)], board, word) return valid
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR STRING RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, word, cursor=None): if not word: return True workboard = [row[:] for row in board] sy, sx, ey, ex = 0, 0, len(board), len(board[0]) if cursor: cx, cy = cursor workboard[cy][cx] = "-" sy = max(sy, cy - 1) sx = max(sx, cx - 1) ey = min(ey, cy + 2) ex = min(ex, cx + 2) for y, row in enumerate(workboard[:ey][sy:]): for x, cell in enumerate(row[:ex][sx:]): if cell == word[0] and find_word(workboard, word[1:], (x + sx, y + sy)): return True return False
FUNC_DEF NONE IF VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER
Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.: ```python [ ["I","L","A","W"], ["B","N","G","E"], ["I","U","A","O"], ["A","S","R","L"] ] ``` Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells. For example, in the above board "BINGO", "LINGO", and "ILNBIA" would all be valid guesses, while "BUNGIE", "BINS", and "SINUS" would not. Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules. Test cases will provide various array and string sizes (squared arrays up to 150x150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it's a valid guess.
def find_word(board, s): def seekFrom(x, y): bd[x][y] = "" ret = next(dfs(x, y), False) bd[x][y] = s[0] return ret def dfs(x, y, i=1): if i == len(s): yield True else: candidates = ( (x + dx, y + dy) for dx in range(-1, 2) for dy in range(-1, 2) if (dx or dy) and 0 <= x + dx < len(bd) and 0 <= y + dy < len(bd[0]) and bd[x + dx][y + dy] == s[i] ) for a, b in candidates: bd[a][b] = "" yield from dfs(a, b, i + 1) bd[a][b] = s[i] bd = list(map(list, board)) return any( seekFrom(x, y) for x, r in enumerate(board) for y, c in enumerate(r) if c == s[0] )
FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF NUMBER IF VAR FUNC_CALL VAR VAR EXPR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
from itertools import permutations class Solution: digitMap = {} wordsRev = [] resultRev = [] limit = 0 def findSolution(self, n, carry=0): if n == self.limit: leadingDigitChars = [w[len(w) - 1] for w in self.wordsRev] leadingDigitChars.append(self.resultRev[len(self.resultRev) - 1]) leadingDigits = [self.digitMap[c] for c in leadingDigitChars] return 0 not in leadingDigits availableDigits = [n for n in range(0, 10) if n not in self.digitMap.values()] digitChars = [w[n] for w in self.wordsRev if n < len(w)] resultChar = self.resultRev[n] knownDigitChars = list(filter(lambda x: x in self.digitMap, digitChars)) unknownDigitChars = [w for w in digitChars if w not in knownDigitChars] digits = [] resultDigit = -1 if len(knownDigitChars) > 0: digits = [self.digitMap[k] for k in knownDigitChars] if resultChar in self.digitMap: resultDigit = self.digitMap[resultChar] for d in permutations(availableDigits, len(unknownDigitChars)): s = sum(digits) + sum(d) + carry c = int(s / 10) s %= 10 if resultDigit >= 0 and s == resultDigit: for i, v in enumerate(d): self.digitMap[unknownDigitChars[i]] = v if self.findSolution(n + 1, c): return True else: for i, v in enumerate(d): if unknownDigitChars[i] in self.digitMap: del self.digitMap[unknownDigitChars[i]] elif ( resultDigit < 0 and ( resultChar not in unknownDigitChars and s not in d or resultChar in unknownDigitChars and s in d ) and s in availableDigits ): for i, v in enumerate(d): self.digitMap[unknownDigitChars[i]] = v if resultChar not in unknownDigitChars: self.digitMap[resultChar] = s elif s != self.digitMap[resultChar]: for i, v in enumerate(d): if unknownDigitChars[i] in self.digitMap: del self.digitMap[unknownDigitChars[i]] continue if self.findSolution(n + 1, c): return True else: for i, v in enumerate(d): if unknownDigitChars[i] in self.digitMap: del self.digitMap[unknownDigitChars[i]] if resultChar in self.digitMap: del self.digitMap[resultChar] return False def isSolvable(self, words: List[str], result: str) -> bool: self.digitMap = {} self.wordsRev = [w[::-1] for w in words] self.resultRev = result[::-1] wordMax = max([len(w) for w in words]) if wordMax > len(result): return False self.limit = max(wordMax, len(result)) return self.findSolution(0, 0)
CLASS_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: allWords = words + [result] firstChars = set(word[0] for word in allWords) maxWordLength = max(list(map(len, allWords))) if len(result) < maxWordLength: return False def dfs(charIdx, wordIdx, carry, visited, char2Digit): if charIdx == maxWordLength: return carry == 0 if wordIdx == len(allWords): sums = sum( char2Digit[word[-charIdx - 1]] if charIdx < len(word) else 0 for word in words ) sums += carry if sums % 10 == char2Digit[result[-charIdx - 1]]: return dfs(charIdx + 1, 0, sums // 10, visited, char2Digit) else: return False currWord = allWords[-wordIdx - 1] if charIdx >= len(currWord): return dfs(charIdx, wordIdx + 1, carry, visited, char2Digit) currChar = currWord[-charIdx - 1] if currChar in char2Digit: return dfs(charIdx, wordIdx + 1, carry, visited, char2Digit) else: startDigit = 1 if currChar in firstChars else 0 for digit in range(startDigit, 10): if digit in list(char2Digit.values()): continue visited.add(digit) char2Digit[currChar] = digit if dfs(charIdx, wordIdx, carry, visited, char2Digit.copy()): return True visited.remove(digit) return False return dfs(0, 0, 0, set(), {})
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR DICT VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: n = len(words) mapper = {} maxlen = 0 for w in words: maxlen = max(maxlen, len(w)) for c in w: mapper[c] = -1 if len(result) < maxlen or len(result) - maxlen > 1: return False for c in result: mapper[c] = -1 used = [0] * 10 def solve(i, j, s): if j == n: l = len(result) - i if l < 0: if s != 0: return False for w in words: if mapper[w[0]] == 0: return False if mapper[result[0]] == 0: return False return True c, s = divmod(s, 10) if mapper[result[l]] >= 0: if mapper[result[l]] == s: return solve(i + 1, 0, c) return False if used[s]: return False mapper[result[l]] = s used[s] = 1 res = solve(i + 1, 0, c) mapper[result[l]] = -1 used[s] = 0 return res w = words[j] l = len(w) - i if l < 0: return solve(i, j + 1, s) if mapper[w[l]] >= 0: return solve(i, j + 1, s + mapper[w[l]]) for k in range(10): if used[k]: continue used[k] = 1 mapper[w[l]] = k if solve(i, j + 1, s + k): return True used[k] = 0 mapper[w[l]] = -1 return False return solve(1, 0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: max_len = len(result) if max(len(w) for w in words) > max_len: return False level = [ [word[-i - 1] for word in words if i < len(word)] for i in range(max_len) ] level_set = [set([result[-i - 1]] + level[i]) for i in range(max_len)] used_digits = set() nonzero_chars = set(word[0] for word in words + [result]) assignments = {} def isSAT(eqn_index, carry): if eqn_index >= max_len: return carry == 0 remaining_terms = [] for t in level_set[eqn_index]: if t not in assignments: for guess in range(t in nonzero_chars, 10): if guess in used_digits: continue assignments[t] = guess used_digits.add(guess) if isSAT(eqn_index, carry): return True del assignments[t] used_digits.remove(guess) return False s = sum(assignments[c] for c in level[eqn_index]) + carry if s % 10 != assignments[result[-eqn_index - 1]]: return False return isSAT(eqn_index + 1, s // 10) return isSAT(0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR LIST VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: self.n = len(result) self.levels = [] self.alldigit = {} self.allch = [] self.result = result self.nonzero = set() for w in words: self.nonzero.add(w[0]) self.nonzero.add(result[0]) for r in range(len(result)): l = [] for w in words: if r < len(w): l.append(w[len(w) - r - 1]) l.append(result[len(result) - 1 - r]) self.levels.append(l) self.occupy = set() self.checkdigit = {} if len(self.levels[-1]) == 1: self.alldigit[self.levels[-1][0]] = 1 self.occupy.add(1) for l in range(len(self.levels)): newch = set(self.levels[l]) - set(self.allch) self.allch.extend(newch) self.checkdigit[len(self.allch)] = l + 1 if self.tryone(0, len(self.allch)): return True else: return False def tryone(self, level, maxl): flag = False if maxl == level: if self.check(self.checkdigit[level]): return True else: return False if level in self.checkdigit: if not self.check(self.checkdigit[level]): return False else: pass if self.allch[level] in self.alldigit: if self.tryone(level + 1, maxl): return True else: return False for i in range(10): if i not in self.occupy: if i != 0 or self.allch[level] not in self.nonzero: self.occupy.add(i) self.alldigit[self.allch[level]] = i if self.tryone(level + 1, maxl): return True del self.alldigit[self.allch[level]] self.occupy.remove(i) return False def check(self, level): plus = 0 for i in range(level): levelsum = 0 for j in range(len(self.levels[i]) - 1): levelsum += self.alldigit[self.levels[i][j]] if (levelsum + plus) % 10 != self.alldigit[self.levels[i][-1]]: return False else: plus = int((levelsum + plus) / 10) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: if max([len(w) for w in words]) > len(result): return False lead_letters = set([w[0] for w in words + [result]]) letters = [] for k in range(1, len(result) + 1): for w in words: if len(w) >= k and w[-k] not in [x[0] for x in letters]: letters.append((w[-k], k)) letters.append((result[-k], -k)) letter2num = {} num2letter = [""] * 10 nums = "0123456789" def helper(k): if k == len(letters): return True letter, digit = letters[k][0], letters[k][1] if digit < 0: left = sum( [int("".join([letter2num[ch] for ch in w[digit:]])) for w in words] ) if left < 10 ** (-digit - 1): return False num = int(str(left)[digit]) if letter not in letter2num and not num2letter[num]: letter2num[letter] = str(num) num2letter[num] = letter if helper(k + 1): return True letter2num.pop(letter) num2letter[int(num)] = "" elif ( num2letter[num] != letter or letter in letter2num and letter2num[letter] != str(num) ): return False else: return helper(k + 1) elif letter not in letter2num: num_range = nums if letter not in lead_letters else nums[1:] for num in num_range: if not num2letter[int(num)]: letter2num[letter] = num num2letter[int(num)] = letter if helper(k + 1): return True letter2num.pop(letter) num2letter[int(num)] = "" return False else: return helper(k + 1) return helper(0)
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST STRING NUMBER ASSIGN VAR STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: def solve(i, j, carry): if j == len(words): csum = carry for k in range(len(words)): csum += 0 if i >= len(words[k]) else assign[words[k][i]] if i >= len(result): return False if result[i] in assign: return csum % 10 == assign[result[i]] and solve( i + 1, 0, csum // 10 ) else: if ( csum % 10 in assign.values() or csum % 10 == 0 and i == len(result) - 1 ): return False assign[result[i]] = csum % 10 ret = solve(i + 1, 0, csum // 10) del assign[result[i]] return ret if i == len(result): return ( i >= max(len(w) for w in words) and carry == 0 and all(assign[w[len(w) - 1]] != 0 for w in words + [result]) ) if i >= len(words[j]) or words[j][i] in assign: return solve(i, j + 1, carry) for val in range(10): if val == 0 and i == len(words[j]) - 1: continue if val not in assign.values(): assign[words[j][i]] = val ret = solve(i, j + 1, carry) if ret: return True del assign[words[j][i]] return False result = result[::-1] words = [w[::-1] for w in words] assign = dict() return solve(0, 0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR LIST VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR RETURN NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: allwords = words + [result] n = max(list(map(len, allwords))) firstc = set(word[0] for word in allwords) if len(result) < n: return False def dfs(charidx, wordidx, carry, visited, char2digit): if charidx == n: return carry == 0 if wordidx == len(allwords): tot = ( sum( char2digit[word[~charidx]] if charidx < len(word) else 0 for word in words ) + carry ) if tot % 10 == char2digit[result[~charidx]]: return dfs(charidx + 1, 0, tot // 10, visited, char2digit) else: return False if wordidx < len(words) and charidx >= len(words[wordidx]): return dfs(charidx, wordidx + 1, carry, visited, char2digit) c = allwords[wordidx][~charidx] first = 1 if c in firstc else 0 if c in char2digit: return dfs(charidx, wordidx + 1, carry, visited, char2digit) else: for d in range(first, 10): if d not in visited: visited.add(d) char2digit[c] = d if dfs(charidx, wordidx + 1, carry, visited, char2digit): return True del char2digit[c] visited.remove(d) return False return dfs(0, 0, 0, set(), {})
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR DICT VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: longest_word = max([len(word) for word in words]) if len(result) != longest_word and len(result) != longest_word + 1: return False result_indices = [] acc = 0 all_chars = [] front_indices = [] for i in range(1, longest_word + 1): for word in words: if i == len(word): front_indices.append(acc) if i <= len(word): all_chars.append(word[i * -1]) acc += 1 if i == len(result): front_indices.append(acc) result_indices.append(acc) acc += 1 all_chars.append(result[i * -1]) if len(result) > longest_word: result_indices.append(acc) front_indices.append(acc) all_chars.append(result[0]) self.words = words self.result = result self.result_indices = result_indices self.all_chars = all_chars self.mappings = {} self.used_chars = set() self.front_indices = front_indices return self.backtrack(0, 0) def backtrack(self, current_i: int, carry: int) -> bool: if current_i == len(self.all_chars): if self.mappings[self.result[0]] == 0: return False return True cur_char = self.all_chars[current_i] if current_i in self.result_indices: code, new_carry = self.verify(self.result_indices.index(current_i), carry) if code == 0: return False else: if self.backtrack(current_i + 1, new_carry): return True if code == 2: self.used_chars.remove(self.mappings[cur_char]) del self.mappings[cur_char] return False if cur_char in self.mappings: if current_i in self.front_indices and self.mappings[cur_char] == 0: return False return self.backtrack(current_i + 1, carry) for i in range(10): if current_i in self.front_indices and i == 0: continue if i not in self.used_chars: self.mappings[cur_char] = i self.used_chars.add(i) if self.backtrack(current_i + 1, carry): return True del self.mappings[cur_char] self.used_chars.remove(i) return False def verify(self, index: int, carry: int) -> (int, int): cur_sum = carry for word in self.words: if index < len(word): cur_sum += self.mappings[word[index * -1 - 1]] carry = int(cur_sum / 10) cur_sum = cur_sum % 10 result_char = self.result[index * -1 - 1] if result_char in self.mappings: if self.mappings[result_char] != cur_sum: return 0, 0 else: return 1, carry else: if cur_sum in self.used_chars: return 0, 0 self.mappings[result_char] = cur_sum self.used_chars.add(cur_sum) return 2, carry
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER NUMBER RETURN NUMBER VAR IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: words.append(result) R, C = len(words), max(list(map(len, words))) used = {} used_d = [None] * 10 def backtrack(row, col, bal): if col >= C: return bal == 0 and all(used[w[0]] != 0 for w in words) if row == R: return bal % 10 == 0 and backtrack(0, col + 1, bal // 10) word = words[row] if col >= len(word): return backtrack(row + 1, col, bal) letter = word[-1 - col] sign = 1 if row < R - 1 else -1 if letter in used: return backtrack(row + 1, col, bal + sign * used[letter]) else: for d, ad in enumerate(used_d): if ad is None and (d or col != len(word) - 1): used[letter] = d used_d[d] = letter if backtrack(row + 1, col, bal + sign * d): return True used_d[d] = None del used[letter] return False return backtrack(0, 0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NONE NUMBER FUNC_DEF IF VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR NONE VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: start = set() for word in words + [result]: if len(word) > 1: start.add(word[0]) n = max(list(map(len, words + [result]))) if len(result) < n: return False def dfs(idx, i, carry, visited, mp): if idx == n: return carry == 0 if i == len(words) + 1: sums = ( sum(mp[word[-idx - 1]] if idx < len(word) else 0 for word in words) + carry ) if sums % 10 == mp[result[-idx - 1]]: carry = sums // 10 return dfs(idx + 1, 0, carry, visited, mp) return False if i < len(words) and idx >= len(words[i]): return dfs(idx, i + 1, carry, visited, mp) tmp = words + [result] ch = tmp[i][-idx - 1] if ch in mp: return dfs(idx, i + 1, carry, visited, mp) begin = 0 if ch in start: begin = 1 for x in range(begin, 10): if x not in visited: visited.add(x) mp[ch] = x if dfs(idx, i + 1, carry, visited, mp.copy()): return True visited.remove(x) return False return dfs(0, 0, 0, set(), {})
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR DICT VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution(object): def isSolvable(self, words, result): words.append(result) R, C = len(words), max(map(len, words)) assigned = {} assigned_inv = [None] * 10 def search(column, row, bal): if column >= C: return bal == 0 if row == R: return bal % 10 == 0 and search(column + 1, 0, bal // 10) word = words[row] if column >= len(word): return search(column, row + 1, bal) letter = word[~column] sign = 1 if row < R - 1 else -1 if letter in assigned: if assigned[letter] or len(word) == 1 or column != len(word) - 1: return search(column, row + 1, bal + sign * assigned[letter]) return False else: for d, ad in enumerate(assigned_inv): if ad is None and (d or len(word) == 1 or column != len(word) - 1): assigned_inv[d] = letter assigned[letter] = d if search(column, row + 1, bal + sign * d): return True assigned_inv[d] = None del assigned[letter] return False return search(0, 0, 0)
CLASS_DEF VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NONE NUMBER FUNC_DEF IF VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR NONE VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: def backtrack(ch_pos_to_end, num_choices, iter_idx, lhs, rhs): if ch_pos_to_end > len(result): return True mul = pow(10, ch_pos_to_end - 1) if iter_idx < len(words): word = words[iter_idx] idx = len(word) - ch_pos_to_end if idx >= 0: if word[idx] not in num_mappings: for n in num_choices: if n == 0 and word[idx] == word[0]: continue num_mappings[word[idx]] = n if backtrack( ch_pos_to_end, num_choices - {n}, iter_idx + 1, lhs + num_mappings[word[idx]] * mul, rhs, ): return True del num_mappings[word[idx]] else: return backtrack( ch_pos_to_end, num_choices, iter_idx + 1, lhs + num_mappings[word[idx]] * mul, rhs, ) else: return backtrack(ch_pos_to_end, num_choices, iter_idx + 1, lhs, rhs) elif iter_idx == len(words): idx = len(result) - ch_pos_to_end if result[idx] not in num_mappings: for n in num_choices: if n == 0 and result[idx] == result[0]: continue new_rhs = rhs + n * mul if not str(lhs).endswith(str(new_rhs)): continue num_mappings[result[idx]] = n if backtrack( ch_pos_to_end + 1, num_choices - {n}, 0, lhs, new_rhs ): return True del num_mappings[result[idx]] else: added = num_mappings[result[idx]] if not str(lhs).endswith(str(rhs + added * mul)): return False return backtrack( ch_pos_to_end + 1, num_choices, 0, lhs, rhs + added * mul ) return False max_len_words = max([len(w) for w in words]) if max_len_words > len(result): return False num_mappings = {} nums = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} return backtrack(1, nums, 0, 0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR RETURN NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER VAR
Given an equation, represented by words on left side and the result on right side. You need to check if the equation is solvable under the following rules: Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result).  Return True if the equation is solvable otherwise return False.   Example 1: Input: words = ["SEND","MORE"], result = "MONEY" Output: true Explanation: Map 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2' Such that: "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652 Example 2: Input: words = ["SIX","SEVEN","SEVEN"], result = "TWENTY" Output: true Explanation: Map 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4 Such that: "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214 Example 3: Input: words = ["THIS","IS","TOO"], result = "FUNNY" Output: true Example 4: Input: words = ["LEET","CODE"], result = "POINT" Output: false   Constraints: 2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.
def make_zip(lists): i = 0 maxlength = max(len(l) for l in lists) ans = [] while i < maxlength: ans.append([l[i] for l in lists if i < len(l)]) i += 1 return ans def make_order(words, result): order = [] formulas = {} for i in range(len(result)): for ch in [*words[i], result[i]]: if ch not in order: order.append(ch) order.append(f"c{i + 1}") lhs = words[i] rhs = result[i] if i > 0: lhs.append(f"c{i}") excess = f"c{i + 1}" formulas[excess] = lhs, rhs, excess return order, formulas def find_free(repl, ch, nonzero): start = 1 if ch in nonzero else 0 choices = set(range(start, 10)) for x, y in repl.items(): if y in choices: choices.remove(y) yield from choices def find_value(n, repl, crepl): return repl[n] if n in repl else crepl[n] def find_excess(lhs, rhs, repl, crepl): return sum(find_value(x, repl, crepl) for x in lhs) - find_value(rhs, repl, crepl) def walk_solutions(puzzle, index, repl, crepl): words, result, order, formulas, nonzero = puzzle if index == len(order): last_ch = order[-1] if crepl[last_ch] != 0: return yield repl else: ch = order[index] if len(ch) == 1: for digit in find_free(repl, ch, nonzero): repl[ch] = digit yield from walk_solutions(puzzle, index + 1, repl, crepl) del repl[ch] else: formula = formulas[ch] lhs, rhs, _ = formula excess = find_excess(lhs, rhs, repl, crepl) if excess >= 0 and excess % 10 == 0: crepl[ch] = excess // 10 yield from walk_solutions(puzzle, index + 1, repl, crepl) del crepl[ch] def is_solvable(words, result): n = len(words) maxword = 10 ** max(len(w) for w in words) - 1 minresult = 10 ** (len(result) - 1) if n * maxword < minresult: return False if not all(len(w) <= len(result) for w in words): return False nonzero = {m[0] for m in [*words, result] if len(m) > 1} result = [ch for ch in result[::-1]] words = make_zip([w[::-1] for w in words]) while len(words) < len(result): words.append([]) order, formulas = make_order(words, result) puzzle = words, result, order, formulas, nonzero return any(walk_solutions(puzzle, 0, {}, {})) class Solution: def isSolvable(self, words: List[str], result: str) -> bool: return is_solvable(words, result)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR LIST VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR VAR FUNC_DEF RETURN VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER RETURN EXPR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR LIST VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER DICT DICT CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(maze): def rec(i, j): if not i in range(len(maze)): return True if not j in range(len(maze[i])): return True if maze[i][j] == "#": return False maze[i][j] = "#" return any( rec(i + k, j + l) for k in (-1, 0, 1) for l in (-1, 0, 1) if bool(k) != bool(l) ) pos = None for i, line in enumerate(maze): for j, c in enumerate(line): if c == "k": if pos: raise Exception("There should not be multiple Kates") else: pos = i, j if not pos: raise Exception("There should be a Kate") maze = list(map(list, maze)) return rec(*pos)
FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR STRING RETURN NUMBER ASSIGN VAR VAR VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(maze): W = len(maze[0]) S = W * len(maze) frontier, unseen = set(), set() for position, content in enumerate("".join(maze)): (frontier.add, unseen.add, id)["k #".index(content)](position) assert len(frontier) == 1 while frontier: position = frontier.pop() if min(position, S - position) < W or -~position % W < 2: return True for way in (position - W, position + W, position - 1, position + 1): if way in unseen: frontier.add(way) unseen.remove(way) return False
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(maze): vector, w = list("".join(maze)), len(maze[0]) assert vector.count("k") == 1 while "k" in vector: for p, cell in enumerate(vector): if cell == "k": if min(p, len(vector) - p) < w or -~p % w < 2: return True for direction in (-w, 1, w, -1): if vector[p + direction] == " ": vector[p + direction] = "k" vector[p] = "+" return False
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER WHILE STRING VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR STRING RETURN NUMBER
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(m): maze = [[x for x in row] for row in m] if not check_edge(maze): return False if not check_kate(maze): raise Exception("There should no be multiple Kates") kate = find_kate(maze) return find_way(maze, kate, kate) def find_way(maze, position, previous_position, trace=[]): if is_exit(maze, position): return True possible_ways = find_possible_ways(maze, position, previous_position) for possible_way in possible_ways: if possible_way not in trace: new_maze = move_kate(maze, possible_way, position) if find_way(new_maze, possible_way, position, trace + [possible_way]): return True return False def find_possible_ways(maze, position, previous_position): r, c = position possible_ways = [] for pr, pc in [(r + 1, c), (r, c + 1), (r - 1, c), (r, c - 1)]: if pr >= 0 and pr < len(maze) and pc >= 0 and pc < len(maze[0]): if maze[pr][pc] == " " and (pr, pc) != previous_position: possible_ways += [(pr, pc)] return possible_ways def find_kate(maze): for r in range(len(maze)): for c in range(len(maze[0])): if maze[r][c] == "k": return r, c def move_kate(maze, new_position, previous_position): nr, nc = new_position pr, pc = previous_position new_m = maze[:pr] + [maze[pr][:pc] + [" "] + maze[pr][pc + 1 :]] + maze[pr + 1 :] return new_m[:nr] + [new_m[nr][:nc] + ["k"] + new_m[nr][nc + 1 :]] + new_m[nr + 1 :] def check_edge(maze): edges = maze[0] + maze[-1] + tr(maze)[0] + tr(maze)[-1] if " " in edges or "k" in edges: return True return False def check_kate(maze): count = 0 for r in range(len(maze)): for c in range(len(maze[0])): if maze[r][c] == "k": count += 1 return count == 1 def is_exit(maze, position): width, length = len(maze) - 1, len(maze[0]) - 1 if width in position or length in position or 0 in position: return True return False def tr(mtx): return [list(x) for x in zip(*mtx)]
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF LIST IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR LIST VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR VAR LIST VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST BIN_OP BIN_OP VAR VAR VAR LIST STRING VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR LIST BIN_OP BIN_OP VAR VAR VAR LIST STRING VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF STRING VAR STRING VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(board): def make(i, j, visited=None): if not visited: visited = [] if ( [i, j] in visited or not (0 <= i < len(board) and 0 <= j < len(board[0])) or board[i][j] == "#" ): return False visited.append([i, j]) if i == 0 or j == 0 or i == len(board) - 1 or j == len(board[0]) - 1: return True return any( [ make(i + 1, j, visited), make(i - 1, j, visited), make(i, j + 1, visited), make(i, j - 1, visited), ] ) li = [[i, k] for i, j in enumerate(board) for k, l in enumerate(j) if l == "k"] if len(li) > 1: raise return make(li[0][0], li[0][1])
FUNC_DEF FUNC_DEF NONE IF VAR ASSIGN VAR LIST IF LIST VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR STRING RETURN NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
MOVES = {(0, 1), (0, -1), (1, 0), (-1, 0)} def has_exit(maze): posSet = { (x, y) for x in range(len(maze)) for y in range(len(maze[x])) if maze[x][y] == "k" } if len(posSet) != 1: raise ValueError("There shouldn't be more than one kate") seen = set(posSet) while posSet: x, y = posSet.pop() if any( not (0 <= x + dx < len(maze) and 0 <= y + dy < len(maze[x + dx])) for dx, dy in MOVES ): return True neighbors = { (x + dx, y + dy) for dx, dy in MOVES if 0 <= x + dx < len(maze) and 0 <= y + dy < len(maze[x + dx]) and maze[x + dx][y + dy] == " " and (x + dx, y + dy) not in seen } posSet |= neighbors seen |= neighbors return False
ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(maze): N, M = len(maze) + 2, len(maze[0]) + 2 maze = ["b" * M] + [("b" + row + "b") for row in maze] + ["b" * M] q = [(i, j) for i, row in enumerate(maze) for j, c in enumerate(row) if c == "k"] if len(q) != 1: raise Exception("Wrong number of Kates") v = set(q) while q: i, j = q.pop() nbs = [ (i + a, j + b, maze[i + a][j + b]) for a in range(-1, 2) for b in range(-1, 2) if bool(a) != bool(b) ] for ni, nj, nv in nbs: if nv == "b": return True if nv == "#" or (ni, nj) in v: continue q.append((ni, nj)) or v.add((ni, nj)) return False
FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP STRING VAR BIN_OP BIN_OP STRING VAR STRING VAR VAR LIST BIN_OP STRING VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR STRING RETURN NUMBER IF VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER
Kate constantly finds herself in some kind of a maze. Help her to find a way out!. For a given maze and Kate's position find if there is a way out. Your function should return True or False. Each maze is defined as a list of strings, where each char stays for a single maze "cell". ' ' (space) can be stepped on, '#' means wall and 'k' stays for Kate's starting position. Note that the maze may not always be square or even rectangular. Kate can move left, up, right or down only. There should be only one Kate in a maze. In any other case you have to throw an exception. Example input ============= ``` ['# ##', '# k#', '####'] ``` Example output ============== True Example input ============= ``` ['####'. '# k#', '####'] ``` Example output ============== False
def has_exit(maze): start = [ [i, maze[i].find("k")] for i in range(len(maze)) if maze[i].find("k") != -1 ] if len(start) != 1: raise Exception("There should no be multiple Kates") if backtracking(maze, start[0][0], start[0][1], set()): return True return False def backtracking(maze, y, x, explored): if maze[y][x] == "#": explored.add((y, x)) return False if (y, x) in explored: return False if x in {0, len(maze[0]) - 1} or y in {0, len(maze) - 1}: return True explored.add((y, x)) found = ( backtracking(maze, y - 1, x, explored) or backtracking(maze, y, x + 1, explored) or backtracking(maze, y + 1, x, explored) or backtracking(maze, y, x - 1, explored) ) return found
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def modified_string_BF(test_str): clst = list(test_str) cmin = [max(clst)] * len(clst) for mx, mch in enumerate(clst): cref = clst.copy() cref.pop(mx) for np in range(len(clst)): ctst = cref.copy() ctst.insert(np, mch) cmin = min(ctst, cmin) return "".join(cmin) def modified_string(test_str): pc = test_str[0] shift = None for cx, ch in enumerate(test_str): if pc > ch: if shift is None or ch <= shift: if shift is None: bsh = pc bx = cx - 1 shift = ch sx = cx pc = ch if shift is not None: for cx, ch in enumerate(test_str): if ch >= shift: shup = test_str[:cx] + shift + test_str[cx:sx] + test_str[sx + 1 :] break shbk = test_str[:bx] for nx, nch in enumerate(test_str[bx + 1 :], bx + 1): if nch <= bsh: shbk += nch else: shbk += bsh + test_str[nx:] break else: shbk += bsh return min(shup, shbk) else: return test_str T = int(input()) for tx in range(T): N = int(input()) S = input().strip() print(modified_string(S))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
for _ in range(int(input())): n = int(input()) mini = ["z"] * 100 s = list(input().strip()) ans = min(mini, s) t = s[:] for i in range(n): curr = s[i] f = 0 ind = 0 temp = s[i] for j in range(i): if s[j] > curr: s.pop(i) s.insert(j, temp) ans = min(ans, s) break s = t[:] for i in range(n): curr = s[i] f = 0 ind = n - 1 temp = s[i] for j in range(i + 1, n): if s[j] > curr: s.pop(i) s.insert(j - 1, temp) ans = min(ans, s) break else: s.pop(i) s.insert(n - 1, temp) ans = min(ans, s) s = t[:] print(*ans, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
t = int(input()) for _ in range(t): n = int(input()) s = input() wow = s for i in range(n): now = s[:i] + s[i + 1 :] for j in range(n): wow = min(wow, now[:j] + s[i] + now[j:]) print(wow)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def modified_string(test_str): clst = list(test_str) cmin = [max(clst)] * len(clst) for mx, mch in enumerate(clst): cref = clst.copy() cref.pop(mx) for np in range(len(clst)): ctst = cref.copy() ctst.insert(np, mch) cmin = min(ctst, cmin) return "".join(cmin) T = int(input()) for tx in range(T): N = int(input()) S = input().strip() print(modified_string(S))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
import sys def modify(s, i, j): c = s[i] t = s[:i] + s[i + 1 :] ret = t[:j] + c + t[j:] return ret t = int(sys.stdin.readline()) while t > 0: t -= 1 n = int(sys.stdin.readline()) s = sys.stdin.readline().strip() m = s for i in range(n): for j in range(n): if i != j: m = min(m, modify(s, i, j)) print(m)
IMPORT FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
t = int(input()) for T in range(t): n = int(input()) s = input() ans = s val, val1 = "", "" for i in range(n): val = s[:i] + s[i + 1 :] for j in range(n): val1 = val[:j] + s[i] + val[j:] ans = min(ans, val1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
T = int(input()) for _ in range(T): n = int(input()) s = input().strip() answer = s for i in range(len(s)): c = s[i] string = s[:i] + s[i + 1 :] for j in range(len(string) + 1): answer = min(answer, string[:j] + c + string[j:]) print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def compare(s1, s2, n): i = 0 while i < n: if s1[i] < s2[i]: return s1 elif s2[i] < s1[i]: return s2 i += 1 return s1 for t in range(int(input())): n = int(input()) a = input() s = [] s1 = [] s2 = [] temp = [] for i in range(n): s.append(a[i]) s1.append(a[i]) s2.append(a[i]) temp.append([a[i], i]) temp.sort() for i in range(n): if s[i] == temp[i][0] or s[i] < temp[i][0]: continue elif s[i] > temp[i][0]: index = temp[i][1] j = n - 1 while j >= 0: if s[j] == temp[i][0] and j > index: index = j j -= 1 del s1[index] s1.insert(i, temp[i][0]) pos = i + 1 while pos < n: if s2[pos] <= s[i]: pos += 1 continue else: del s2[i] s2.insert(pos - 1, s[i]) break if pos >= n: del s2[i] s2.insert(n - 1, s[i]) break x = "".join(s1) y = "".join(s2) ans = compare(x, y, n) print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def modify(s, i, j): k = s[i] del s[i] s.insert(j, k) return str(s) for _ in range(int(input())): n = int(input()) s = list(input()) ans = str(s) for i in range(n): for j in range(n): if i != j: ans = min(ans, modify(s, i, j)) k = s[j] del s[j] s.insert(i, k) for i in ans: if ord(i) >= 65 and ord(i) <= 90: print(i, end="") print()
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def rightShift(s, i, j): s = list(s) while j > i: s[j], s[j - 1] = s[j - 1], s[j] j -= 1 return "".join(s) def leftShift(s, i, j): s = list(s) while j > i: s[i], s[i + 1] = s[i + 1], s[i] i += 1 return "".join(s) def solution(): N = int(input()) S = input() ans = S for i in range(N): for j in range(N): ls = leftShift(S, i, j) rs = rightShift(S, i, j) ans = min(ans, rs, ls) print(ans) T = int(input()) while T > 0: T = T - 1 solution()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def Min_str(): n = int(input()) s = input() temp = s[:] for i in range(n): s1 = [ord(x) for x in s] t = s1[i] del s1[i] j = 0 while j < n - 1: if s1[j] > t: s1[j:j] = [t] break j += 1 if len(s1) != n: s1 += [t] T = "".join([chr(x) for x in s1]) if T < temp: temp = T print(temp) t = int(input()) for i in range(t): Min_str()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
def getInput(): T = int(input()) taille = [] chaine = [] for i in range(T): taille.append(int(input())) chaine.append(input()) return T, taille, chaine def solve(taille, chaine): solution = chaine for i in range(taille): for j in range(taille): tempChaine = list(chaine) lettre = tempChaine[i] tempChaine.pop(i) tempChaine.insert(j, lettre) if "".join(tempChaine) < solution: solution = "".join(tempChaine) return solution T, taille, chaine = getInput() for a in range(T): print(solve(taille[a], chaine[a]))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) stacks = [[] for i in range(n + 1)] queue = [] q_start = 0 unread = 0 ans = [] for i in range(q): action, num = map(int, input().split()) if action == 1: queue.append(0) stacks[num].append(len(queue) - 1) unread += 1 elif action == 2: for i in range(len(stacks[num])): if stacks[num][i] >= q_start and queue[stacks[num][i]] == 0: queue[stacks[num][i]] = 1 unread -= 1 stacks[num] = [] else: for i in range(q_start, num): if queue[i] == 0: queue[i] = 1 unread -= 1 q_start = max(q_start, num) ans.append(unread) print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) ans = 0 res = "" num = 0 pre = 0 noti = [] arr = [([0] * (n + 10)) for i in range(2)] for i in range(q): op, x = map(int, input().split()) if op == 1: arr[0][x] += 1 ans += 1 num += 1 noti += [x] elif op == 2: ans -= arr[0][x] arr[0][x] = 0 arr[1][x] = num else: for j in range(pre, x): if j + 1 > arr[1][noti[j]]: ans -= 1 arr[0][noti[j]] -= 1 pre = max(pre, x) res += str(ans) + "\n" print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST VAR IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
import sys n, m = map(int, input().split()) k = 0 pos = 0 L = [] L1 = [] d = {i: [0, -1] for i in range(1, n + 1)} for i in range(m): a, b = map(int, sys.stdin.readline()[:-1].split()) if a == 1: d[b][0] += 1 k += 1 L.append(b) elif a == 2: k -= d[b][0] d[b][0] = 0 d[b][1] = len(L) else: for j in range(pos, b): if d[L[j]][0] > 0 and d[L[j]][1] < j + 1: k -= 1 d[L[j]][0] -= 1 pos = max(pos, b) L1.append(k) sys.stdout.write("\n".join(map(str, L1)))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, p = map(int, input().split()) L = [] A = [[] for _ in range(n)] op = 0 bec = 0 rt = [] for i in range(p): a, b = map(int, input().split()) if a == 2: while len(A[b - 1]) > 0: y = A[b - 1].pop() bec = bec - L[y] L[y] = 0 elif a == 1: L.append(1) A[b - 1].append(len(L) - 1) bec += 1 else: while b > op: bec = bec - L[op] L[op] = 0 op += 1 rt.append(str(bec)) print("\n".join(rt))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) count = [(0) for i in range(n + 1)] queue = [] read = set() unread = 0 ans = [] last_q_idx = 0 last_app_idx = [(1) for i in range(n + 1)] for i in range(q): action, num = map(int, input().split()) if action == 1: queue.append((num, count[num] + 1)) count[num] += 1 unread += 1 elif action == 2: for number in range(last_app_idx[num], count[num] + 1): if (num, number) not in read: read.add((num, number)) unread -= 1 last_app_idx[num] = max(last_app_idx[num], count[num]) else: for idx in range(last_q_idx, num): app, number = queue[idx] if (app, number) not in read: read.add((app, number)) last_app_idx[app] = max(last_app_idx[app], number) unread -= 1 last_q_idx = max(last_q_idx, num) ans.append(unread) print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
def main(): n, q = map(int, input().split()) vol, tot, l, res = [0] * (n + 1), [0] * (n + 1), [], [] z = m = 0 for _ in range(q): t, x = map(int, input().split()) if t == 1: l.append(x) tot[x] += 1 vol[x] += 1 z += 1 elif t == 2: z -= vol[x] vol[x] = 0 elif m < x: r, m = range(m, x), x for i in r: x = l[i] tot[x] -= 1 if vol[x] > tot[x]: vol[x] -= 1 z -= 1 res.append(z) print("\n".join(map(str, res))) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
[n, q] = list(map(int, input().split(" "))) total_cell = [0] * n max_2 = [0] * n max_3 = 0 message = [] total = 0 time = 0 ans = "" for i in range(q): [t, x_t] = list(map(int, input().split(" "))) if t == 1: total_cell[x_t - 1] += 1 message.append(x_t) total += 1 elif t == 2: total -= total_cell[x_t - 1] total_cell[x_t - 1] = 0 max_2[x_t - 1] = len(message) else: for j in range(max_3, x_t): if j >= max_2[message[j] - 1]: total_cell[message[j] - 1] -= 1 total -= 1 max_3 = max(max_3, x_t) ans += str(total) + "\n" print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) xi = [([0] * (n + 1)) for i in range(2)] noti = [] ans = [] num = 0 num2 = 0 num3 = 0 while q > 0: typ, xt = map(int, input().split()) if typ == 1: xi[0][xt] += 1 noti += [xt] num += 1 num2 += 1 elif typ == 3: for i in range(num3, xt): if i + 1 > xi[1][noti[i]]: xi[0][noti[i]] -= 1 num -= 1 num3 = max(num3, xt) else: num -= xi[0][xt] xi[0][xt] = 0 xi[1][xt] = num2 ans.append(num) q -= 1 print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
import sys def main(): tmp = [int(x) for x in sys.stdin.readline().split()] n = tmp[0] q = tmp[1] readApp = [0] * n unreadApp = [0] * n unreadTot = 0 genApp = [0] * n firstFlag = 0 genOps = [] for i in range(q): tmp = [int(x) for x in sys.stdin.readline().split()] ty, x = tmp[0], tmp[1] if ty == 1: x -= 1 unreadTot += 1 unreadApp[x] += 1 genApp[x] += 1 genOps.append((x, genApp[x])) elif ty == 2: x -= 1 unreadTot -= unreadApp[x] readApp[x] += unreadApp[x] unreadApp[x] = 0 else: for j in range(firstFlag, x): app, index = genOps[j][0], genOps[j][1] if readApp[app] < index: readApp[app] += 1 unreadApp[app] -= 1 unreadTot -= 1 if x > firstFlag: firstFlag = x print(unreadTot) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
import sys input = sys.stdin.readline n, q = map(int, input().split()) l = [[] for _ in range(n)] is_read = [] is_read_idx = 0 ans = 0 prev_t = 0 for _ in range(q): ty, v = map(int, input().split()) if ty == 1: l[v - 1].append(is_read_idx) is_read_idx += 1 is_read.append(False) ans += 1 elif ty == 2: for idx in l[v - 1]: if not is_read[idx]: is_read[idx] = True ans -= 1 l[v - 1] = [] elif v > prev_t: for idx in range(prev_t, v): if not is_read[idx]: is_read[idx] = True ans -= 1 prev_t = v print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) l = [] a = [[] for _ in range(n)] tp = 0 ans = 0 anss = [] for i in range(q): x, b = map(int, input().split()) if x == 1: l.append(1) a[b - 1].append(len(l) - 1) ans += 1 elif x == 2: while len(a[b - 1]) > 0: z = a[b - 1].pop() ans -= l[z] l[z] = 0 else: while tp < b: ans -= l[tp] l[tp] = 0 tp += 1 anss.append(str(ans)) print("\n".join(anss))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = map(int, input().split()) ans = [0] * q unread = [0] * n last = [-1] * n qur, cur = [], 0 for i in range(q): t, x = map(int, input().split()) ans[i] = ans[i - 1] if t == 1: unread[x - 1] += 1 ans[i] += 1 qur.append(x - 1) elif t == 2: ans[i] -= unread[x - 1] unread[x - 1] = 0 last[x - 1] = len(qur) - 1 elif x > cur: for j in range(cur, x): v = qur[j] if last[v] < j: ans[i] -= 1 unread[v] -= 1 last[v] = j cur = x print("\n".join(map(str, ans)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
import itertools n, q = map(int, input().split()) counterGlob = itertools.count() Queue = [] StartQ = 0 counter = 0 ans = "" X = [[] for i in range(n + 1)] F = [0] * (n + 1) for i in range(q): typeQ, a = map(int, input().split()) if typeQ == 1: counter += 1 ind = next(counterGlob) Queue.append(True) X[a].append(ind) elif typeQ == 2: for ind in X[a]: if ind >= StartQ and Queue[ind]: counter -= 1 Queue[ind] = False X[a] = [] else: for ind in range(StartQ, a): if Queue[ind]: counter -= 1 Queue[ind] = False StartQ = max(StartQ, a) ans += str(counter) ans += "\n" print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
from sys import stdin input = stdin.readline n, q = map(int, input().split()) arr = [tuple(map(int, input().split())) for _ in range(q)] adj = [[] for _ in range(n + 1)] curr, cnt, res, vis = 0, 0, [], [] for t, v in arr: if t == 1: adj[v].append(len(vis)) vis.append(0) cnt += 1 elif t == 2: for u in adj[v]: if not vis[u]: vis[u] = 1 cnt -= 1 adj[v] = [] else: while v > curr: if not vis[curr]: vis[curr] = 1 cnt -= 1 curr += 1 res.append(cnt) print("\n".join(map(str, res)))
ASSIGN VAR VAR ASSIGN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER LIST LIST FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: 1. Application x generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application x (he may re-read some notifications). 3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q). Output Print the number of unread notifications after each event. Examples Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
n, q = input().split() n = int(n) q = int(q) p0 = 0 Ans = [] Q = [(0, True)] App = [set() for i in range(n + 1)] t = 1 ans = 0 for i in range(q): q, p = input().split() p = int(p) if q == "1": App[p].add(t) Q.append([p, False]) t += 1 ans += 1 elif q == "2": for i in App[p]: Q[i][1] = True ans -= len(App[p]) App[p] = set() elif q == "3": if p > p0: for i in range(p0, p + 1): if Q[i][1] == False: Q[i][1] = True App[Q[i][0]].remove(i) ans -= 1 p0 = p Ans.append(ans) print("\n".join(map(str, Ans)))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR STRING IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
import sys def doremon(c, i, j): u = v = i z = c[i] w = x = j crt = 0 while ( u >= 0 and v < a and w >= 0 and x < b and c[u][j] == c[v][j] and c[i][w] == c[i][x] ): u = u - 1 v = v + 1 w = w - 1 x = x + 1 crt += 1 return crt n = int(input()) while n != 0: a, b = map(int, input().split()) c = list() crt = 0 for i in range(a): c.append(list(map(int, sys.stdin.readline().split()))) for i in range(a): for j in range(b): t = doremon(c, i, j) crt = crt + t print(crt) n = n - 1
IMPORT FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def func(n, m, matrix): s = n * m for i in range(1, n - 1): for j in range(1, m - 1): if ( matrix[i][j - 1] == matrix[i][j + 1] and matrix[i - 1][j] == matrix[i + 1][j] ): s += 1 k = 2 while ( j + k < m and i + k < n and i - k >= 0 and j - k >= 0 and matrix[i][j - k] == matrix[i][j + k] and matrix[i - k][j] == matrix[i + k][j] ): s += 1 k += 1 print(s) t = int(input()) for i in range(t): n, m = map(int, input().split()) matrix = [] for i in range(n): a = input().split() matrix.append(a) func(n, m, matrix)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
T = int(input()) for t in range(T): N, M = [int(x) for x in input().split()] inp = [] for i in range(N): inp.append([int(x) for x in input().split()]) phor = [([0] * M) for i in range(N)] pvert = [([0] * M) for i in range(N)] for i in range(N): l = 0 r = -1 for j in range(M): if j > r: k = 1 else: k = min(phor[i][l + r - j], r - j + 1) while 0 <= j - k and j + k < M and inp[i][j - k] == inp[i][j + k]: k += 1 phor[i][j] = k k -= 1 if j + k > r: l = j - k r = j + k for j in range(M): l = 0 r = -1 for i in range(N): if i > r: k = 1 else: k = min(pvert[l + r - i][j], r - i + 1) while 0 <= i - k and i + k < N and inp[i - k][j] == inp[i + k][j]: k += 1 pvert[i][j] = k k -= 1 if i + k > r: l = i - k r = i + k summ = 0 for i in range(N): for j in range(M): summ += min(phor[i][j], pvert[i][j]) print(summ)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def maxPall(text): g = [0] * len(text) c, r = 0, 0 l = len(text) for i in range(0, len(text)): mirror = 2 * c - i if mirror >= 0 and i < r: g[i] = min(r - i, g[mirror]) try: while i + g[i] < l and i - g[i] >= 0 and text[i + g[i]] == text[i - g[i]]: g[i] += 1 except IndexError as e: pass if i + g[i] > r: c = i r = i + g[i] return g for _ in range(int(input())): n, m = map(int, input().split()) ar = [] for i in range(n): ar.append(list(map(int, input().split()))) rowPal = [] colPal = [] for i in range(n): rowPal.append(maxPall(ar[i])) arr = list(map(list, zip(*ar))) for i in range(m): colPal.append(maxPall(arr[i])) colPal = list(map(list, zip(*colPal))) g = 0 for i in range(n): for j in range(m): g += min(rowPal[i][j], colPal[i][j]) print(g)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def Shwetank(l, t): count = 0 for i in range(1, n - 1): a = l[i] for j in range(1, m - 1): b = t[j] r1 = j - 1 r2 = j + 1 c1 = i - 1 c2 = i + 1 while r1 >= 0 and r2 <= m - 1 and c1 >= 0 and c2 <= n - 1: if a[r1] == a[r2] and b[c1] == b[c2]: count += 1 r1 -= 1 r2 += 1 c1 -= 1 c2 += 1 else: break return count for _ in range(int(input())): n, m = map(int, input().split()) l = [] count = 0 for i in range(n): l.append(list(map(int, input().split()))) t = [[l[j][i] for j in range(len(l))] for i in range(len(l[0]))] count += Shwetank(l, t) count += n * m print(count)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def countt(mat, i, j): x = 1 count = 0 while i - x >= 0 and i + x < n and j - x >= 0 and j + x < m: if mat[i - x][j] == mat[i + x][j] and mat[i][j - x] == mat[i][j + x]: count += 1 x += 1 else: break return count t = int(input()) for i in range(t): n, m = list(map(int, input().split())) mat = [] sum = 0 for i in range(n): mat.append(list(map(int, input().split()))) for i in range(1, n): for j in range(1, m): sum += countt(mat, i, j) print(n * m + sum)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
test = False def calc(grid, r, c): maxRange = min(r + 1, c + 1, M - c, N - r) k = 1 temp = 0 while k < maxRange: if grid[r - k][c] == grid[r + k][c] and grid[r][c - k] == grid[r][c + k]: temp += 1 else: return temp k += 1 return temp T = int(input().strip()) for i in range(T): N, M = map(int, input().strip().split(" ")) grid = [] for j in range(N): grid.append([int(x) for x in input().strip().split(" ")]) if test: print(N, M, grid) answer = 0 for r in range(1, N - 1): for c in range(1, M - 1): answer += calc(grid, r, c) answer += M * N print(answer)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def largest_palind(matrix, i, j, n, m): limit = min(min(i, abs(n - 1 - i)), min(j, abs(m - 1 - j))) max_i = 0 max_j = 0 for k in range(1, limit + 1): if matrix[i + k][j] == matrix[i - k][j]: max_i = k else: break if matrix[i][j + k] == matrix[i][j - k]: max_j = k else: break return min(max_i, max_j) for _ in range(int(input())): n, m = [int(x) for x in input().split()] matrix = [] sum = n * m for i in range(n): row = [int(x) for x in input().split()] matrix.append(row) for i in range(1, n - 1): for j in range(1, m - 1): val = largest_palind(matrix, i, j, n, m) sum += val print(sum)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR 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 VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def accumulator(A, ci, cj, limit): count = 0 for k in range(1, limit + 1): if A[ci + k][cj] == A[ci - k][cj] and A[ci][cj + k] == A[ci][cj - k]: count += 1 else: return count return count tests = int(input()) for l in range(tests): N, M = input().split(" ") N, M = int(N), int(M) A = [] for ll in range(N): A.append([int(i) for i in input().split(" ")]) count = N * M for i in range(1, N - 1): for j in range(1, M - 1): count += accumulator(A, i, j, min(i, j, N - i - 1, M - j - 1)) print(count)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
from itertools import count def read_mat(rows, cols): mat = [input().split() for _ in range(rows)] return mat def noof_pairs(mat, rows, cols): noof_elems = rows * cols cnt = 0 for i in range(1, rows - 1): for j in range(1, cols - 1): for k in count(1): if i - k >= 0 and j - k >= 0 and i + k < rows and j + k < cols: pass else: break if mat[i][j - k] == mat[i][j + k] and mat[i - k][j] == mat[i + k][j]: cnt += 1 else: break return cnt + noof_elems def main(): for i in range(int(input())): rows, cols = map(int, input().split()) mat = read_mat(rows, cols) cnt = noof_pairs(mat, rows, cols) print(cnt) main()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR 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 FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
II = lambda: [int(x) for x in input().split()] I2 = lambda: int(input()) T = int(input()) for _ in range(T): a = II() N, M = a A = [II() for _ in range(N)] H = [([1] * M) for _ in range(N)] for i in range(N): c, r = 0, 0 for j in range(1, M - 1): m_pos = c - (j - c) m_val = H[i][m_pos] if m_pos >= 0 else 1 if j + m_val - 1 < r: H[i][j] = m_val continue m_val = max(min(r - j + 1, m_val), 1) H[i][j] = m_val for d in range(m_val, M): if 0 <= j - d <= j + d < M and A[i][j - d] == A[i][j + d]: H[i][j] += 1 else: c, r = j, j + d - 1 break V = [([1] * M) for _ in range(N)] for j in range(M): c, r = 0, 0 for i in range(1, N - 1): m_pos = c - (i - c) m_val = V[m_pos][j] if m_pos >= 0 else 1 if i + m_val - 1 < r: V[i][j] = m_val continue m_val = max(min(r - i + 1, m_val), 1) V[i][j] = m_val for d in range(m_val, M): if 0 <= i - d <= i + d < N and A[i - d][j] == A[i + d][j]: V[i][j] += 1 else: c, r = i, i + d - 1 break count = 0 for i in range(N): for j in range(M): count += min(H[i][j], V[i][j]) print(count)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def dora(a, n, m): ans = 0 for i in range(n): for j in range(m): d = min(i, n - 1 - i, j, m - 1 - j) p = 0 for k in range(d + 1): if a[i - k][j] != a[i + k][j]: break else: p = p + 1 g = 0 for k in range(d + 1): if a[i][j - k] != a[i][j + k]: break else: g = g + 1 ans = ans + min(p, g) print(ans) for _ in range(int(input())): n, m = input().split() n = int(n) m = int(m) a = [] for i in range(n): b = list(map(int, input().split())) a.append(b) dora(a, n, m)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def func(li, i, j, n, m): a = 1 count = 0 while (i - a >= 0 and i + a < n) and (j - a >= 0 and j + a < m): if li[i][j - a] == li[i][j + a] and li[i - a][j] == li[i + a][j]: count += 1 a += 1 else: break return count for k in range(int(input(""))): li = [] count = 0 n, m = map(int, input("").split(" ")) for j in range(n): li.append(list(map(int, input("").split(" ")))) for i in range(1, n): for j in range(1, m): count += func(li, i, j, n, m) ans = n * m + count print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def solve(l, n, m): cnt = n * m for r in range(1, n - 1): for c in range(1, m - 1): _min = min(min(c, m - c - 1), min(r, n - r - 1)) for k in range(1, _min + 1): if l[r][c + k] == l[r][c - k] and l[r - k][c] == l[r + k][c]: cnt += 1 else: break return cnt t = int(input()) for _ in range(t): ans = 0 n, m = map(int, input().split()) l = [] for rows in range(n): x = list(map(int, input().split())) l.append(x) ans += solve(l, n, m) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR 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 FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def func(arr, n, m): ans = n * m for i in range(1, n - 1): for j in range(1, m - 1): k = 1 while i - k >= 0 and i + k < n and j - k >= 0 and j + k < m: if arr[i - k][j] == arr[i + k][j] and arr[i][j - k] == arr[i][j + k]: ans += 1 else: break k += 1 return ans t = int(input()) for _ in range(t): n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(list(map(int, input().split()))) result = func(arr, n, m) print(result)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def check(a, b, m, A, B): x = min(a, b, A - a, B - b) s = 0 f1 = 0 for j in range(1, x + 1): if m[a - j][b] == m[a + j][b] and m[a][b - j] == m[a][b + j]: s = s + 1 else: f1 = 1 break return s t = int(input()) for i in range(0, t): N, M = map(int, input().split()) arr = [] for j in range(0, N): m = list(map(int, input().split())) arr.append(m) ans = N * M for p in range(1, N - 1): for q in range(1, M - 1): ans = ans + check(p, q, arr, N - 1, M - 1) print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def check(x, i, j, y): r = 0 count = 0 while r <= y: if x[i][j - r] == x[i][j + r] and x[i - r][j] == x[i + r][j]: count += 1 else: return count r += 1 return count t = int(input()) while t > 0: n, m = [int(x) for x in input().split()] matrix = [] for i in range(n): a = [int(y) for y in input().split()] matrix.append(a) m = len(a) final = 0 for i in range(n): for j in range(m): temp1 = 1000000 temp2 = 1000000 if i >= n / 2: temp1 = n - 1 - i if j >= m / 2: temp2 = m - j - 1 temp = min(i, j, temp1, temp2) final = final + check(matrix, i, j, temp) print(final) t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def palindrome(mat, i, j, c): h = 0 for k in range(1, c + 1): if mat[i][j - k] == mat[i][j + k]: if mat[i - k][j] == mat[i + k][j]: h = h + 1 else: break else: break return h t = int(input()) for i in range(t): n, m = map(int, input().split()) mat = [] for i in range(n): a = list(map(int, input().split())) mat.append(a) k = 0 for i in range(n): for j in range(m): if i > 0 and j > 0: if n - 1 - i > 0 and m - 1 - j > 0: a = min(i, n - 1 - i) b = min(j, m - 1 - j) c = min(a, b) k = k + palindrome(mat, i, j, c) print(k + n * m)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
from sys import stdin, stdout def main(): from sys import stdin, stdout t = int(input()) for i in range(0, t): n, m = map(int, input().split()) a = [] for i in range(n): b = list(map(int, input().split())) a.append(b) c = 0 for i in range(0, n): for j in range(0, m): k = 1 while ( i - k >= 0 and j - k >= 0 and i + k < n and j + k < m and a[i - k][j] == a[i + k][j] and a[i][j - k] == a[i][j + k] ): k = k + 1 c = c + k print(c) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
def solve(mat, row, col, count): for i in range(row): for j in range(col): maxRange = min(i, row - i - 1, j, col - j - 1) for k in range(1, maxRange + 1): if mat[i][j - k] == mat[i][j + k] and mat[i - k][j] == mat[i + k][j]: count += 1 else: break return count for _ in range(int(input())): r, c = map(int, input().split()) arr = [] for i in range(r): a = list(map(str, input().split())) arr.append(a) print(solve(arr, r, c, r * c))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Doraemon has a matrix with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). Let's denote the element in row $i$ and column $j$ by $A_{i,j}$. Next, let's define a sub-row of a row $r$ as a sequence $A_{r, x}, A_{r, x+1}, \ldots, A_{r, y}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ M$) and a sub-column of a column $c$ by $A_{x, c}, A_{x+1, c}, \ldots, A_{y, c}$ for some $x$ and $y$ ($1 ≤ x ≤ y ≤ N$). You need to find the number of pairs (sub-row of some row, sub-column of some column) with the following properties: 1. Both sequences (the sub-row and the sub-column) have the same length. 2. This length is odd. 3. The central elements of both sequences are the same (they correspond to the same element of the matrix). 4. Both sequences are palindromes. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains $M$ space-separated integers $A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid pairs. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, M$ $N \cdot M ≤ 10^{5}$ $1 ≤ A_{i, j} ≤ 10^{6}$ for each valid $i, j$ the sum of $N \cdot M$ over all test cases does not exceed $3 \cdot 10^{5}$ ------ Subtasks ------ Subtask #1 (30 points): $T ≤ 10$ $N \cdot M ≤ 10^{3}$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 1 3 3 2 1 2 1 1 1 2 1 2 ----- Sample Output 1 ------ 10 ----- explanation 1 ------ Example case 1: Each element forms a valid pair (where the sub-row and sub-column contain only this element) and the middle row and middle column also form one valid pair.
from sys import stdin, stdout def main(): from sys import stdin, stdout for _ in range(int(input())): n, m = map(int, input().split()) ctr = 0 l2 = [] for i in range(n): a = list(map(int, input().split())) l2.append(a) for i in range(0, n): for j in range(0, m): l3 = 1 while ( l3 <= i and l3 <= j and l3 + i < n and l3 + j < m and l2[i - l3][j] == l2[i + l3][j] and l2[i][j - l3] == l2[i][j + l3] ): l3 += 1 ctr += l3 print(ctr) 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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR