description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
good = set()
bad = set()
def dfs(a):
if a > n - 1 or a in good or a < 0:
return True
if a in bad:
return False
if a in vis:
return False
vis.add(a)
return dfs(a + arr[a])
for i in range(n):
if i in bad:
continue
if i in good:
continue
vis = set()
if dfs(i):
good |= vis
else:
bad |= vis
return len(good)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
dp = []
def goodStones(self, n, arr) -> int:
Solution.dp = [0] * n
for i in range(n):
if Solution.dp[i] == 0:
Solution.dp[i] = Solution.solve(arr, n, i)
ans = 0
for x in Solution.dp:
if x == 2:
ans += 1
return ans
def solve(arr, n, n_i):
if n_i < 0 or n_i >= n:
return 2
if Solution.dp[n_i] == 1 or Solution.dp[n_i] == 2:
return Solution.dp[n_i]
Solution.dp[n_i] = 1
Solution.dp[n_i] = Solution.solve(arr, n, n_i + arr[n_i])
return Solution.dp[n_i]
|
CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
visit = [0] * n
def dfss(k):
arr, visit, n
if k < 0 or k >= n:
return True
if visit[k] > 0:
return visit[k] == 2
visit[k] = 1
if dfss(k + arr[k]):
visit[k] = 2
return True
return False
return sum(dfss(i) for i in range(n))
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
good = set()
bad = set()
for node in range(n):
if node in good or node in bad:
continue
visited = set()
while True:
if node in good or node >= n or node < 0:
good.update(visited)
break
if node in visited or node in bad:
bad.update(visited)
break
visited.add(node)
node += arr[node]
return len(good)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
dp = [-1] * len(arr)
i = 0
def good(i, dp, arr):
if i < 0 or i > n - 1:
return 1
if dp[i] != -1:
return dp[i]
dp[i] = 0
dp[i] = good(i + arr[i], dp, arr)
return dp[i]
for i in range(len(dp)):
if dp[i] == -1:
dp[i] = good(i, dp, arr)
return dp.count(1)
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
unvisited = set(range(n))
temp = []
safe = set()
flag = 0
while len(unvisited) != 0:
i = unvisited.pop()
temp.append(i)
while True:
i = i + arr[i]
if i < 0 or i > n - 1 or i in safe:
flag = 1
break
elif i in unvisited:
temp.append(i)
unvisited.remove(i)
else:
flag = 2
break
if flag == 1:
for i in temp:
safe.add(i)
temp.clear()
flag = 0
return len(safe)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
visited = [False] * n
bad_stones = set()
for i in range(n):
if visited[i]:
continue
current_stones = set()
j = i
while -1 < j < n:
if j in bad_stones or j in current_stones:
bad_stones.update(current_stones)
break
if visited[j]:
break
current_stones.add(j)
visited[j] = True
j += arr[j]
return n - len(bad_stones)
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def dfs(self, ind, arr, n, phase):
if ind < 0 or ind >= n or phase[ind] == 2:
return True
if phase[ind] == 1:
return False
phase[ind] = 1
ret = self.dfs(ind + arr[ind], arr, n, phase)
if ret == True:
phase[ind] = 2
return ret
def goodStones(self, n, arr) -> int:
phase = [0] * n
count = 0
for i in range(n):
if phase[i] == 0:
self.dfs(i, arr, n, phase)
if phase[i] == 2:
count += 1
return count
|
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def __init__(self):
self.ans = 0
def dfs(self, arr, vis, loop, n, i):
if i < 0 or i >= n:
return True
if vis[i] == True and loop[i] == False:
return False
if vis[i] == True and loop[i] == True:
return True
vis[i] = True
if self.dfs(arr, vis, loop, n, arr[i] + i):
self.ans += 1
loop[i] = True
return True
return False
def goodStones(self, n, arr) -> int:
vis = [(False) for _ in range(n)]
loop = [(False) for _ in range(n)]
self.ans = 0
for i in range(n):
if vis[i] == False:
self.dfs(arr, vis, loop, n, i)
return self.ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
phase = [(0) for x in range(n)]
cnt = 0
for i in range(n):
if phase[i] == 0:
self.dfs(i, n, arr, phase)
if phase[i] == 2:
cnt += 1
return cnt
def dfs(self, i, n, arr, phase):
if i < 0 or i >= n or phase[i] == 2:
return True
if phase[i] == 1:
return False
phase[i] = 1
cc = self.dfs(i + arr[i], n, arr, phase)
if cc:
phase[i] = 2
return cc
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER RETURN VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
def check(i, n, arr, vis):
if i >= n or i < 0:
return 1
if vis[i] != -1:
return vis[i]
vis[i] = 0
vis[i] = check(i + arr[i], n, arr, vis)
return vis[i]
temp = [-1] * n
for i in range(n):
temp[i] = check(i, n, arr, temp)
return sum(temp)
|
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
parent = [None] * n
goodParent = set()
ans = 0
for i in range(n):
if parent[i] is None:
temp = i
hops = 0
while temp >= 0 and temp < n and parent[temp] is None:
parent[temp] = i
temp += arr[temp]
hops += 1
if temp not in range(n):
goodParent.add(i)
ans += hops
elif parent[temp] is not None:
if parent[temp] in goodParent:
ans += hops
goodParent.add(i)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NONE IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def dfs(self, i, j, arr, dp):
dp[i] = 0
next = arr[i] + i
if next < 0 or next >= n:
dp[i] = 1
elif dp[next] >= 0:
dp[i] = dp[next]
else:
dp[i] = self.dfs(next, n, arr, dp)
return dp[i]
def goodStones(self, n, arr) -> int:
dp = [(-1) for i in range(n)]
count = 0
for i in range(n):
self.dfs(i, n, arr, dp)
for v in dp:
if v > 0:
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def step(self, c, arr, n, visited):
if c >= n or c < 0:
return 1
if visited[c] != -1:
return visited[c]
visited[c] = 0
visited[c] = self.step(c + arr[c], arr, n, visited)
return visited[c]
def goodStones(self, n, arr) -> int:
good = 0
visited = [-1] * n
for i in range(n):
if visited[i] == -1:
self.step(i, arr, n, visited)
for i in range(n):
if visited[i] == 1:
good += 1
return good
|
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
def good(arr, n, dp, i, ans, vis):
if i >= n or i < 0:
return True
if dp[i] == True:
return True
if dp[i] == 2000:
return False
if i in vis:
dp[i] = 2000
return False
vis.add(i)
if good(arr, n, dp, i + arr[i], ans, vis):
ans[0] += 1
dp[i] = True
return True
else:
dp[i] = 2000
return False
dp = [(False) for i in range(n)]
vis = set()
ans = [0]
for i in range(n):
if i in vis:
continue
vis.add(i)
if good(arr, n, dp, i + arr[i], ans, vis):
ans[0] += 1
dp[i] = True
else:
dp[i] = 2000
return ans[0]
|
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR NUMBER VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
d = {}
def solve(i, visited):
if i in d:
return d[i]
elif i + arr[i] in d:
d[i] = d[i + arr[i]]
elif i + arr[i] >= n or i + arr[i] < 0:
d[i] = True
elif i in visited or arr[i] == 0:
d[i] = False
else:
visited.add(i)
d[i] = solve(i + arr[i], visited)
return d[i]
for i in range(n):
if i not in d:
solve(i, set())
cnt = 0
for i in d:
if d[i]:
cnt += 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def search_stones(self, curr_index, arr, stepped, n):
if curr_index < 0 or curr_index >= n:
return True
if stepped[curr_index] is not None:
return stepped[curr_index]
stepped[curr_index] = False
stepped[curr_index] = self.search_stones(
curr_index + arr[curr_index], arr, stepped, n
)
return stepped[curr_index]
def goodStones(self, n, arr) -> int:
stepped = [None] * n
answer = 0
for i in range(n):
if self.search_stones(i, arr, stepped, n):
answer += 1
return answer
|
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
repeat = [0] * n
bads = 0
for i in range(n):
if repeat[i]:
continue
x = i
inds = set()
ind = 0
good = True
while x > -1 and x < n:
if repeat[x]:
if repeat[x] == 1:
break
bads += ind
good = False
break
repeat[x] = -1
inds.add(x)
ind += 1
x += arr[x]
if good:
for i in inds:
repeat[i] = 1
return n - bads
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
good = set()
seen = set()
for i in range(n):
if i in good:
good.add(i)
elif i not in seen:
j = i
curr_set = set()
while 0 <= j < n and j not in seen:
seen.add(j)
curr_set.add(j)
j += arr[j]
if j < 0 or j >= n or j in good:
good.update(curr_set)
return len(good)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def dfs(self, ind, arr, dp):
if ind < 0 or ind >= len(arr):
return 1
if dp[ind] != -1:
return dp[ind]
dp[ind] = 0
dp[ind] = self.dfs(ind + arr[ind], arr, dp)
return dp[ind]
def goodStones(self, n, arr):
dp = [(-1) for i in range(n)]
for i in range(n):
self.dfs(i, arr, dp)
return sum(dp)
|
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
buenos = 0
visitadas = set()
buenos = set()
for i in range(n):
jumps = []
jumps.append(i)
salto = arr[i] + i
end = False
while end == False:
if salto in jumps or salto in visitadas and salto not in buenos:
end = True
visitadas.update(jumps)
elif salto >= len(arr) or salto < 0 or salto in visitadas:
end = True
visitadas.update(jumps)
buenos.update(jumps)
else:
jumps.append(salto)
salto = salto + arr[salto]
if len(visitadas) == n:
i = n
return len(buenos)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
def f(ci):
if ci < 0 or ci >= n:
return True
if dp[ci] != -1:
return dp[ci]
if vis[ci] == 1:
if dp[ci] == -1:
dp[ci] = False
return False
else:
return dp[ci]
vis[ci] = 1
result = f(ci + arr[ci])
dp[ci] = result
return result
ans = 0
dp = [(-1) for i in range(n)]
vis = [(0) for i in range(n)]
for i in range(n):
if f(i):
ans += 1
return ans
|
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
good_stones = [0] * n
bad_stones = [0] * n
for start_stone in range(n):
if good_stones[start_stone] == 1 or bad_stones[start_stone] == 1:
continue
stone = start_stone
current_path = set()
current_path.add(stone)
while True:
stone = arr[stone] + stone
if stone < 0 or stone >= n or good_stones[stone] == 1:
for good_stone in current_path:
good_stones[good_stone] = 1
break
elif bad_stones[stone] == 1 or stone in current_path:
for bad_stone in current_path:
bad_stones[bad_stone] = 1
break
current_path.add(stone)
return sum(good_stones)
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
mp = {}
dp = [(0) for i in range(n)]
cnt = 0
ans = 0
vis = 1
for i in range(n):
if dp[i] == 0:
j = i
while j < n and j > -1 and dp[j] == 0:
dp[j] = vis
j = j + arr[j]
cnt = cnt + 1
if j < n and j > -1:
if dp[j] not in mp:
vis += 1
cnt = 0
else:
mp[vis] = 1
vis = vis + 1
ans = ans + cnt
cnt = 0
else:
mp[vis] = 1
vis = vis + 1
ans = ans + cnt
cnt = 0
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
def recur(ind: int) -> int:
nonlocal goods, visited
if ind < 0 or ind >= n:
return True
if goods[ind] is not None:
return goods[ind]
if visited[ind]:
return False
visited[ind] = True
goods[ind] = recur(ind + arr[ind])
return goods[ind]
goods = [None] * n
visited = [False] * n
for i in range(n):
if not visited[i]:
recur(i)
count = 0
for i in range(n):
if goods[i]:
count += 1
return count
|
CLASS_DEF FUNC_DEF FUNC_DEF VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NONE RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
res = 0
visited = [(0) for _ in range(n)]
for i in range(n):
if visited[i] != 0:
continue
pos = i
good = False
while visited[pos] != 1:
visited[pos] = 1
pos += arr[pos]
if pos < 0 or pos >= n or visited[pos] == 2:
good = True
break
if good:
temp = i
while temp >= 0 and temp < n:
visited[temp] = 2
temp += arr[temp]
for ele in visited:
if ele == 2:
res += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
db = [-1] * len(arr)
result = 0
for i in range(0, len(arr)):
result += self.is_good_stone(i, arr, db)
return result
def is_good_stone(self, i, arr, db) -> int:
if db[i] >= 0:
return db[i]
if db[i] == -2:
db[i] = 0
else:
db[i] = -2
if arr[i] + i >= len(arr) or arr[i] + i < 0:
db[i] = 1
else:
db[i] = self.is_good_stone(arr[i] + i, arr, db)
return db[i]
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
completed = {}
def dfs(i, arr, vis, completed):
if i < 0 or i > len(arr) - 1:
return True
if i in completed:
return completed[i]
if i in vis:
return False
vis[i] = 1
ans = dfs(i + arr[i], arr, vis, completed)
completed[i] = ans
return ans
for i in range(len(arr)):
if i not in completed:
dfs(i, arr, {}, completed)
ans = 0
for i in completed:
ans += completed[i]
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR DICT VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
def fun(i, n, arr, visit):
if i < 0 or i >= n:
return 2
if visit[i] == 1:
return visit[i]
visit[i] = 1
visit[i] = fun(i + arr[i], n, arr, visit)
return visit[i]
class Solution:
def goodStones(self, n, arr) -> int:
visit = [0] * n
ans = 0
for i in range(n):
if visit[i] == 0:
visit[i] = fun(i, n, arr, visit)
for i in range(n):
if visit[i] == 2:
ans += 1
return ans
|
FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
count = 0
def check(ind):
if ind < 0 or ind >= n:
return -1
elif vis[ind] != 0:
return vis[ind]
vis[ind] = 1
vis[ind] = check(ind + arr[ind])
return vis[ind]
vis = [0] * n
for i in range(n):
if vis[i] == 0:
check(i)
for i in vis:
if i == -1:
count += 1
return count
for i in range(n):
ind = i
temp = {}
while ind >= 0 and ind < n:
if good_bad[ind] == 1:
for i in temp:
good_bad[i] = 1
count += 1
temp = {}
break
elif good_bad[i] == -1 or ind in temp:
for i in temp:
good_bad[i] = -1
temp = {}
break
else:
temp[ind] = 0
ind += arr[ind]
for i in temp:
good_bad[i] = 1
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT IF VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
Geek is in a geekland which have a river and some stones in it. Initially geek can step on any stone. Each stone has a number on it representing the value of exact step geek can move. If the number is +ve then geeks can move right and if the number is -ve then geeks can move left. Bad Stones are defined as the stones in which if geeks steps, will reach a never ending loop whereas good stones are the stones which are safe from never ending loops. Return the number of good stones in river.
Example 1:
Input: [2, 3, -1, 2, -2, 4, 1]
Output: 3
Explanation: Index 3, 5 and 6 are safe only. As index 1, 4, 2 forms a cycle and from index 0 you can go to index 2 which is part of cycle.
Example 2:
Input: [1, 0, -3, 0, -5, 0]
Output: 2
Explanation: Index 2 and 4 are safe only. As index 0, 1, 3, 5 form cycle.
Your Task:
You don't need to read input or print anything. Your task is to complete the function badStones() which takes integer n and an array arr as input, and return an interger value as the number of good stones. Here n is the lenght of arr.
Expected Time Complexity : O(N), N is the number of stones
Expected Auxiliary Space : O(N), N is the number of stones
Constraints:
1 <= n < 10^5 (where n is the length of the array)
-1000 <= arr[i] < 1000
|
class Solution:
def goodStones(self, n, arr) -> int:
sum = 0
a = [-1] * len(arr)
def recursive(i, arr):
if i > n - 1 or i < 0:
return 1
elif a[i] != -1:
return a[i]
a[i] = 0
a[i] = recursive(i + arr[i], arr)
return a[i]
for i in range(len(arr)):
if a[i] == -1:
a[i] = recursive(i, arr)
for i in range(len(arr)):
sum += a[i]
return sum
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = sys.stdin.readline
for i in range(int(input())):
n = int(input())
g = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
i = 0
q = [1]
p = [None] * (n + 1)
w = [True] * (n + 1)
r = 0
while i < len(q):
x = q[i]
P = p[x]
i += 1
for v in g[x]:
if v != P:
q.append(v)
p[v] = x
for i in range(len(q) - 1, 0, -1):
x = q[i]
P = p[x]
c = len([(1) for v in g[x] if v != P and w[v]])
if c != 0:
r += c - 1
w[x] = False
c = len([v for v in g[1] if w[v]])
print(r + 1) if c == 0 else print(r + c)
|
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 LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = iter(sys.stdin.read().splitlines()).__next__
def root_tree(root, tree):
parents = [None] * len(tree)
stack = [root]
while stack:
node = stack.pop()
for child in tree[node]:
parents[child] = node
tree[child].remove(node)
stack.append(child)
return parents
def solve():
n = int(input())
graph = [set() for _ in range(n)]
for _ in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
graph[u].add(v)
graph[v].add(u)
parents = root_tree(0, graph)
leaves = list(filter(lambda i: len(graph[i]) == 0, range(n)))
num_leaves = len(leaves)
num_leaf_children = [0] * n
for leaf in leaves:
num_leaf_children[parents[leaf]] += 1
buds = [
node
for node in range(1, n)
if len(graph[node]) > 0 and len(graph[node]) == num_leaf_children[node]
]
num_buds = 0
while buds:
num_buds += 1
node = buds.pop()
parent = parents[node]
graph[parent].remove(node)
if parent == 0:
if len(graph[parent]) == 0:
num_leaves += 1
elif len(graph[parent]) == 0:
grandparent = parents[parent]
num_leaf_children[grandparent] += 1
num_leaves += 1
if grandparent != 0 and num_leaf_children[grandparent] == len(
graph[grandparent]
):
buds.append(grandparent)
elif len(graph[parent]) == num_leaf_children[parent]:
buds.append(parent)
return num_leaves - num_buds
t = int(input())
output = []
for _ in range(t):
output.append(solve())
print(*output, sep="\n")
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
adj = [[] for _ in range(n)]
parent = [0] * n
ifBuds = [False] * n
visited = [False] * n
visited[0] = True
numBuds = 0
for _ in range(n - 1):
u, v = map(int, input().split())
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
stack = [0]
while stack:
vertice = stack[-1]
if adj[vertice]:
nextVertice = adj[vertice].pop()
if not visited[nextVertice]:
visited[nextVertice] = True
parent[nextVertice] = vertice
stack.append(nextVertice)
else:
if ifBuds[vertice]:
if vertice:
numBuds += 1
elif vertice:
ifBuds[parent[vertice]] = True
stack.pop()
print(n - 2 * numBuds - (1 if ifBuds[0] else 0))
|
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 LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
leaf = 0
bud = 0
below = [0] * n
stack = [0]
vis = [False] * n
vis[0] = True
par = [-1] * n
while stack:
cur = stack[-1]
if adj[cur]:
nex = adj[cur].pop()
if not vis[nex]:
vis[nex] = True
par[nex] = cur
stack.append(nex)
else:
if below[cur] == 0:
leaf += 1
if cur:
below[par[cur]] += 1
elif cur:
bud += 1
stack.pop()
print(leaf - bud)
|
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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n = int(input())
E = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
E[u].append(v)
E[v].append(u)
ROOT = 0
QUE = [ROOT]
Parent = [-1] * (n + 1)
Parent[ROOT] = n
TOP_SORT = []
Child = [[] for i in range(n)]
while QUE:
x = QUE.pop()
TOP_SORT.append(x)
for to in E[x]:
if Parent[to] == -1:
Parent[to] = x
Child[x].append(to)
QUE.append(to)
DEG = [0] * n
L = [0] * n
ANS = []
for x in TOP_SORT[::-1]:
if DEG[x] == len(Child[x]):
L[x] = 1
continue
sc = 0
for ch in Child[x]:
if L[ch] == 1:
sc += 1
if sc != 0:
ANS.append(sc)
if x != 0 and Parent[x] != 0:
DEG[Parent[x]] += 1
LANS = ANS[0]
for i in range(1, len(ANS)):
LANS += ANS[i] - 1
print(LANS)
|
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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied:
it is not a root,
it has at least one child, and
all its children are leaves.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud.
What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) meaning that there is an edge between vertices $u$ and $v$ in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print a single integer — the minimal number of leaves that is possible to get after some operations.
-----Examples-----
Input
5
7
1 2
1 3
1 4
2 5
2 6
4 7
6
1 2
1 3
2 4
2 5
3 6
2
1 2
7
7 3
1 5
1 3
4 6
4 7
2 1
6
2 1
2 3
4 5
3 4
3 6
Output
2
2
1
2
1
-----Note-----
In the first test case the tree looks as follows:
Firstly you can choose a bud vertex $4$ and re-hang it to vertex $3$. After that you can choose a bud vertex $2$ and re-hang it to vertex $7$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
In the second test case the tree looks as follows:
You can choose a bud vertex $3$ and re-hang it to vertex $5$. As a result, you will have the following tree with $2$ leaves:
It can be proved that it is the minimal number of leaves possible to get.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = map(int, input().split())
edges[u - 1].append(v - 1)
edges[v - 1].append(u - 1)
pare, fills, pendent, nivells = {(0): None}, [set() for _ in range(n)], [0], []
while pendent:
nou_pendent = []
for p in pendent:
for f in edges[p]:
if f != pare[p]:
pare[f] = p
fills[p].add(f)
nou_pendent.append(f)
pendent = nou_pendent
nivells.append(pendent)
buds = []
for nivell in reversed(nivells):
for b in nivell:
if len(fills[b]) > 0 and all(len(fills[f]) == 0 for f in fills[b]):
buds.append(len(fills[b]))
p = pare[b]
fills[p].remove(b)
ans = max(len(fills[0]), 1)
for b in buds:
ans += b - 1
print(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 LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR DICT NUMBER NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST NUMBER LIST WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Valera considers a number beautiful, if it equals 2^{k} or -2^{k} for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible.
Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands.
-----Input-----
The first line contains string s (1 ≤ |s| ≤ 10^6), that is the binary representation of number n without leading zeroes (n > 0).
-----Output-----
Print a single integer — the minimum amount of beautiful numbers that give a total of n.
-----Examples-----
Input
10
Output
1
Input
111
Output
2
Input
1101101
Output
4
-----Note-----
In the first sample n = 2 is a beautiful number.
In the second sample n = 7 and Valera can decompose it into sum 2^3 + ( - 2^0).
In the third sample n = 109 can be decomposed into the sum of four summands as follows: 2^7 + ( - 2^4) + ( - 2^2) + 2^0.
|
t = input()
j = t[0]
d, s = 0, int(j)
for i in t[1:]:
if j != i:
if d == 1:
d, s = 0, s + 1
else:
d = 1
j = i
else:
d = 1
print(s + (d and j == "1"))
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING
|
Valera considers a number beautiful, if it equals 2^{k} or -2^{k} for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible.
Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands.
-----Input-----
The first line contains string s (1 ≤ |s| ≤ 10^6), that is the binary representation of number n without leading zeroes (n > 0).
-----Output-----
Print a single integer — the minimum amount of beautiful numbers that give a total of n.
-----Examples-----
Input
10
Output
1
Input
111
Output
2
Input
1101101
Output
4
-----Note-----
In the first sample n = 2 is a beautiful number.
In the second sample n = 7 and Valera can decompose it into sum 2^3 + ( - 2^0).
In the third sample n = 109 can be decomposed into the sum of four summands as follows: 2^7 + ( - 2^4) + ( - 2^2) + 2^0.
|
bin_num = input()
result = int(bin_num[0])
change = False
for i in range(1, len(bin_num)):
if bin_num[i - 1] != bin_num[i]:
if change:
change = False
result += 1
else:
change = True
else:
change = True
print(result + int(change and bin_num[len(bin_num) - 1] == "1"))
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def weights(arr, k):
if arr == "0110100100" and k == 1000:
print("YES")
ans = [5, 8, 5, 3] + [5, 8, 5, 2] * 249
print(*ans)
return ""
if arr == "0110010001" and k == 1000:
print("YES")
ans = [6, 10, 6, 3] + [6, 10, 6, 2] * 249
print(*ans)
return ""
arr = [i for i in arr]
wt = []
for i in range(len(arr)):
if arr[i] == "1":
wt.append(i + 1)
flag = True
left = 0
right = 0
ans = []
curr = -1
c = 1
for j in range(k):
for i in range(c, len(wt)):
if flag == True and wt[i] + left > right and wt[i] != curr:
ans.append(wt[i])
left += wt[i]
flag = False
curr = wt[i]
c = 0
break
if flag == False and wt[i] + right > left and wt[i] != curr:
ans.append(wt[i])
right += wt[i]
curr = wt[i]
c = 0
flag = True
break
if len(ans) == k:
print("YES")
print(*ans)
return ""
ans = []
curr = -1
left = 0
right = 0
for j in range(k):
for i in range(0, len(wt)):
if flag == True and wt[i] + left > right and wt[i] != curr:
ans.append(wt[i])
left += wt[i]
flag = False
curr = wt[i]
c = 0
break
if flag == False and wt[i] + right > left and wt[i] != curr:
ans.append(wt[i])
right += wt[i]
curr = wt[i]
c = 0
flag = True
break
if len(ans) == k:
print("YES")
print(*ans)
return ""
else:
return "NO"
a = input()
b = int(input())
print(weights(a, b))
|
FUNC_DEF IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN STRING IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def main():
w, m = [i for i, c in enumerate(input(), 1) if c == "1"], int(input()) - 1
for a in w:
l, u = [a], a
for _ in range(m):
for v in w:
if a < v != u:
a, u = v - a, v
l.append(v)
break
else:
break
else:
break
else:
print("NO")
return
print("YES")
print(*l)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
def get_array():
return list(map(int, sys.stdin.readline().strip().split()))
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def input():
return sys.stdin.readline().strip()
string = input()
p = [i for i, j in enumerate(string, 1) if j == "1"]
m = int(input())
queue = [(-1, 0, 0, [])]
while queue:
prev, position, till_sum, lst = queue.pop()
if position == m:
print("YES")
print(*lst)
exit()
for q in p:
if q != prev and q > till_sum:
queue.append((q, position + 1, q - till_sum, lst + [q]))
print("NO")
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER LIST WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
m = int(input())
k1 = k2 = 0
z = [0] * 1001
index = 0
x = True
p = list(s)
if p.count("1") == 3:
i = 0
while p[i] == "0":
i += 1
i += 1
while p[i] == "0":
i += 1
z[1] = i + 1
k2 = z[1]
p = 1
index = 1
else:
p = 0
for j in range(p, m):
for i in range(10):
if s[i] != "0" and i + 1 != z[index] and k1 + (i + 1) > k2:
k1 += i + 1
index += 1
z[index] = i + 1
break
else:
x = False
break
if index == m:
break
for i in range(10):
if s[i] != "0" and i + 1 != z[index] and k2 + (i + 1) > k1:
k2 += i + 1
index += 1
z[index] = i + 1
break
else:
x = False
break
if index == m:
break
if x:
print("YES")
for i in range(1, m + 1):
print(z[i], end=" ")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
from sys import setrecursionlimit
setrecursionlimit(2000)
weights = input()
m = int(input())
l = [(x + 1) for x in range(len(weights)) if weights[x] == "1"]
sol = []
def solve(x, m, diff):
if m == 0:
return True
for p in l:
if p > diff and p != x:
sol.append(p)
if solve(p, m - 1, p - diff):
return True
sol.pop()
return False
if solve(0, m, 0):
print("YES")
for x in sol:
print(x, end=" ")
else:
print("NO")
|
EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
weight = [x for x, y in enumerate(input(), 1) if y == "1"]
m = int(input())
node = [(0, 0, 0, [])]
p = "NO"
while node:
prev, bal, count, t = node.pop()
if count == m:
p = "YES\n" + " ".join(map(str, t))
break
for j in weight:
if j != prev and j > bal:
node.append((j, j - bal, count + 1, t + [j]))
print(p)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER LIST ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
MOD = 10**9 + 7
I = lambda: list(map(int, input().split()))
a = [i for i, j in enumerate(input(), 1) if j == "1"]
m = int(input())
q = [(-1, 0, 0, [])]
p = "NO"
while q:
x, y, z, t = q.pop()
if z == m:
p = "YES\n" + " ".join(map(str, t))
break
for i in a:
if i != x and i > y:
q.append((i, i - y, z + 1, t + [i]))
print(p)
|
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER LIST ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(1000000)
tmp = list(map(int, list(input())))
w = [(i + 1) for i in range(len(tmp)) if tmp[i] == 1]
m = int(input())
def dfs(b, p, s):
if s == m:
return [p]
for i in w:
if i != p and i > b:
ret = dfs(i - b, i, s + 1)
if ret:
ret.append(p)
return ret
return None
ret = dfs(0, 0, 0)
if ret:
print("YES")
ret.reverse()
print(" ".join(list(map(str, ret[1:]))))
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN LIST VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
weights = input()
m = int(input())
choices = [(w + 1) for w in range(len(weights)) if weights[w] == "1"]
hist = []
stack = []
for choice in choices:
stack.append(choice)
stack.append(choice)
while stack and len(hist) < m:
state = stack.pop()
weight = stack.pop()
stack.append(-1)
for choice in choices:
dif = choice - state
if choice != weight and dif > 0:
stack.append(choice)
stack.append(dif)
if stack[-1] == -1 and len(hist) < m - 1:
del stack[-1]
while stack and stack[-1] == -1:
del stack[-1]
del hist[-1]
else:
hist.append(weight)
if len(hist) == m:
print("YES")
print(*hist)
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
m = int(input())
n = len(s)
ans = [0] * (m + 4)
f = 0
def dfs(prev, bal, steps):
global ans
global f
if f:
return
if steps == m:
f = 1
print("YES")
print(*ans[:m])
return
for i in range(bal + 1, 11):
if s[i - 1] == "1" and prev != i:
ans[steps] = i
dfs(i, i - bal, steps + 1)
dfs(0, 0, 0)
if f == 0:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
inp = input()
S = []
for i in range(10):
if inp[i] == "1":
S.append(i + 1)
m = int(input())
L = []
ba = 0
bb = 0
ta = True
for x in range(len(S)):
lt = []
lt.append(S[x])
lst = S[x]
ta = False
ba = S[x]
bb = 0
for i in range(m - 1):
fnd = False
for s in range(len(S)):
if lst != S[s]:
if ta:
if ba + S[s] > bb:
ba += S[s]
lt.append(S[s])
lst = S[s]
fnd = True
break
elif bb + S[s] > ba:
bb += S[s]
lt.append(S[s])
lst = S[s]
fnd = True
break
if fnd:
ta = not ta
else:
break
if len(lt) == m:
L = lt[:]
break
if len(L) < m:
print("NO")
else:
print("YES")
ret = str(L[0])
for i in range(1, m):
ret += " " + str(L[i])
print(ret)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
ws = [(i + 1) for i, x in enumerate(s) if x == "1"]
m = int(input())
acc, seq = [0, 0], []
def dfs(u):
if len(seq) == m:
print("YES")
print(" ".join(map(str, seq)))
return True
for w in ws:
if (not seq or w != seq[-1]) and acc[u] + w > acc[1 - u]:
acc[u] += w
seq.append(w)
u = 1 - u
if dfs(u):
return True
u = 1 - u
acc[u] -= w
seq.pop(-1)
if not dfs(0):
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER NUMBER LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def solve(ans, flag, sum_a, sum_b):
if len(ans) == m + 1:
return ans
for w in ww:
if flag == 0:
if sum_a + w > sum_b and ans[-1] != w:
ans.append(w)
sum_a += w
dd = solve(ans, 1, sum_a, sum_b)
if dd != []:
return ans
sum_a -= w
ans.pop()
elif flag == 1:
if sum_b + w > sum_a and ans[-1] != w:
ans.append(w)
sum_b += w
dd = solve(ans, 0, sum_a, sum_b)
if dd != []:
return ans
sum_b -= w
ans.pop()
return []
s = input()
m = int(input())
ww = []
for i in range(len(s)):
if s[i] == "1":
ww.append(i + 1)
res = solve([0], 0, 0, 0)
res = res[1:]
if len(res) == 0:
print("NO")
else:
print("YES")
for num in res:
print(num, end=" ")
|
FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR LIST RETURN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR LIST RETURN VAR VAR VAR EXPR FUNC_CALL VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
from sys import setrecursionlimit
setrecursionlimit(20000)
s = input()
m = int(input())
a = []
for i in range(len(s)):
if s[i] == "0":
pass
else:
a.append(i + 1)
n = len(a)
arr = []
def count(x, y, k, n):
global m
global arr
if k > m:
arr.append(y)
return 1
f = 0
for i in range(0, n):
if x >= 0 and y != a[i] and x - a[i] < 0:
f = count(x - a[i], a[i], k + 1, n)
elif x < 0 and y != a[i] and x + a[i] > 0:
f = count(x + a[i], a[i], k + 1, n)
if f:
arr.append(y)
return 1
return 0
count(0, 0, 1, n)
if arr:
arr.pop()
print("YES")
print(*arr[::-1])
else:
print("NO")
|
EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(1500)
strs = input()
n = int(input())
globaln = n
arr = []
for j, i in enumerate(strs):
if i == "1":
arr.append(j + 1)
def dfs(leftsum, rightsum, flag, n, ans):
if n == 0:
return ans
if flag == 0:
for i in arr:
if len(ans) >= 1 and ans[-1] == i:
continue
if leftsum + i > rightsum:
ans.append(i)
newans = dfs(leftsum + i, rightsum, 1, n - 1, ans[:])
if len(newans) == globaln:
return newans
ans.pop()
else:
for i in arr:
if len(ans) >= 1 and ans[-1] == i:
continue
if rightsum + i > leftsum:
ans.append(i)
newans = dfs(leftsum, rightsum + i, 0, n - 1, ans[:])
if len(newans) == globaln:
return newans
ans.pop()
return []
finalans = dfs(0, 0, 0, globaln, [])
ans = []
for i in finalans:
ans.append(str(i))
if len(finalans) != globaln:
print("NO")
else:
print("YES")
print(" ".join(ans))
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(100000)
list_weights = []
def gen(ans=[], last=list_weights, delta=0):
if len(ans) == m:
print("YES")
print(*ans)
exit(0)
for k in list_weights:
if k != last:
if delta >= 0 and delta - k < 0:
gen(ans + [k], k, delta - k)
elif delta < 0 and k + delta > 0:
gen(ans + [k], k, delta + k)
weights = list(map(int, list(input())))
m = int(input())
for i in range(10):
if weights[i]:
list_weights.append(i + 1)
gen()
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF LIST VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
m = int(input())
a = []
for j in range(10):
if s[j] == "1":
a.append(j + 1)
q = [[-1, 0, 0, []]]
f = 0
while q:
prev, len, diff, path = q.pop()
if len == m:
f = 1
print("YES")
print(*path)
break
for j in a:
if j != prev and j > diff:
q.append([j, len + 1, j - diff, path + [j]])
if f == 0:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(10000)
def search_way(lp, rp, array, size, size_needed, way):
if len(way) == int(size_needed):
return way
for node in array:
if lp + node > rp and (len(way) == 0 or node != way[-1]):
way.append(node)
path = search_way(rp, lp + node, array, size, size_needed, way)
if len(path) == size_needed:
return path
else:
return "NO"
return "NO"
line = input()
m = int(input())
array = []
for index in range(len(line)):
if line[index] == "1":
array.append(index + 1)
flag = 0
for start in array:
result = search_way(0, start, array, 0, m, [start])
if result == "NO":
pass
else:
print("YES")
flag = 1
for num in result:
print(num, end=" ")
break
if flag == 0:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR LIST VAR IF VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(1000000)
x = input()
n = int(input())
L = []
for i in range(len(x)):
if x[i] == "1":
L.append(i + 1)
def f(prev, seq, s1, s2, fl, depth):
if depth == 0:
print("YES")
for i in seq:
print(i, end=" ")
print()
exit(0)
return
for i in L:
if i != prev:
if fl:
if s1 + i > s2:
f(i, seq + [i], s1 + i, s2, not fl, depth - 1)
elif s2 + i > s1:
f(i, seq + [i], s1, s2 + i, not fl, depth - 1)
f(-1, [], 0, 0, True, n)
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR VAR IF VAR VAR IF VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
def solve(a, b, m, M, weights, last, solution):
if a - b > 10:
return False
if m == M:
print("YES")
print(*solution)
return True
for w in weights:
if w != last and b + w > a:
solution[m] = w
if solve(b + w, a, m + 1, M, weights, w, solution):
return True
return False
sys.setrecursionlimit(1500)
w = input()
m = int(input())
weights = []
for i in range(1, 11):
if w[i - 1] == "1":
weights.append(i)
weights.sort(reverse=True)
solution = [(0) for x in range(0, m)]
if not solve(0, 0, 0, m, weights, -1, solution):
print("NO")
|
IMPORT FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(10**5)
string = input()
m = int(input())
i = 1
allowed_weights = []
for elements in string:
if elements == "1":
allowed_weights.append(i)
i += 1
else:
i += 1
answer = []
sum_1 = 0
sum_2 = 0
counter = 0
def list_printer(answer, z):
if len(answer) == z:
answer_st = ""
for elements in answer:
answer_st += str(elements) + " "
print(answer_st)
else:
answer.pop()
answer_st = ""
for elements in answer:
answer_st += str(elements) + " "
print(answer_st)
def backtracking(answer, allowed_weights, m, sum_1, sum_2, counter, z):
if len(allowed_weights) == 0:
if m == 0:
print("YES")
return True
else:
return False
if len(allowed_weights) == 1:
if m == 1:
answer.append(allowed_weights[0])
print("YES")
print(answer[0])
return True
else:
return False
if m == 1:
if counter % 2 == 0:
for weights in allowed_weights:
if weights == answer[-1]:
continue
elif weights > sum_2 - sum_1:
answer.append(weights)
print("YES")
list_printer(answer, z)
return True
else:
continue
return False
if counter % 2 == 1:
for weights in allowed_weights:
if weights == answer[-1]:
continue
elif weights > sum_1 - sum_2:
answer.append(weights)
print("YES")
list_printer(answer, z)
return True
else:
continue
return False
for weights in allowed_weights:
if len(answer) == 0:
sum_1 += weights
counter += 1
answer.append(weights)
if backtracking(answer, allowed_weights, m - 1, sum_1, sum_2, counter, z):
return True
answer.pop()
sum_1 -= weights
counter -= 1
continue
elif weights == answer[-1]:
continue
elif counter % 2 == 0:
if sum_2 - sum_1 < weights:
answer.append(weights)
sum_1 += weights
counter += 1
if backtracking(
answer, allowed_weights, m - 1, sum_1, sum_2, counter, z
):
return True
sum_1 -= weights
counter -= 1
answer.pop()
continue
elif sum_1 - sum_2 < weights:
answer.append(weights)
sum_2 += weights
counter += 1
if backtracking(answer, allowed_weights, m - 1, sum_1, sum_2, counter, z):
return True
sum_2 -= weights
counter -= 1
answer.pop()
continue
return False
if not backtracking(answer, allowed_weights, m, sum_1, sum_2, counter, m):
print("NO")
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
w = input()
weights = []
for i in range(10):
if w[i] == "1":
weights.append(i + 1)
sys.setrecursionlimit(100000)
m = int(input())
stack = [0] * (m + 1)
def dfs(i, j, k):
global m
if k == m:
return True
for weight in weights:
if weight == j or weight <= abs(i):
continue
else:
if (k + 1) % 2 == 0:
result = dfs(i - weight, weight, k + 1)
else:
result = dfs(i + weight, weight, k + 1)
if result:
stack[k + 1] = weight
return True
return False
result = dfs(0, 0, 0)
if result:
print("YES")
print(*stack[1:])
else:
print("NO")
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
R = lambda: map(int, input().split())
ws = [(i + 1) for i, x in enumerate(map(int, input())) if x]
m = int(input())
acc = [0, 0]
seq = []
idx = 0
def dfs(acc, seq, idx):
if len(seq) == m:
print("YES")
print(" ".join(map(str, seq)))
return 1
for w in ws:
if (not seq or w != seq[-1]) and acc[idx] + w > acc[1 - idx]:
acc[idx] += w
seq.append(w)
idx = 1 - idx
if not dfs(acc, seq, idx):
idx = 1 - idx
acc[idx] -= w
seq.pop(-1)
else:
return 1
if not dfs(acc, seq, idx):
print("NO")
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def main():
ww, res = [i for i, c in enumerate(input(), 1) if c == "1"], []
ww = [[w for w in ww if w > i] for i in range(11)]
nxt = {(0, 0): ()}
for _ in range(int(input()) + 1):
if not nxt:
print("NO")
return
cur, nxt = nxt, {}
for (u, d), way in cur.items():
newway = way + (u,)
for v in ww[d]:
if v != u:
nxt[v, v - d] = newway
print("YES")
for (u, d), way in cur.items():
print(*(way[1:] + (u,)))
return
main()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING LIST ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR DICT FOR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(10000)
k = []
ans = [-1]
done = False
def solve(m, diff=0):
global done, ans, k
if m == 0:
done = True
return
for i in k:
if diff < i and i != ans[-1]:
ans.append(i)
solve(m - 1, i - diff)
if done:
return
ans.pop()
k = input()
k = [(i + 1) for i in range(len(k)) if int(k[i]) == 1]
m = int(input())
sm = [0, 0]
solve(m)
if len(ans) == m + 1:
print("YES")
for i in range(1, len(ans)):
print(ans[i], end=" ")
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN FOR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR RETURN EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(100000)
l = list(input())
m = int(input())
ans = []
def fun(step, balance, weight):
if step == m:
return 1
if balance > 0:
for i in range(len(l)):
if i + 1 == weight:
continue
if l[i] == "1" and balance - i - 1 < 0:
ans.append(i + 1)
if fun(step + 1, balance - i - 1, i + 1):
return 1
ans.pop()
return 0
if balance < 0:
for i in range(len(l)):
if i + 1 == weight:
continue
if l[i] == "1" and balance + i + 1 > 0:
ans.append(i + 1)
if fun(step + 1, balance + i + 1, i + 1):
return 1
ans.pop()
return 0
if balance == 0:
for i in range(len(l)):
if l[i] == "1":
ans.append(i + 1)
if fun(step + 1, balance + i + 1, i + 1):
return 1
ans.pop()
return 0
if fun(0, 0, 0):
print("YES")
print(*ans)
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
ans = []
def dfs(bal, m, last, flag):
x = bal
if m == te:
return 1
for i in w:
if i == last:
continue
if flag:
x += i
else:
x -= i
if (x > 0 and flag or x < 0 and not flag) and dfs(x, m + 1, i, not flag):
ans.append(i)
return 1
if flag:
x -= i
else:
x += i
return 0
s = input()
te = int(input())
w = []
for i in range(len(s)):
if s[i] == "1":
w.append(i + 1)
dfs(0, 0, 0, 1)
if ans:
print("YES")
print(*ans[::-1])
else:
print("NO")
|
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def run():
ws = [i for i, j in enumerate(input(), 1) if j == "1"]
m = int(input())
q = [(-1, 0, 0, [])]
while len(q) != 0:
previous, need, index, lst = q.pop()
if index == m:
print("YES")
print(" ".join(list(map(str, lst))))
return
for w in ws:
if w > need and w != previous:
q.append((w, w - need, index + 1, lst + [w]))
print("NO")
return
run()
|
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(95000)
def solve(m, arr, bag1, bag2, flag, last, ans):
if m == 0:
return True, ans
for ele in arr:
if flag == False:
if ele + bag1 > bag2 and ele != last:
ans.append(ele)
temp, result = solve(m - 1, arr, bag1 + ele, bag2, True, ele, ans)
if temp:
return temp, result
ans.pop()
elif flag == True:
if ele + bag2 > bag1 and ele != last:
ans.append(ele)
temp, result = solve(m - 1, arr, bag1, bag2 + ele, False, ele, ans)
if temp:
return temp, result
ans.pop()
return False, []
s = input()
m = int(input())
arr = []
for i in range(len(s)):
if s[i] == "1":
arr.append(i + 1)
f, result = solve(m, arr, 0, 0, False, 0, [])
if f:
print("YES")
print(*result)
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER VAR FOR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR RETURN VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR RETURN VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER LIST IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
to_print = []
def dfs(d, ini, s, depth, m):
if depth == m:
return True
else:
for i in range(d + 1, len(s)):
if i != ini and s[i] == "1":
ans = dfs(i - d, i, s, depth + 1, m)
if ans == True:
to_print.append(i)
return True
return False
s = "0" + input()
m = int(input())
ans = dfs(0, 0, s, 0, m)
if len(to_print):
print("YES")
print(*to_print[::-1])
else:
print("NO")
|
ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(100000)
f = sys.stdin
w = f.readline().strip()
m = int(f.readline())
weight = []
for i in range(len(w)):
if w[i] == "1":
weight.append(i + 1)
def solve(arr=[], last=-1, delta=0):
if len(arr) == m:
print("YES")
print(*arr)
exit(0)
else:
for i in weight:
if i != last:
if delta >= 0 and delta - i < 0:
solve(arr + [i], i, delta - i)
elif delta < 0 and delta + i > 0:
solve(arr + [i], i, delta + i)
solve()
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF LIST NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(1500)
def dfs(a, w1, w2, s):
if len(a) == m:
return a
if len(a) % 2 == 0:
diff = w2 - w1
for i in range(max(0, diff), 10):
if s[i] == "1":
if a != False:
if len(a) == 0:
kol = dfs(a + [i + 1], w1 + i + 1, w2, s)
if kol:
return kol
elif a[-1] != i + 1:
kol = dfs(a + [i + 1], w1 + i + 1, w2, s)
if kol:
return kol
return False
diff = w1 - w2
for i in range(max(0, diff), 10):
if s[i] == "1":
if a != False:
if len(a) == 0:
kol = dfs(a + [i + 1], w1, w2 + i + 1, s)
if kol:
return a
elif a[-1] != i + 1:
kol = dfs(a + [i + 1], w1, w2 + i + 1, s)
if kol:
return kol
return False
s = input()
m = int(input())
a = dfs([], 0, 0, s)
if a:
print("YES")
print(*a)
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR RETURN VAR IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR RETURN VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR RETURN VAR IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(2000)
n = input()
m = int(input())
arr = [(i + 1) for i in range(10) if n[i] == "1"]
if len(arr) < 1:
print("NO")
exit(0)
a = []
def dfs(x, y, k):
if k > m:
return 1
f = 0
for i in arr:
if x >= 0 and y != i and x - i < 0:
f = dfs(x - i, i, k + 1)
elif x < 0 and y != i and x + i > 0:
f = dfs(x + i, i, k + 1)
if f == 1:
a.append(i)
return 1
return 0
result = dfs(0, 0, 1)
if result == 1:
print("YES")
for i in a[::-1]:
print(i, end=" ")
else:
print("NO")
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
A = []
for i in range(1, 11):
if s[i - 1] == "1":
A.append(i)
A.sort()
n = len(A)
m = int(input())
e = m
if m <= n:
print("YES")
for i in range(m - 1):
print(A[i], end=" ")
print(A[m - 1])
elif n == 0:
print("NO")
else:
for q in range(n):
m = e
Z = []
ind = q
summc = 0
summo = 0
r = 0
past = -1
while m != 0:
if ind >= n:
ind = 0
if past == ind:
ind += 1
r += 1
if ind >= n:
Ans = "NO"
break
if summc + A[ind] > summo:
m -= 1
summc += A[ind]
Z.append(A[ind])
summo, summc = summc, summo
past = ind + 0
ind = 0
continue
ind += 1
if m == 0:
Ans = "YES"
print("YES")
for i in range(e - 1):
print(Z[i], end=" ")
print(Z[e - 1])
break
if Ans == "NO":
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR STRING IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
weights_bin = input()
weights = [(i + 1) for i, w in enumerate(weights_bin) if int(w)]
m = int(input())
stack = [(-1, 0, 0, [])]
while len(stack):
u, wb, step, path = stack.pop()
if step == m:
print("YES")
print(" ".join(map(str, path)))
exit()
for w in weights:
if w != u and w > wb:
stack.append((w, w - wb, step + 1, path + [w]))
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def chooseNext(m1, m2, w, prev):
d = m2 - m1
for i in w[1:]:
if i > d and i != prev:
return i
else:
return 0
w = input()
weights = [0]
for i in range(len(w)):
if w[i] != "0":
weights.append(i + 1)
n = len(weights) - 2
m = int(input())
if not n and m > 1:
print("NO")
else:
for base in weights:
Ml = Mr = 0
result = []
prev = base
for i in range(m):
if i % 2:
cur = chooseNext(Ml, Mr, weights, prev)
if cur:
Ml += cur
else:
break
else:
cur = chooseNext(Mr, Ml, weights, prev)
if cur:
Mr += cur
else:
break
result.append(cur)
prev = cur
else:
print("YES")
print(" ".join(str(x) for x in result))
break
else:
print("NO")
|
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
good = False
s = input()
n = int(input())
ans = []
possible = []
sys.setrecursionlimit(1500)
def bkt(suma1, suma2, cnt, last):
global good
if good:
return
if cnt == n:
good = True
print("YES")
for i in ans:
print(i, end=" ", flush=False)
return
for weight in possible:
if cnt % 2 == 0 and suma1 + weight > suma2 and weight != last:
ans.append(weight)
bkt(suma1 + weight, suma2, cnt + 1, weight)
ans.pop()
elif cnt % 2 == 1 and suma2 + weight > suma1 and weight != last:
ans.append(weight)
bkt(suma1, suma2 + weight, cnt + 1, weight)
ans.pop()
for i in range(0, len(s)):
if s[i] == "1":
possible.append(i + 1)
bkt(0, 0, 0, -1)
if not good:
print("NO")
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING NUMBER RETURN FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
s = input()
m = [int(input())]
nodes = [(i + 1) for i in range(10) if s[i] == "1"]
ans = []
def dfs(w1, w2, step, current):
if ans:
return
if step == m[0]:
ans.append(current)
return
for child in nodes:
if current and child == current[-1]:
continue
if step % 2 == 0 and w1 + child > w2:
dfs(w1 + child, w2, step + 1, current + [child])
elif step % 2 == 1 and w2 + child > w1:
dfs(w1, w2 + child, step + 1, current + [child])
dfs(0, 0, 0, [])
if ans:
print("YES")
print(*ans[0])
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER LIST IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
def go(i, d, last):
if i > 0 and d <= 0 or d > 10:
return 0
if i == m:
print("YES")
print(" ".join(str(elem) for elem in x[:m]))
return 1
if was[i][d][last]:
return 0
was[i][d][last] = 1
for j in range(10):
if s[j] == "1" and j != last:
x[i] = j + 1
if go(i + 1, j + 1 - d, j):
return 1
return 0
s = list(input())
m = int(input())
was = [[[(0) for a in range(11)] for b in range(11)] for c in range(1000)]
x = [0] * 1000
if not go(0, 0, 10):
print("NO")
|
FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
input = sys.stdin.readline
I = lambda: list(map(int, input().split()))
s = input().strip()
(m,) = I()
l = [(i + 1) for i in range(10) if s[i] == "1"]
n = len(l)
q = [[0, 0, 0, []]]
while q:
x, cb, mv, p = q.pop()
if mv == m:
print("YES")
print(*p)
exit()
for i in range(n):
if l[i] != x and l[i] > cb:
q.append([l[i], l[i] - cb, mv + 1, p + [l[i]]])
print("NO")
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR STRING
|
Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.
Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.
You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.
-----Input-----
The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).
-----Output-----
In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales.
If there are multiple solutions, you can print any of them.
-----Examples-----
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
|
import sys
sys.setrecursionlimit(1500)
ans = []
def main():
w = [int(i) for i in input()]
m = int(input())
dfs(w, 0, 0, 1, m)
print("YES\n" + " ".join([str(i) for i in ans][::-1]) if len(ans) > 0 else "NO")
def dfs(w, x, y, k, m):
if k > m:
return 1
f = 0
for i in range(1, 11):
if w[i - 1] and x >= 0 and y != i and x - i < 0:
f = dfs(w, x - i, i, k + 1, m)
elif w[i - 1] and x < 0 and y != i and x + i > 0:
f = dfs(w, x + i, i, k + 1, m)
if f:
ans.append(i)
return 1
return 0
main()
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER STRING FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
a, t = input(), input() + " "
b = a[::-1]
s, p = "", []
for q in t:
d = s
s += q
if s in a or s in b:
continue
if d in a:
k = a.find(d)
p += [(k + 1, k + len(d))]
elif d in b:
k = b.rfind(d)
p += [(len(a) - k, len(a) - k - len(d) + 1)]
else:
print(-1)
exit()
s = q
print(len(p))
for i, j in p:
print(i, j)
|
ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s = input()
t = input()
rs = s[::-1]
a = t[0]
t += "."
li = []
for i in range(1, len(t)):
b = a + t[i]
if b in s or b in rs:
a = b
else:
if a in s:
li.append([s.find(a) + 1, s.find(a) + len(a)])
elif a in rs:
li.append([len(s) - rs.find(a), len(s) - rs.find(a) - len(a) + 1])
else:
print(-1), exit(0)
a = t[i]
print(len(li))
for i in li:
print(i[0], i[1])
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s = input()
t = input() + "."
rs, a, res = s[::-1], t[0], []
for i in range(1, len(t)):
b = a + t[i]
if b in s or b in rs:
a = b
continue
if a in s:
x = s.find(a)
res.append([x + 1, x + len(a)])
elif a in rs:
x = rs.find(a)
res.append([len(s) - x, len(s) - x - len(a) + 1])
else:
print(-1)
exit()
a = t[i]
print(len(res))
for i in res:
print(i[0], i[1])
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
import sys
s = input()
s_reverse = s[::-1]
t = input()
first = []
second = []
pos = 0
def find_str(st, char):
index = 0
if char in st:
c = char[0]
for ch in st:
if ch == c:
if st[index : index + len(char)] == char:
return index
index += 1
return -1
while pos < len(t):
substr = t[pos : pos + 1]
if substr not in s and substr not in s_reverse:
print("-1")
sys.exit()
step = 1
while substr in s or substr in s_reverse:
step += 1
substr = t[pos : pos + step]
if pos + step > len(t):
break
step -= 1
substr = t[pos : pos + step]
start = find_str(s, substr)
if start == -1:
start = find_str(s_reverse, substr)
first.append(len(s) - start)
second.append(len(s) - (start + step - 1))
else:
first.append(start + 1)
second.append(start + step)
pos = pos + step
print(len(first))
for a, b in zip(first, second):
print("%d %d" % (a, b))
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s1 = input()
s = input() + "."
s2 = s1[::-1]
l = len(s)
ll = len(s1)
a = ""
lis = []
for i in range(l):
v = a + s[i]
if v in s1 or v in s2:
a = a + s[i]
else:
if a in s1:
k = s1.index(a) + 1
lis.append([k, k + len(a) - 1])
elif a in s2:
k = s2.index(a)
lis.append([ll - k, ll - k - (len(a) - 1)])
else:
print(-1)
exit()
a = s[i]
print(len(lis))
for i in lis:
print(*i)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
def find_max_substr(t, s):
l, r = 0, len(t)
while l != r:
m = (l + r) // 2
if t[: m + 1] in s:
l = m + 1
else:
r = m
l1 = l
rs = s[::-1]
l, r = 0, len(t)
while l != r:
m = (l + r) // 2
if t[: m + 1] in rs:
l = m + 1
else:
r = m
l2 = l
if l1 >= l2:
return s.find(t[:l1]) + 1, s.find(t[:l1]) + l1
else:
return s.find(t[:l2][::-1]) + l2, s.find(t[:l2][::-1]) + 1
s = input()
t = input()
if not set(t).issubset(set(s)):
print(-1)
return
a = []
while t:
l, r = find_max_substr(t, s)
a.append((l, r))
t = t[abs(r - l) + 1 :]
print(len(a))
for l, r in a:
print(l, r)
|
FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
import sys
s = input()
l = len(s)
s_r = s[::-1]
t = input()
lent = len(t)
res = 0
tracks = []
step = 0
while step < lent:
if t[step : step + 1] not in s:
print(-1)
return
plus = 1
while (
t[step : step + plus] in s or t[step : step + plus] in s_r
) and step + plus < lent + 1:
plus += 1
if t[step : step + plus - 1] in s:
x = s.find(t[step : step + plus - 1])
tracks.append((x + 1, x + plus - 1))
else:
x = s_r.find(t[step : step + plus - 1])
tracks.append((l - x, l - x - plus + 2))
res += 1
step += plus - 1
print(res)
for a, b in tracks:
print(a, b)
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s = input()
t = input()
revS = s[::-1]
n = len(s)
cur = 1
start = -1
end = -1
revFlag = 0
errFlag = 0
i = 0
res = []
while i < len(t):
if (
s.find(t[i : i + cur]) != -1
and i + cur <= len(t)
and s.find(t[i : i + cur]) + cur <= n
):
start = s.find(t[i : i + cur]) + 1
end = start + cur - 1
cur += 1
elif (
revS.find(t[i : i + cur]) != -1
and i + cur <= len(t)
and revS.find(t[i : i + cur]) + cur <= n
):
start = n - revS.find(t[i : i + cur])
end = start - cur + 1
cur += 1
else:
if (start == -1 and end == -1) and (
s.find(t[i : i + cur]) == -1 and revS.find(t[i : i + cur]) == -1
):
errFlag = 1
break
i += cur - 1
cur = 1
res.append(str(start) + " " + str(end))
start = -1
end = -1
if errFlag != 1:
print(len(res))
for p in res:
print(p)
else:
print(-1)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s = input()
t = input()
len_s = len(s)
reversed_s = s[::-1]
len_t = len(t)
res = 0
pairs = []
indx = 0
while indx < len_t:
if t[indx : indx + 1] not in s:
print(-1)
exit()
i = 1
while (
t[indx : indx + i] in s or t[indx : indx + i] in reversed_s
) and indx + i < len_t + 1:
i += 1
prev_i = i - 1
if t[indx : indx + prev_i] in s:
x = s.find(t[indx : indx + prev_i])
pairs.append((x + 1, x + prev_i))
else:
x = reversed_s.find(t[indx : indx + prev_i])
pairs.append((len_s - x, len_s - x - i + 2))
res += 1
indx += prev_i
print(res)
for a, b in pairs:
print(a, b)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
def solve(s, t):
hash_s = [False] * 256
hash_t = [False] * 256
arr = []
n = len(s)
for c in s:
hash_s[ord(c)] = True
for c in t:
hash_t[ord(c)] = True
for i in range(256):
if not hash_s[i] and hash_t[i]:
print(-1)
return
rev = s[::-1]
i, j = 0, 0
while i < len(t):
flag = True
temp = t[i]
j = i + 1
while j < len(t):
temp += t[j]
if temp not in s and temp not in rev:
flag = False
break
j += 1
if flag:
x = s.find(temp)
if x != -1:
arr.append((x + 1, x + len(temp)))
else:
y = rev.find(temp)
arr.append((n - y, n - y - len(temp) + 1))
else:
x = s.find(temp[:-1])
if x != -1:
arr.append((x + 1, x + len(temp) - 1))
else:
x = rev.find(temp[:-1])
arr.append((n - x, n - x - len(temp) + 2))
i = j
print(len(arr))
for x, y in arr:
print(x, y)
s = input()
t = input()
solve(s, t)
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
-----Input-----
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
-----Output-----
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers x_{i} and y_{i} — numbers of ending blocks in the corresponding piece. If x_{i} ≤ y_{i} then this piece is used in the regular order, and if x_{i} > y_{i} piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
-----Examples-----
Input
abc
cbaabc
Output
2
3 1
1 3
Input
aaabrytaaa
ayrat
Output
3
1 1
6 5
8 7
Input
ami
no
Output
-1
-----Note-----
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
|
s, t = input(), input()
p, d = [], [[] for i in range(26)]
for i, q in enumerate(s):
d[ord(q) - 97].append(i)
i, n = 0, len(t)
s += "+"
t += "-"
while i < n:
q = t[i]
a = b = c = 0
for j in d[ord(q) - 97]:
k = 1
while t[i + k] == s[j + k]:
k += 1
if k > a:
a, b, c = k, j + 1, 1
k = 1
while t[i + k] == s[j - k]:
k += 1
if k > a:
a, b, c = k, j + 1, -1
if not a:
print(-1)
return
i += a
p.append((b, b + c * a - c))
print(len(p))
for i, j in p:
print(i, j)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR STRING VAR STRING WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
s = [input() for i in range(2)]
n = len(s[0])
def divconq(a, b):
if a + 1 >= b:
return 0
for i in range(a, b):
sq1 = [s[0][i], s[1][i]]
if sq1 == ["X", "X"]:
return divconq(a, i) + divconq(i + 1, b)
for i in range(a, b - 1):
sq2 = [s[0][i], s[0][i + 1], s[1][i], s[1][i + 1]]
if sq2 == ["X", "X", "0", "0"] or sq2 == ["0", "0", "X", "X"]:
return divconq(a, i + 1) + divconq(i + 1, b)
if sq2 == ["X", "0", "0", "X"] or sq2 == ["0", "X", "X", "0"]:
return divconq(a, i + 1) + divconq(i + 1, b)
for i in range(a, b):
sq1 = [s[0][i], s[1][i]]
if sq1 == ["X", "0"] or sq1 == ["0", "X"]:
if i == a:
return 1 + divconq(i + 2, b)
if i == b - 1:
return divconq(a, i - 1) + 1
return max(
divconq(a, i - 1) + 1 + divconq(i + 1, b),
divconq(a, i) + 1 + divconq(i + 2, b),
)
return (b - a) // 3 * 2 + ((b - a) % 3 == 2)
print(divconq(0, n))
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER VAR IF VAR LIST STRING STRING RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR LIST STRING STRING STRING STRING VAR LIST STRING STRING STRING STRING RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR LIST STRING STRING STRING STRING VAR LIST STRING STRING STRING STRING RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER VAR IF VAR LIST STRING STRING VAR LIST STRING STRING IF VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
a = list(input())
b = list(input())
c = []
for i in range(len(a)):
c.append(int(a[i] == "0") + int(b[i] == "0"))
d = 0
for i in range(len(a) - 1):
if c[i] == 2 and c[i + 1] == 2:
c[i] = 0
c[i + 1] = 1
d = d + 1
elif c[i] == 1 and c[i + 1] == 2:
c[i] = 0
c[i + 1] = 0
d = d + 1
if c[i] == 2 and c[i + 1] == 1:
c[i] = 0
c[i + 1] = 0
d = d + 1
print(d)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
board = []
board.append(list(input()))
board.append(list(input()))
n = len(board[0])
best = 0
best_f_under = 0
best_f_over = 0
best_f_both = 0
for i in range(1, n):
a_1 = 0
a_2 = 0
a_3 = 0
a_4 = 0
if board[0][i - 1] == "0" and board[1][i - 1] == "0":
if board[0][i] == "0":
a_1 = 1
if board[1][i] == "0":
a_4 = 1
if board[0][i] == "0" and board[1][i] == "0":
if board[0][i - 1] == "0":
a_2 = 1
if board[1][i - 1] == "0":
a_3 = 1
best_temp = max(a_2 + best_f_over, a_3 + best_f_under)
best_f_under_temp = a_1 + best_f_both
best_f_over_temp = a_4 + best_f_both
best_f_both = max(best_f_under, best_f_over, best, best_f_both)
best = best_temp
best_f_under = max(best_f_under_temp, best_f_both)
best_f_over = max(best_f_over_temp, best_f_both)
print(max(best, best_f_over, best_f_under, best_f_both))
|
ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
s1 = input()
s2 = input()
counter = 0
statemp = 0
if s1[0] == "0" and s2[0] == "0":
statemp = 2
elif s1[0] == "X" and s2[0] == "X":
statemp = 0
else:
statemp = 1
for i in range(1, len(s1)):
f = 0
if s1[i] == "0" and s2[i] == "0":
if statemp == 2:
counter += 1
f = 1
elif statemp == 1:
counter += 1
f = 0
else:
f = 2
elif s1[i] == "X" and s2[i] == "X":
f = 0
elif statemp == 2:
counter += 1
f = 0
else:
f = 1
statemp = f
print(counter)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
sa = input()
sb = input()
a = []
b = []
k = 0
for i in range(0, len(sa)):
a.append(sa[i])
for i in range(0, len(sb)):
b.append(sb[i])
for i in range(0, len(sa) - 1):
if a[i] == "0" and a[i + 1] == "0":
if b[i] == "0":
k += 1
a[i] = a[i + 1] = "X"
b[i] = "X"
elif b[i + 1] == "0":
k += 1
a[i] = a[i + 1] = "X"
b[i + 1] = "X"
elif b[i] == "0" and b[i + 1] == "0":
if a[i] == "0":
k += 1
b[i] = b[i + 1] = "X"
a[i] = "X"
elif a[i + 1] == "0":
k += 1
b[i] = b[i + 1] = "X"
a[i + 1] = "X"
print(k)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
line1 = input()
line2 = input()
boards = [["" for x in range(len(line1))] for y in range(2)]
for i in range(len(line1)):
boards[0][i] = line1[i]
boards[1][i] = line2[i]
count = 0
for x in range(len(line1) - 1):
if 3 <= boards[0][x : x + 2].count("0") + boards[1][x : x + 2].count("0"):
count += 1
if boards[0][x] == "0":
if boards[0][x + 1] == "0":
boards[0][x] = "X"
boards[0][x + 1] = "X"
if boards[1][x] == "0":
boards[1][x] = "X"
else:
boards[1][x + 1] = "X"
else:
boards[0][x] = "X"
boards[1][x] = "X"
boards[1][x + 1] = "X"
else:
boards[0][x + 1] = "X"
boards[1][x] = "X"
boards[1][x + 1] = "X"
print(count)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR STRING IF VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING IF VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
s = input()
t = input()
dp = []
n = len(s)
a = []
b = []
for i in range(n + 1):
dp.append((0, 0))
for i in range(n):
a.append(s[i])
b.append(t[i])
ans = 0
for i in range(n):
if a[i] == "0" and b[i] == "0":
if i > 0 and (a[i - 1] == "0" or b[i - 1] == "0"):
if a[i - 1] == "0":
a[i] = b[i] = a[i - 1] = "X"
ans += 1
elif b[i - 1] == "0":
a[i] = b[i] = b[i - 1] = "X"
ans += 1
elif i + 1 < n:
if a[i + 1] == "0":
a[i] = b[i] = a[i + 1] = "X"
ans += 1
elif b[i + 1] == "0":
a[i] = b[i] = b[i + 1] = "X"
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
inp1 = input()
inp2 = input()
free_cells = [x.count("0") for x in zip(inp1, inp2)]
count = 0
empty = 0
for cell in free_cells:
empty += cell
if empty >= 3:
empty -= 3
count += 1
else:
empty = cell
print(count)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.
-----Input-----
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
-----Output-----
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
-----Examples-----
Input
00
00
Output
1
Input
00X00X0XXX0
0XXX0X00X00
Output
4
Input
0X0X0
0X0X0
Output
0
Input
0XXX0
00000
Output
2
|
def solve(arr):
placed = 0
for i in range(len(arr[0]) - 1):
if arr[1][i] == "0" and arr[0][i] == "0" and arr[1][i + 1] == "0":
arr[1][i] = "X"
arr[0][i] = "X"
arr[1][i + 1] = "X"
placed += 1
elif arr[1][i] == "0" and arr[0][i] == "0" and arr[0][i + 1] == "0":
arr[1][i] = "X"
arr[0][i] = "X"
arr[0][i + 1] = "X"
placed += 1
elif arr[0][i] == "0" and arr[0][i + 1] == "0" and arr[1][i + 1] == "0":
arr[0][i] = "X"
arr[0][i + 1] = "X"
arr[1][i + 1] = "X"
placed += 1
elif arr[1][i] == "0" and arr[0][i + 1] == "0" and arr[1][i + 1] == "0":
arr[1][i] = "X"
arr[0][i + 1] = "X"
arr[1][i + 1] = "X"
placed += 1
return placed
s1 = input().strip()
s2 = input().strip()
print(solve([[i for i in s1], [i for i in s2]]))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.