description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) temp = l[0] prev = temp flag = False for i in range(1, n): temp = max(temp + 1 - k, l[i]) prev = min(prev - 1 + k, l[i] + k - 1) if temp > prev: print("NO") flag = True break if flag: continue if l[n - 1] >= temp and prev >= l[n - 1]: print("YES") else: print("NO")
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 VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = list(map(int, input().split())) h = list(map(int, input().split())) a = [(0) for i in range(n)] poss = True for i in range(1, n - 1): lb = h[i - 1] + a[i - 1] - h[i] - k + 1 ub = h[i - 1] + a[i - 1] - h[i] + k - 1 if ub < 0 or lb >= k: poss = False break elif h[i + 1] > h[i]: a[i] = min(ub, k - 1) else: a[i] = max(lb, 0) if poss == True: lb = h[n - 2] + a[n - 2] - h[n - 1] - k + 1 ub = h[n - 2] + a[n - 2] - h[n - 1] + k - 1 if lb > 0 or ub < 0: poss = False if poss == True: print("YES") else: print("NO")
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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = list(map(int, input().split())) h = list(map(int, input().split())) min1 = h[0] max1 = h[0] + k yes = 0 for i in range(1, n - 1): if h[i] + 2 * k - 1 <= min1 or h[i] >= max1: yes = 1 break else: if h[i] >= min1: min1 = h[i] else: min1 = max(min1 - k + 1, h[i]) if h[i] + k * 2 - 1 <= max1: max1 = h[i] + k * 2 - 1 else: max1 = min(max1 + k - 1, h[i] + k * 2 - 1) if min1 >= k + h[-1] or max1 <= h[-1] or yes == 1: print("NO") else: print("YES")
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 VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin t = int(stdin.readline()) def run(): L = [int(x) for x in stdin.readline().split(" ")] n, k = L[0], L[1] A = [int(x) for x in stdin.readline().split(" ")] lo = A[0] hi = A[0] for i in range(1, n): lo = max(lo - (k - 1), A[i]) hi = min(A[i] + (k - 1), hi + (k - 1)) if lo > hi: print("NO") return if lo <= A[-1] <= hi: print("YES") else: print("NO") for _ in range(t): run()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def readLine(): return [int(s) for s in input().split(" ")] def solve(): xd = readLine() n = xd[0] k = xd[1] h = readLine() lo = h[0] + 1 hi = h[0] + 1 c = 0 for cur in h: c = c + 1 if c == 1: continue lo = lo - (k - 1) hi = hi + (k - 1) hi = min(hi, cur + 1 + k - 1) lo = max(lo, cur + 1) if lo > hi: print("No") return if lo <= h[-1] + 1 and h[-1] + 1 <= hi: print("Yes") else: print("No") tt = int(input()) for x in range(tt): solve()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def getCases(): cases = [] t = int(input()) for case in range(t): n, k = list(map(int, input().strip().split())) h = list(map(int, input().strip().split())) cases.append([n, k, h]) return cases def check(case): n, k, heights = case _min = heights[0] _max = heights[0] foo = True for i in range(1, n): _min = max(_min - k + 1, heights[i]) _max = min(_max + k - 1, heights[i] + k - 1) if _min > _max: foo = False break if heights[n - 1] < _min or heights[n - 1] > _max: foo = False return "YES" if foo else "NO" for case in getCases(): print(check(case))
FUNC_DEF ASSIGN VAR LIST 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR STRING STRING FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) h = list(map(int, input().split())) minL = h[0] maxL = h[0] done = True for i in range(1, n): minL = max(minL - k + 1, h[i]) maxL = min(maxL + k - 1, h[i] + k - 1) if minL > maxL: done = False break if h[n - 1] > maxL or h[n - 1] < minL: done = False if done == True: print("YES") else: print("NO")
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n, k = map(int, input().split()) hh = [int(a) for a in input().split()] mx = hh[0] for h in hh[1:]: if h > mx + k - 1: print("No") return else: mx = min(mx + k - 1, h + k - 1) mx = hh[-1] for h in hh[-2::-1]: if h > mx + k - 1: print("No") return else: mx = min(mx + k - 1, h + k - 1) print("Yes") def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF 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 VAR NUMBER FOR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = input().split() n, k = int(n), int(k) h = list(map(int, input().split())) poss = True lo = h[0] hi = h[0] for i in range(1, n): lo = lo - k + 1 hi = hi + k - 1 cl = h[i] ch = h[i] + k - 1 if i == n - 1: ch = cl if hi < cl or ch < lo: poss = False break lo = max(lo, cl) hi = min(hi, ch) if poss: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys def input(): return sys.stdin.readline().strip() def solve(): for t in range(int(input())): n, k = map(int, input().split()) k -= 1 ii = iter(map(int, input().split())) m = next(ii) M = m bad = False for h in ii: m = max(m - k, h) M = min(M + k, h + k) if m > M: bad = True break if not bad and m <= h and M >= h: print("YES") else: print("NO") solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
cases = int(input()) ans_list = [] for c in range(cases): n, k = list(map(int, input().split(" "))) heights = list(map(int, input().split(" "))) max_top = heights[0] + k min_top = heights[0] + k ans = "YES" for i in range(1, n - 1): if heights[i] >= max_top or heights[i] + k + k - 1 <= min_top - k: ans = "NO" break max_top = min(heights[i] + k - 1, max_top - 1) + k min_top = max(heights[i], min_top - 2 * k + 1) + k if ans == "YES": if not (min_top - k < heights[-1] + k and heights[-1] < max_top): ans = "NO" ans_list.append(ans) print("\n".join(ans_list))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR IF VAR STRING IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for t in range(int(input())): n, k = map(int, input().split()) mas = list(map(int, input().split())) mn = mas[0] mx = mas[0] + k - 1 f = True for i in range(1, n): if mx - mn < k - 1: print("No") f = False break if i == n - 1 and mn - k + 1 > mas[i]: print("No") f = False break if i == n - 1 and mx < mas[i]: print("No") f = False break if mn - k + 1 >= mas[i] + k: print("No") f = False break if mx < mas[i]: print("No") f = False break mx = min(mx + k - 1, mas[i] + 2 * k - 2) mn = max(mas[i], mn - k + 1) if f: print("Yes")
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 VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def share(l1, u1, l2, u2): if l2 < u1 and l2 >= l1 or u2 > l1 and u2 <= u1: return True return False t = int(input()) for z in range(t): n, k = [int(i) for i in input().split()] h = [int(i) for i in input().split()] u, l = h[0] + k, h[0] c = 0 for i in range(1, n - 1): if share(l, u, h[i], h[i] + k) or share(l, u, h[i] + k - 1, h[i] + k + k - 1): lmin = max(h[i], l + 1 - k) umax = min(h[i] + k + k - 1, u - 1 + k) u, l = umax, lmin else: c = 1 if c == 0 and share(l, u, h[n - 1], h[n - 1] + k) == False: c = 1 if c == 0: print("YES") else: print("NO")
FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) ma = a[0] mi = a[0] f = True for i in range(1, n): if a[i] > a[i - 1]: if not k - 1 >= abs(ma - a[i]): f = False elif not 2 * (k - 1) >= abs(mi - a[i]): f = False ma = min(a[i] + k - 1, ma + k - 1) mi = max(a[i], mi - k + 1) if f and mi == a[n - 1]: print("YES") else: print("NO")
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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
innum = lambda: int(input()) inmul = lambda: map(int, input().split()) instr = lambda: str(input()) inarr = lambda: list(map(int, input().split())) def solve(): n, k = inmul() a = inarr() ma = a[0] mi = a[0] for i in range(1, n - 1): mi = max(mi - (k - 1), a[i]) ma = min(ma + k - 1, a[i] + k - 1) if mi > ma: print("NO") return if a[-1] >= mi - k + 1 and a[-1] <= ma + k - 1: print("YES") else: print("NO") def main(): t = 1 t = int(input()) for _ in range(t): solve() main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) rs = lambda: input().rstrip() def solve_case(): n, k = rl() h = rl() lo = hi = 0 ok = True for i, v in enumerate(h): if i == 0: lo, hi = v, v else: lo, hi = max(lo - (k - 1), v), min(hi + (k - 1), v + (k - 1)) if lo > hi: ok = False if ok and lo == h[-1]: print("YES") else: print("NO") T = ri() for _ in range(T): solve_case()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) a = [*map(int, input().split())] b = [(a[0], a[0])] f = 0 for i in range(1, n): x = max(b[i - 1][0] - (k - 1), a[i]) y = min(b[i - 1][1] + (k - 1), a[i] + (k - 1)) b.append((x, y)) if x > y: f = 1 break if a[n - 1] not in range(x, y + 1): f = 1 if f: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for i in range(t): n, k = map(int, input().split()) hi = list(map(int, input().split())) minh = hi[0] - k + 1 maxh = hi[0] + k - 1 ans = 1 for i in range(1, n - 1): curminh = hi[i] curmaxh = hi[i] + k - 1 minh = max(curminh, minh) maxh = min(curmaxh, maxh) if minh > maxh: ans = 0 break minh = minh - k + 1 maxh = maxh + k - 1 curminh = hi[-1] curmaxh = hi[-1] minh = max(curminh, minh) maxh = min(curmaxh, maxh) if minh > maxh: ans = 0 if ans: print("YES") else: print("NO")
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 BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def inInt(): return int(input()) def inStr(): return input().strip("\n") def inIList(): return list(map(int, input().split())) def inSList(): return input().split() def bsearch(nums, target): N = len(nums or []) l = 0 r = N - 1 while l <= r: mid = (l + r) // 2 if nums[mid] < target: l = mid + 1 elif nums[mid] > target: r = mid - 1 else: return None, mid, None return r if r >= 0 else None, None, l if l <= N - 1 else None def yesOrNo(val): print("YES" if val else "NO") def printSpacedArray(nums): print(*nums) def solve(): n, k = inIList() h = inIList() top = bottom = 0 for i in range(n): if i == 0: top = bottom = h[i] else: bottom = max(bottom - k + 1, h[i]) top = min(top + k - 1, h[i] + k - 1) if top < bottom: return "NO" if bottom != h[-1]: return "NO" return "YES" tests = inInt() for case in range(tests): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NONE VAR NONE RETURN VAR NUMBER VAR NONE NONE VAR BIN_OP VAR NUMBER VAR NONE FUNC_DEF EXPR FUNC_CALL VAR VAR STRING STRING FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import * input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) ini = [0 + l[0], k + l[0]] ans = "YES" for i in range(1, n): pMin = ini[0] + 1 - k pMax = ini[1] - 1 + k if i < n - 1 and pMin - (k - 1) <= l[i] < ini[1]: ini = [max(pMin, l[i]), min(pMax, l[i] + k + k - 1)] elif i == n - 1 and pMin <= l[i] < ini[1]: pass else: ans = "NO" break print(ans)
ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def calc(): [n, k] = list(map(int, input().split(" "))) h = list(map(int, input().split(" "))) l = [0] * n u = [0] * n for i in range(1, n): l[i] = max([h[i - 1] + l[i - 1] - k + 1, h[i]]) - h[i] if l[i] >= k: print("NO") return u[i] = min([h[i - 1] + u[i - 1] + k - 1, h[i] + k - 1]) - h[i] if u[i] < 0 or u[i] < l[i]: print("NO") return if l[n - 1] == 0: print("YES") else: print("NO") t = int(input()) for i in range(t): calc()
FUNC_DEF ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN 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 FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for t in range(int(input())): n, k = [int(x) for x in input().split()] h = [int(x) for x in input().split()] flag = True pu = h[0] pd = h[0] for i in range(1, n): if pu + k <= h[i] or pd + 1 - k > k - 1 + h[i]: flag = False break else: pu = min(pu + k - 1, h[i] + k - 1) pd = max(pd + 1 - k, h[i]) if flag and h[n - 1] <= pu and h[n - 1] >= pd: print("YES") else: print("NO")
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 NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) ar = list(map(int, input().split())) curmx, curmn = ar[0], ar[0] for i in range(1, n): if curmx + (k - 1) < ar[i]: print("NO") break if curmn - (k - 1) > ar[i] + (k - 1): print("NO") break if i == n - 1 and curmn - (k - 1) > ar[i]: print("NO") break curmx = min(curmx + k - 1, ar[i] + k - 1) curmn = max(ar[i], curmn - (k - 1)) else: print("YES")
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 VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
n = int(input()) for i in range(n): n, k = map(int, input().split()) nums = list(map(int, input().split())) bot = nums[0] top = nums[0] + k control = "YES" top2 = 0 for j in range(1, n): if j == n - 1: top2 = nums[j] + k else: top2 = nums[j] + 2 * k - 1 if top2 <= bot or nums[j] >= top: control = "NO" break bot = max(nums[j], bot - k + 1) top = min(top + k - 1, top2) print(control)
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 VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys input = sys.stdin.readline def solution(): n, k = map(int, input().split()) h = list(map(int, input().split())) if n == 2: if abs(h[0] - h[-1]) >= k: print("NO") return sweep = [] for i in range(1, n - 1): sweep.append((h[i], i)) sweep.sort(reverse=True) H = [-1] * n H[0] = h[0] H[-1] = h[-1] for h_i, i in sweep: cur = h_i if H[i - 1] != -1: cur = max(cur, H[i - 1] + 1 - k) if H[i + 1] != -1: cur = max(cur, H[i + 1] + 1 - k) if cur - h_i >= k: print("NO") return H[i] = cur for i in range(n - 1): if abs(H[i] - H[i + 1]) >= k: print("NO") return print("YES") T = int(input()) for _ in range(T): solution()
IMPORT ASSIGN VAR VAR 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 IF VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
from sys import stdin stdin.readline def mp(): return list(map(int, stdin.readline().strip().split())) def it(): return int(stdin.readline().strip()) def fun(): n, k = mp() l = mp() low, high = l[0], l[0] for i in l[1:]: low, high = max(low - k + 1, i), min(high + k - 1, i + k - 1) if low > high: return False return low <= l[-1] <= high for _ in range(it()): if fun(): print("YES") else: print("NO")
EXPR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for testcase in range(int(input())): n, k = map(int, input().split(" ")) a = list(map(int, input().split(" "))) x = [a[0], a[0]] cond = True for i in a[1:-1]: x = [max(i, x[0] - k + 1), min(i + k - 1, x[1] + k - 1)] cond = cond and x[0] <= x[1] if a[-1] + k - 1 < x[0] or x[1] + k - 1 < a[-1]: cond = False print("YES" * cond + "NO" * (not cond))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in " " * int(input()): n, k = map(int, input().split()) a = list(map(int, input().split())) p, x, f = a[0], a[0], 0 for i in range(1, n): l, g = max(a[i], p - k + 1), min(a[i] + k - 1, x + k - 1) p, x = l, g if l > g: f = 1 break print("YNEOS"[f or not p <= a[-1] <= x :: 2])
FOR VAR BIN_OP STRING 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 VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR VAR NUMBER VAR NUMBER
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) H = [int(h) for h in input().split()] a, b = H[0], H[0] + k ans = "YES" for h in H[1:-1]: x, y = h, h + 2 * k - 1 a = max(x, a - k + 1) b = min(y, b + k - 1) if b - a < k: ans = "NO" if H[-1] >= b or H[-1] + k <= a: ans = "NO" print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for testcases in range(t): n, k = list(map(int, input().split())) a = [int(x) for x in input().split()] s = -1 r = -1 ok = 1 for x in a: if s == -1: s = x r = x else: s = max(s - k + 1, x) r = min(r + k - 1, x + k - 1) if s > r: ok = 0 break if ok == 1 and s <= x: print("YES") else: print("NO")
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def fence(): n, k = map(int, input().split()) H = list(map(int, input().split())) l, r = 0, 0 for i in range(len(H)): if i == 0: l = r = H[0] else: l = max(l - k + 1, H[i]) r = min(r + k - 1, H[i] + k - 1) if l > r or i == n - 1 and l != H[i]: return False for _ in range(int(input())): if fence() == None: print("Yes") else: print("No")
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 VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_int(): return list(map(int, sys.stdin.readline().strip().split()))[0] def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() N = 0 a = [] def solve(): global N N, k = get_ints() a = get_list() mx = a[0] mn = a[0] possible = True for i in a[1:]: mn = max(mn - (k - 1), i) mx = min(mx + (k - 1), i + (k - 1)) if mx < mn: possible = False break if a[-1:][0] not in range(mn, mx + 1): possible = False print("YES" if possible else "NO") test_cases = get_int() for _ in range(test_cases): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
for _ in range(int(input())): n, k = map(int, input().split()) h = list(map(int, input().split())) end = False cur = h[0] for i in range(n): if cur < h[i]: end = True break if i != n - 1: cur = min(cur + k - 1, h[i + 1] + k - 1) cur = h[-1] for i in range(n - 1, -1, -1): if cur < h[i]: end = True break if i: cur = min(cur + k - 1, h[i - 1] + k - 1) if end: print("NO") else: print("YES")
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 NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def get_overlap(A, B): p, q = A r, s = B haha = [r - K + 1, s - 1] nxt_a = max(p, haha[0]) nxt_b = min(q, haha[1]) if nxt_b >= nxt_a: return [nxt_a, nxt_b] else: return False for _ in range(int(input())): N, K = map(int, input().split()) heights = list(map(int, input().split())) base_range = [] base_range.append([heights[0], heights[0]]) possible = True for i in range(1, N - 1): possible_base_range = [heights[i], heights[i] + K - 1] possible_last_range = [base_range[i - 1][0], base_range[i - 1][1] + K] nxt = get_overlap(possible_base_range, possible_last_range) if nxt == False: possible = False break else: base_range.append(nxt) if possible: possible_base_range = [heights[N - 1], heights[N - 1]] possible_last_range = [base_range[N - 2][0], base_range[N - 2][1] + K] nxt = get_overlap(possible_base_range, possible_last_range) if nxt == False: possible = False if possible: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN LIST VAR VAR RETURN NUMBER 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 LIST EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
T = int(input()) ans = [] for t in range(T): flag = True n, k = map(int, input().split()) h = list(map(int, input().split())) tmp = [0] * n tmp[0] = h[0] for i in range(1, n - 1): ma = min(tmp[i - 1] + k - 1, h[i] + k - 1) mi = max(tmp[i - 1] - k + 1, h[i]) if mi > ma: flag = False if h[i + 1] > h[i]: tmp[i] = ma else: tmp[i] = mi if abs(h[n - 1] - tmp[n - 2]) >= k: flag = False ans.append("YES" if flag else "NO") for i in range(T): print(ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
def check(n, k, h): a = [h[0]] b = [h[0]] for i in range(1, n - 1): if h[i] >= b[i - 1] + k or h[i] + k <= a[i - 1] + 1 - k: return "NO" else: a.append(max(a[i - 1] + 1 - k, h[i])) b.append(min(b[i - 1] - 1 + k, h[i] + k - 1)) if b[n - 2] + k <= h[n - 1] or a[n - 2] >= h[n - 1] + k: return "NO" else: return "YES" t = int(input()) for _ in range(t): n, k = map(int, input().split()) h = list(map(int, input().split())) print(check(n, k, h))
FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = [int(j) for j in input().split()] current_range = [arr[0] + k, arr[0] + k] flag = 0 for i in range(1, n - 1): if arr[i] >= current_range[1] or arr[i] + k - 1 + k <= current_range[0] - k: flag = 1 break if arr[i] + k <= current_range[0] - k: current_range[0] = current_range[0] - k + 1 else: current_range[0] = arr[i] + k current_range[1] = current_range[1] - 1 + k current_range[1] = min(current_range[1], arr[i] + k - 1 + k) if arr[-1] + k <= current_range[0] - k or arr[-1] >= current_range[1]: flag = 1 if flag == 1: print("NO") else: print("YES")
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You want to build a fence that will consist of $n$ equal sections. All sections have a width equal to $1$ and height equal to $k$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $i$-th section is equal to $h_i$. You should follow several rules to build the fence: the consecutive sections should have a common side of length at least $1$; the first and the last sections should stand on the corresponding ground levels; the sections between may be either on the ground level or higher, but not higher than $k - 1$ from the ground level $h_i$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le 10^8$) β€” the number of sections in the fence and the height of each section. The second line of each test case contains $n$ integers $h_1, h_2, \dots, h_n$ ($0 \le h_i \le 10^8$), where $h_i$ is the ground level beneath the $i$-th section. It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). -----Examples----- Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Output YES YES NO -----Note----- In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $k = 3$, $h_1 = 0$, and $h_2 = 2$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $3$ and the third section on height $2$. According to the first rule, the second section should be on the height of at least $2$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $h_2 + k - 1 = 1$.
t = int(input()) for i in range(t): m = 0 str = input() _, k = [int(item) for item in str.split(" ")] str = input() ground = [int(item) for item in str.split(" ")] _min, _max = ground[0], ground[0] for i in range(1, len(ground)): _min -= k - 1 _max += k - 1 if _min < ground[i]: _min = ground[i] if _max > ground[i] + k - 1: _max = ground[i] + k - 1 if _min > _max: print("NO") m = 1 break if m == 0: if _min == ground[-1]: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: n = len(text) @lru_cache(None) def dfs(i, j): if i > j: return 0 if i == j: return 1 res = 1 k = 1 while i + k <= j - k + 1: if text[i : i + k] == text[j - k + 1 : j + 1]: res = max(res, 2 + dfs(i + k, j - k)) k += 1 return res return max(dfs(0, n - 1), 1)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: @lru_cache(None) def helper(i, j): if i > j: return 0 if i == j: return 1 ans = 1 tmp = 0 l = j - i + 1 for k in range(1, l // 2 + 1): if text[i : i + k] == text[j - k + 1 : j + 1]: tmp = max(tmp, 2 + helper(i + k, j - k)) return max(tmp, 1) return helper(0, len(text) - 1)
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: found = {} def search(start, end): if start > end: return 0 if (start, end) in found: return found[start, end] m = 1 for i in range(1, (end - start + 1) // 2 + 1): if text[start : start + i] == text[end + 1 - i : end + 1]: m = max(m, 2 + search(start + i, end - i)) found[start, end] = m return m return search(0, len(text) - 1)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: idxMap = defaultdict(list) for i, ch in enumerate(text): idxMap[ch].append(i) memo = {} def recurse(i, j): if i == j: return 1 if i > j: return 0 if (i, j) in memo: return memo[i, j] curMax = 1 for k in idxMap[text[i]]: if k > j or i == k: continue if text[i : i + j - k + 1] == text[k : j + 1]: curMax = max(2 + recurse(i + j - k + 1, k - 1), curMax) memo[i, j] = curMax return curMax return recurse(0, len(text) - 1)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: count = 0 i = 0 while text: if text[: i + 1] == text[-(i + 1) :]: count += 1 if i + 1 < len(text): count += 1 text = text[i + 1 : -(i + 1)] i = 0 continue i += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: l, r, lh, rh, curr_len, modulo, base, res = ( 0, len(text) - 1, 0, 0, 0, 32416189573, 26, 0, ) nums = [(ord(c) - ord("a")) for c in text] while l < r: lh = (lh * base + nums[l]) % modulo rh = (nums[r] * base**curr_len + rh) % modulo curr_len += 1 if lh == rh and text[l - curr_len + 1 : l + 1] == text[r : r + curr_len]: res += 2 lh = rh = curr_len = 0 l += 1 r -= 1 if l == r or curr_len: res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: n = len(text) splits = 0 leftstart, leftend = 0, 0 rightstart, rightend = n - 1, n - 1 while leftend < rightstart: if text[leftstart : leftend + 1] == text[rightstart : rightend + 1]: leftstart = leftend + 1 leftend = leftstart rightstart = rightstart - 1 rightend = rightstart splits += 2 else: leftend += 1 rightstart -= 1 return splits + 1 if leftstart <= rightend else splits
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR
Return the largest possible kΒ such that there existsΒ a_1, a_2, ..., a_kΒ such that: Each a_i is a non-empty string; Their concatenation a_1 + a_2 + ... + a_k is equal to text; For all 1 <= i <= k,Β Β a_i = a_{k+1 - i}. Β  Example 1: Input: text = "ghiabcdefhelloadamhelloabcdefghi" Output: 7 Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)". Example 2: Input: text = "merchant" Output: 1 Explanation: We can split the string on "(merchant)". Example 3: Input: text = "antaprezatepzapreanta" Output: 11 Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)". Example 4: Input: text = "aaa" Output: 3 Explanation: We can split the string on "(a)(a)(a)". Β  Constraints: text consists only of lowercase English characters. 1 <= text.length <= 1000
class Solution: def longestDecomposition(self, text: str) -> int: if not text: return 0 i, j, result = 0, len(text) - 1, 0 while i < j: if text[: i + 1] == text[j:]: return self.longestDecomposition(text[i + 1 : j]) + 2 else: i, j = i + 1, j - 1 return 1
CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) stored = [(-1) for i in range(n + 2)] counted = [[0, 0] for i in range(n + 2)] count = 0 for i in range(n): stored[a[i]] = a[i] if counted[a[i]][0] == 0: count += 1 counted[a[i]][0] += 1 counted[a[i]][1] += 1 pt = n + 1 p_pt = 0 for i in range(n + 1, -1, -1): if stored[i] >= 0: p_pt = pt pt = stored[i] stored[i] = p_pt else: stored[i] = pt ans = [(0) for i in range(n + 2)] for i in range(n): counted[a[i]][1] -= 1 if ( counted[stored[a[i]]][0] - counted[stored[a[i]]][1] == 0 and counted[a[i]][1] == 0 ): ans[stored[a[i]]] = ans[a[i]] + 1 maxi = max(max(ans[: n + 1]) + 1, ans[-1]) print(count - maxi)
IMPORT ASSIGN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
import sys as _sys def main(): q = int(input()) for i_q in range(q): (n,) = _read_ints() a = tuple(_read_ints()) result = find_min_sorting_cost(sequence=a) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split(" ")) def find_min_sorting_cost(sequence): sequence = tuple(sequence) if not sequence: return 0 indices_by_values = {x: [] for x in sequence} for i, x in enumerate(sequence): indices_by_values[x].append(i) borders_by_values = { x: (indices[0], indices[-1]) for x, indices in indices_by_values.items() } borders_sorted_by_values = [ borders for x, borders in sorted(borders_by_values.items()) ] max_cost_can_keep_n = curr_can_keep_n = 1 for prev_border, curr_border in zip( borders_sorted_by_values, borders_sorted_by_values[1:] ): if curr_border[0] > prev_border[1]: curr_can_keep_n += 1 else: if curr_can_keep_n > max_cost_can_keep_n: max_cost_can_keep_n = curr_can_keep_n curr_can_keep_n = 1 if curr_can_keep_n > max_cost_can_keep_n: max_cost_can_keep_n = curr_can_keep_n return len(set(sequence)) - max_cost_can_keep_n main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
from sys import stdin input = stdin.readline def main(): anses = [] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) f = [0] * (n + 1) d = sorted(list(set(a))) for q in range(1, len(d) + 1): f[d[q - 1]] = q for q in range(len(a)): a[q] = f[a[q]] n = len(d) starts, ends = [-1] * (n + 1), [n + 1] * (n + 1) for q in range(len(a)): if starts[a[q]] == -1: starts[a[q]] = q ends[a[q]] = q s = [0] * (n + 1) max1 = -float("inf") for q in range(1, n + 1): s[q] = s[q - 1] * (ends[q - 1] < starts[q]) + 1 max1 = max(max1, s[q]) anses.append(str(len(d) - max1)) print("\n".join(anses)) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
from sys import stdin, stdout def main(): from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) inp1 = [-1] * (n + 1) inp2 = [-1] * (n + 1) for i, ai in enumerate(map(int, stdin.readline().split())): if inp1[ai] < 0: inp1[ai] = i inp2[ai] = i inp1 = tuple(inp1i for inp1i in inp1 if inp1i >= 0) inp2 = tuple(inp2i for inp2i in inp2 if inp2i >= 0) n = len(inp1) ans = 0 cur = 0 for i in range(n): if i and inp1[i] < inp2[i - 1]: cur = 1 else: cur += 1 ans = max(ans, cur) stdout.write(f"{n - ans}\n") main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) compression_dict = {a: id for id, a in enumerate(sorted(set(A)))} A = [compression_dict[a] for a in A] lenth = len(A) l = [-1] * lenth r = [-1] * lenth tot = 0 for i in range(lenth): if l[A[i]] == -1: tot += 1 l[A[i]] = i r[A[i]] = i else: r[A[i]] = i ans = 0 sum = 1 for i in range(1, tot): if l[i] > r[i - 1]: sum += 1 else: ans = max(ans, sum) sum = 1 ans = max(ans, sum) print(tot - ans)
IMPORT ASSIGN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
from sys import stdin input = stdin.readline def Input(): global A, n, D, F n = int(input()) A = list(map(int, input().split())) D = sorted(list(set(A))) F = [0] * (n + 1) def Ans(): for i in range(1, len(D) + 1): F[D[i - 1]] = i for i in range(n): A[i] = F[A[i]] m = len(D) Start = [-1] * (m + 1) End = [n + 1] * (m + 1) for i in range(n): if Start[A[i]] == -1: Start[A[i]] = i End[A[i]] = i S = [0] * (m + 1) Max = -float("inf") for i in range(1, m + 1): S[i] = S[i - 1] * (End[i - 1] < Start[i]) + 1 Max = max(Max, S[i]) Result.append(str(m - Max)) def main(): global Result Result = [] for _ in range(int(input())): Input() Ans() print("\n".join(Result)) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
import sys input = sys.stdin.readline q = int(input()) for testcases in range(q): n = int(input()) A = list(map(int, input().split())) compression_dict = {a: ind for ind, a in enumerate(sorted(set(A)))} A = [compression_dict[a] for a in A] MAX = max(A) miind = [10**6] * (MAX + 1) maind = [0] * (MAX + 1) for i, a in enumerate(A): miind[a] = min(i, miind[a]) maind[a] = max(i, maind[a]) ANS = 0 con = 0 for i in range(MAX): if maind[i] < miind[i + 1]: con += 1 ANS = max(ANS, con) else: con = 0 print(MAX - ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) for _ in range(t): s = input() t = input() n = len(s) m = len(t) flag = 0 if m > n: print("NO") continue pointer = (n - m) % 2 curr = 0 while curr < m and pointer < n: if s[pointer] == t[curr]: curr += 1 pointer += 1 else: pointer += 2 if curr == m: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
T = int(input()) for ia in range(T): s = input() t = input() n = len(s) - 1 n1 = len(t) - 1 while n > -1 and n1 > -1: if s[n] == t[n1]: n -= 1 n1 -= 1 else: n -= 2 if n1 == -1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): st = input() rt = input() pt1 = 0 pt2 = 0 n = len(st) m = len(rt) stack = [-1] queue = [-2] flag = 0 for i in range(n): if pt1 < m and st[i] == rt[pt1] and (stack[-1] + i) % 2 == 1: stack.append(i) pt1 += 1 if pt2 < m and st[i] == rt[pt2] and (queue[-1] + i) % 2 == 1: queue.append(i) pt2 += 1 if pt1 == m and (n - stack[-1]) % 2 == 1: print("YES") elif pt2 == m and (n - queue[-1]) % 2 == 1: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve() -> bool: string = input() target = input() string_len = len(string) target_len = len(target) if string_len < target_len: return False q = k = 0 for i in range((string_len - target_len) % 2, string_len): if k == 1: k = 0 continue if q < target_len and string[i] == target[q]: q += 1 else: k += 1 return q == target_len for _ in range(int(input())): print("YES" if solve() else "NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
T = int(input()) for t in range(T): s = input() t = input() ss = s[::-1] tt = t[::-1] j = 0 for i in range(len(tt)): while j < len(ss) and ss[j] != tt[i]: j += 2 j += 1 if j > len(ss): print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
from sys import stdin input = stdin.readline def answer(): j = 0 for i in range(m): flag = False while j < n: if t[i] == s[j] and i % 2 == j % 2: j += 1 flag = True break j += 1 if flag == False: break if flag and m % 2 == n % 2: return "YES" j = 0 for i in range(1, m + 1): flag = False while j < n: if t[i - 1] == s[j] and i % 2 == j % 2: j += 1 flag = True break j += 1 if flag == False: break if flag and (m + 1) % 2 == n % 2: return "YES" return "NO" for T in range(int(input())): s = input().strip() n = len(s) t = input().strip() m = len(t) print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
from sys import stdin, stdout input = stdin.readline mp = lambda: map(int, input().split()) it = lambda: int(input()) ls = lambda: list(input().strip()) mt = lambda r: [ls() for _ in range(r)] lcm = lambda a, b: a * b // gcd(a, b) def fibo_n(n): return ((1 + sqrt(5)) / 2) ** n / sqrt(5) for _ in range(it()): a = list(input().strip()) b = input().strip() k = len(b) - 1 while k >= 0 and a: if a[-1] == b[k]: a.pop() k -= 1 else: if a: a.pop() if a: a.pop() if k == -1: print("YES") else: print("NO")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
q = int(input()) def solve(): s = input() t = input() n = len(s) m = len(t) i = n - 1 j = m - 1 delete = 0 while i >= 0 and j >= 0: if s[i] == t[j] and not delete: j -= 1 else: delete ^= 1 i -= 1 if j < 0: print("YES") else: print("NO") for _ in range(q): solve()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): dels = len(s) - len(t) si = 0 ti = 0 while si < len(s) and ti < len(t): if t[ti] == s[si]: ti += 1 si += 1 else: si += 2 if ti == len(t): return True return False for _ in range(int(input())): s = input() s = s[::-1] t = input() t = t[::-1] if solve(s, t): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): n = len(s) m = len(t) if n < m: return False p = n - m & 1 q = 0 k = 0 for i in range(p, n): if k == 1: k = 0 continue if q < m and s[i] == t[q]: q += 1 else: k += 1 return q == m n = int(input()) for x in range(n): s = input() t = input() if solve(s, t): print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys number_tests = input("") lines = [] for line in sys.stdin: lines.append(line) slist = [] tlist = [] for i in range(len(lines)): if i % 2 == 0: slist.append(lines[i]) else: tlist.append(lines[i]) for s, t in zip(slist, tlist): j = len(t) - 1 i = len(s) - 1 while j >= 0 and i >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 if j < 0: print("YES") else: print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
n = int(input()) for i in range(n): s, t = input()[::-1], input()[::-1] i = j = 0 while i < len(s) and j < len(t): if s[i] == t[j]: i += 1 j += 1 else: i += 2 print("YES" if j == len(t) else "NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
tx = int(input()) for _ in range(tx): s = input() t = input() if len(s) < len(t): print("NO") continue f = 1 i = len(s) - 1 for j in range(len(t) - 1, -1, -1): while i >= 0 and s[i] != t[j]: i -= 2 if i < 0: f = 0 break i -= 1 if f == 0: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s = input() t = input() s = s[::-1] t = t[::-1] k = 0 last = -1 ans = False for i in range(len(s)): if s[i] == t[k] and (i - last + 1) % 2 == 0: k += 1 last = i if k == len(t): ans = True break print("YES" if ans else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def isPossible(s, t): i, j = len(s) - 1, len(t) - 1 f = 0 while j >= 0: while i >= 0 and s[i] != t[j]: i -= 2 if i < 0: break else: i -= 1 j -= 1 if j >= 0: return False return True def checkPossible(): t1 = int(input()) for k in range(t1): s = input() t = input() ans = isPossible(s, t) if ans: print("YES") else: print("NO") checkPossible()
FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys input = sys.stdin.readline def solve(): s = input().strip() t = input().strip() i = len(t) - 1 j = len(s) - 1 while i >= 0: while j >= 0 and s[j] != t[i]: j -= 2 if j < 0: print("NO") return j -= 1 i -= 1 print("YES") for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys def read(): line = sys.stdin.readline().rstrip() if " " in line: return line.split() return line.strip() def solver(): for i, j in enumerate(A): charIndxs[ord(j) - 97].append(i) for i in charIndxs[ord(B[0]) - 97]: if i & 1: if oddPos[0] == None: oddPos[0] = i elif evenPos[0] == None: evenPos[0] = i for i in range(1, len(B)): ascii = ord(B[i]) - 97 lst = charIndxs[ascii] if oddPos[i - 1] != None: for j in range(oddCharIndx[ascii], len(lst)): if lst[j] > oddPos[i - 1] and lst[j] - oddPos[i - 1] & 1: oddPos[i] = lst[j] oddCharIndx[ascii] = j + 1 break if evenPos[i - 1] != None: for j in range(evenCharIndx[ascii], len(lst)): if lst[j] > evenPos[i - 1] and lst[j] - evenPos[i - 1] & 1: evenPos[i] = lst[j] evenCharIndx[ascii] = j + 1 break T = int(read()) for t in range(T): A = read() B = read() charIndxs = [[] for _ in range(26)] oddPos = [None] * len(B) evenPos = [None] * len(B) oddCharIndx = [0] * 27 evenCharIndx = [0] * 27 solver() if ( oddPos[-1] != None and len(A) - oddPos[-1] & 1 or evenPos[-1] != None and len(A) - evenPos[-1] & 1 ): print("YES") else: print("NO") sys.exit()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR RETURN FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NONE FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NONE FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NONE BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER NONE BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) for i in range(t): s = input() t = input() def solve(s, t): ind = 0 for ch in t: while ind < len(s) and s[ind] != ch: ind += 2 if ind >= len(s): return "NO" ind += 1 return "YES" print(solve(s[::-1], t[::-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): i, j = len(s) - 1, len(t) - 1 while j >= 0 and i >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 if j < 0: print("YES") else: print("NO") for _ in range(int(input())): s, t = input(), input() solve(s, t)
FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
N = int(input()) for _ in range(N): s, t = list(input()), list(input()) while s and t: if s[-1] == t[-1]: s.pop() t.pop() else: s.pop() if s: s.pop() print("NO" if t else "YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): m = len(s) n = len(t) if m < n: return "NO" if m == n: if s == t: return "YES" else: return "NO" cnt = 0 rgt = n - 1 for i in range(m - 1, -1, -1): if rgt < 0: return "YES" if s[i] == t[rgt]: if cnt % 2 == 0: rgt -= 1 cnt = 0 continue else: cnt += 1 else: cnt += 1 if rgt >= 0: return "NO" return "YES" t = int(input()) for i in range(t): s = input() t = input() print(solve(s, t))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING IF VAR VAR IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN STRING IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s = input() t = input() n = len(s) m = len(t) if n < m: print("NO") else: x = m - 1 prev = 0 f = 0 i = n - 1 while i >= 0: if x == -1: f = 1 break if s[i] == t[x]: i -= 1 x -= 1 else: i -= 2 if f == 1 or x == -1: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s, t = input(), input() a, b, paritat = len(s) - 1, len(t) - 1, 0 while a >= 0 and b >= 0: if paritat == 0 and s[a] == t[b]: b -= 1 else: paritat = (paritat + 1) % 2 a -= 1 print("YES" if b < 0 else "NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) results = [] for i in range(t): text = input()[::-1] word = input()[::-1] i_text = 0 i_word = 0 while i_text < len(text) and i_word < len(word): if word[i_word] == text[i_text]: i_text += 1 i_word += 1 else: i_text += 2 results.append(i_word == len(word)) for i in range(t): if results[i]: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
from sys import stdin input = stdin.readline q = int(input()) for _ in range(q): s = input().rstrip() t = input().rstrip() even = -1 odd = -1 for i in range(len(s)): if s[i] == t[0]: if i % 2 == 0 and even == -1: even = i elif i % 2 > 0 and odd == -1: odd = i if (len(s) - len(t)) % 2 == 0: i = even else: i = odd if i == -1: print("NO") continue found = False j = 0 while i < len(s): if s[i] == t[j]: i += 1 j += 1 if j >= len(t): found = True break else: i += 2 if found: print("YES") else: print("NO")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) for _ in range(t): s = str(input()) t = str(input()) lt = len(t) ls = len(s) if lt > ls or ls + 1 == lt: print("No") continue j = len(t) - 1 i = len(s) - 1 while i >= 0 and j >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 if j == -1: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys input = sys.stdin.readline for _ in range(int(input())): s = input()[:-1] t = input()[:-1] n = len(s) m = len(t) p = n - 1 q = m - 1 while p >= 0: if q == -1: break if s[p] != t[q]: p -= 2 else: q -= 1 p -= 1 if q == -1: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for i in range(int(input())): s = input() t = input() n, m = len(s), len(t) if m > n: print("NO") else: if (n + m) % 2 == 1: s = s[1:] n = n - 1 i = 0 f = 0 for ch in t: while i < n and s[i] != ch: i += 2 if i >= n: f = 1 print("NO") break i += 1 if not f: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
from sys import stdout out = lambda x, end="\n": stdout.write(str(x) + end) ip = [*open(0)] for i in range(0, 2 * int(ip[0]), 2): s, t = ip[i + 1][:-1], ip[i + 2][:-1] if t == s: out("YES") elif len(t) >= len(s): out("NO") else: i = len(s) - 1 j = len(t) - 1 while i >= 0 and j >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 if j == -1: out("YES") else: out("NO")
ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s, t = input(), input() ps, pt = len(s) - 1, len(t) - 1 ans = "" while ps >= 0 and len(ans) != len(t): if s[ps] == t[pt]: ans += t[pt] ps -= 1 pt -= 1 else: ps -= 2 if len(ans) == len(t): print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s = input() t = input() i = len(s) - 1 j = len(t) - 1 while i >= 0 and j >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 print("YES") if j == -1 else print("NO") num_inp = lambda: int(input()) arr_inp = lambda: list(map(int, input().split())) sp_inp = lambda: map(int, input().split()) str_inp = lambda: input()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def check(s, t): i = len(s) - 1 j = len(t) - 1 if i < j: return "NO" while i >= 0 and j >= 0: if s[i] == t[j]: i -= 1 j -= 1 else: i -= 2 if j == -1: return "YES" return "NO" test_cases = int(input()) array = [] while test_cases != 0: s = input() t = input() test_cases -= 1 array.append(check(s, t)) for i in array: print(i)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN STRING WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
cant_tests = int(input()) s = [] t = [] for i in range(cant_tests): s.append(input()) t.append(input()) for j in range(cant_tests): str = s[j] t1 = [0, -1] t2 = [0, -2] for i in range(len(str)): if t[j][t1[0]] == str[i] and i % 2 != t1[1] % 2: t1[0] += 1 t1[1] = i if t[j][t2[0]] == str[i] and i % 2 != t2[1] % 2: t2[0] += 1 t2[1] = i if t1[0] == len(t[j]): if (len(str) - t1[1] - 1) % 2 == 0: print("YES") break else: t1[0] -= 1 t1[1] -= 1 if t2[0] == len(t[j]): if (len(str) - t2[1] - 1) % 2 == 0: print("YES") break else: t2[0] -= 1 t2[1] -= 1 if i == len(str) - 1: print("NO")
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 EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): n1 = len(s) n2 = len(t) if n1 < n2: return "NO" s.reverse() t.reverse() j = 0 k = 0 diff = 0 while k < n1 and j < n2: if s[k] == t[j] and diff % 2 == 0: j += 1 diff = 0 else: diff += 1 k += 1 if j == n2: return "YES" return "NO" for _ in range(int(input())): s = list(input()) t = list(input()) print(solve(s, t))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
n = int(input()) for _ in range(n): s = input() t = input() i = len(s) - 1 fail = False for c in t[::-1]: while i >= 0 and c != s[i]: i -= 2 if i < 0 or i == 0 and s[0] != c: fail = True break i -= 1 print("NO" if fail else "YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) for _ in range(t): a = list(input()) b = list(input()) ind = dict() n = len(a) for i in range(len(a)): if a[i] not in ind: ind[a[i]] = [i] else: ind[a[i]].append(i) j = 0 last = 0 done = 0 for i in range(len(b)): cau = j if i == 0: while j < n: if a[j] == b[i] and j % 2 == 0: last = j j += 1 done += 1 break j += 1 else: while j < n: if a[j] == b[i] and (j - last) % 2 == 1: last = j j += 1 done += 1 break j += 1 if cau == j or j == n: break if done == len(b): break if done == len(b) and (j - len(a)) % 2 == 0: print("YES") continue j = 0 last = 0 done = 0 for i in range(len(b)): cau = j if i == 0: while j < n: if a[j] == b[i] and j % 2 == 1: last = j j += 1 done += 1 break j += 1 else: while j < n: if a[j] == b[i] and (j - last) % 2 == 1: last = j j += 1 done += 1 break j += 1 if cau == j or j == n: break if done == len(b): break if done == len(b) and (j - len(a)) % 2 == 0: print("YES") continue print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
a = int(input()) for i in range(0, a): A = input() B = input() p = False L1 = len(A) L2 = len(B) i = L1 - 1 j = L2 - 1 T = 0 while i >= 0 and j >= 0: if A[i] == B[j]: i = i - 1 j = j - 1 p = True T = T + 1 else: i = i - 2 p = False if T == L2: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys input = lambda: sys.stdin.readline() int_arr = lambda: list(map(int, input().split())) str_arr = lambda: list(map(str, input().split())) get_str = lambda: map(str, input().split()) get_int = lambda: map(int, input().split()) get_flo = lambda: map(float, input().split()) mod = 1000000007 def solve(s, t): n, m = len(s), len(t) while n > 0 and m > 0: if s[n - 1] == t[m - 1]: n -= 1 m -= 1 else: n -= 2 if not m: print("YES") else: print("NO") for _ in range(int(input())): s = str(input())[:-1] t = str(input())[:-1] solve(s, t)
IMPORT ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
t = int(input()) for _ in range(t): s = input()[::-1] t = input()[::-1] if len(t) > len(s): print("no") continue fin = "" z = 0 for x in t: for y in range(z, len(s), 2): ch = s[y] if ch == x: z = y + 1 fin += ch break if fin == t: print("yes") else: print("no")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for k in range(int(input())): x = input() y = input() i = len(x) - 1 j = len(y) - 1 while i >= 0 and j >= 0: if x[i] == y[j]: i -= 1 j -= 1 else: i -= 2 print(["NO", "YES"][j == -1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST STRING STRING VAR NUMBER
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
def solve(s, t): ns, nt = len(s), len(t) latest = ns for i in range(nt - 1, -1, -1): c = t[i] z = latest - 1 found = False while z >= 0: if s[z] == c: found = True latest = z break z -= 2 if not found: return False return True def main(): q = int(input()) for _ in range(q): s = input() t = input() if solve(s, t): print("YES") else: print("NO") main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): s = input()[:-1] t = input()[:-1] if len(s) % 2 != len(t) % 2: s = s[1:] idx = 0 i = 0 while i < len(s): if idx < len(t) and s[i] == t[idx]: i += 1 idx += 1 else: i += 2 if idx == len(t): print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys def main(): num_tests = int(sys.stdin.readline()) for _ in range(num_tests): s = sys.stdin.readline() t = sys.stdin.readline() print("YES" if is_possible(s, t) else "NO") def is_possible(s: str, t: str) -> bool: if solve(s, t, 1): return True return solve(s, t, 0) def solve(s: str, t: str, initial_mod: int) -> bool: i = 0 j = 0 cur_mod = initial_mod while i < len(s) and j < len(t): if s[i] == t[j] and i % 2 == cur_mod: j += 1 cur_mod = 1 if cur_mod == 0 else 0 i += 1 return j == len(t) and (len(s) - i) % 2 == 0 main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys def checkBack(s, t): ptr1 = 1 ptr2 = 1 if len(t) > len(s): return False while ptr2 <= len(t) and ptr1 <= len(s): if s[-ptr1] == t[-ptr2]: ptr1, ptr2 = ptr1 + 1, ptr2 + 1 continue elif ptr1 == len(s): return False elif ptr1 == len(s) - 1: return False else: ptr1 = ptr1 + 2 return ptr2 == len(t) + 1 data = sys.stdin.read().split() cases = data[0] ss = data[1::2] ts = data[2::2] for i in range(int(cases)): if checkBack(ss[i], ts[i]): print("YES") else: print("NO")
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for testis in range(int(input())): s = input() t = input() n, m = len(s), len(t) if m > n: print("NO") else: p, q = n - 1, m - 1 while q >= 0: if s[p] == t[q]: p -= 1 q -= 1 else: p -= 2 if p < 0: break if q >= 0: print("NO") else: print("YES")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") for _ in range(int(input())): s = input() t = input() n, m = len(s), len(t) i, j = 0, 0 prev = -1 n -= 1 m -= 1 while i < n and j < m: if s[i] == t[j]: if prev == -1: prev = i i += 1 j += 1 elif (i - prev) % 2: i += 1 j += 1 prev = i - 1 else: i += 1 else: i += 1 last = n for i in range(n, -1, -1): if s[i] == t[-1] and (n + 1 - i) % 2: last = i break if ( j == m and last > prev and s[last] == t[-1] and (prev == -1 or (last - prev) % 2) and (n + 1 - last) % 2 ): print("Yes") continue if t[0] not in s: print("No") continue start = 0 ind = s.index(t[0]) % 2 for i in range(n + 1): if s[i] == t[0] and i % 2 != ind: start = i break i, j = start, 0 prev = -1 while i < n and j < m: if s[i] == t[j]: if prev == -1: prev = i i += 1 j += 1 elif (i - prev) % 2: i += 1 j += 1 prev = i - 1 else: i += 1 else: i += 1 if ( j == m and last > prev and s[last] == t[-1] and (prev == -1 or (last - prev) % 2) and (n + 1 - last) % 2 ): print("Yes") else: print("No")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$. -----Input----- The first line contains a single integer $q$ ($1 \le q \le 10^5$) β€” the number of test cases. The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter. The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). -----Examples----- Input 4 ababa ba ababa bb aaa aaaa aababa ababa Output YES NO NO YES -----Note----- Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
for _ in range(int(input())): s = input() t = input() j = len(t) - 1 i = len(s) - 1 c = 0 while i >= 0 and j >= 0: if c % 2 == 0 and s[i] == t[j]: i -= 1 j -= 1 c = 0 continue i -= 1 c += 1 if j < 0: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING