通过在给定范围之间选择跳转值检查是否可以到达给定二进制字符串的结尾
给定两个正整数L和R以及一个大小为N的二进制字符串S ,任务是检查是否通过一系列索引跳转从索引0到达字符串的末尾,比如i使得S[i]是在[L, R]范围内允许0次跳跃。如果可以到达,则打印Yes 。否则,打印No 。
例子:
Input: S = “011010”, L = 2, R = 3
Output: Yes
Explanation:
Following are the series of moves having characters at that indices as 0:
S[0](= 0) -> S[3](= 0) -> S[5](= 0) and S[5] is the end of the string S.
Therefore, print Yes.
Input: S = “01101110”, L = 2, R = 3
Output: No
方法:上述问题可以借助动态规划来解决,其思想是维护一维数组,比如dp[]其中dp[i]将存储到达第 i个位置的可能性并相应地更新每个索引。以下是步骤:
- 初始化一个数组dp[]以便dp[i]存储是否可以从索引0到达任何索引i 。将dp[0]的值更新为1 ,因为它是当前常设索引。
- 初始化一个变量, pre为0 ,用于存储可以访问当前索引的索引数。
- 迭代范围[1, N)并更新pre变量的值,如下所示:
- 如果 ( i >= minJump ) 的值,则将pre的值增加dp[i – minJump] 。
- 如果 ( i > maxJump ) 的值,则将pre的值递减dp[i – maxJump – 1] 。
- 如果pre的值为正,则 至少有 1 个索引可以访问当前索引。因此,如果S[i] = 0的值,则更新dp[i] = 1的值。
- 完成上述步骤后,如果dp[N – 1]的值为正,则打印Yes 。否则,打印No 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if it is possible
// to reach the end of the binary string
// using the given jumps
bool canReach(string s, int L, int R)
{
// Stores the DP states
vector dp(s.length());
// Initial state
dp[0] = 1;
// Stores count of indices from which
// it is possible to reach index i
int pre = 0;
// Traverse the given string
for (int i = 1; i < s.length(); i++) {
// Update the values of pre
// accordingly
if (i >= L) {
pre += dp[i - L];
}
// If the jump size is out of
// the range [L, R]
if (i > R) {
pre -= dp[i - R - 1];
}
dp[i] = (pre > 0) and (s[i] == '0');
}
// Return answer
return dp[s.length() - 1];
}
// Driver Code
int main()
{
string S = "01101110";
int L = 2, R = 3;
cout << (canReach(S, L, R) ? "Yes" : "No");
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to check if it is possible
// to reach the end of the binary string
// using the given jumps
static int canReach(String s, int L, int R)
{
// Stores the DP states
int dp[] = new int[s.length()];
// Initial state
dp[0] = 1;
// Stores count of indices from which
// it is possible to reach index i
int pre = 0;
// Traverse the given string
for (int i = 1; i < s.length(); i++) {
// Update the values of pre
// accordingly
if (i >= L) {
pre += dp[i - L];
}
// If the jump size is out of
// the range [L, R]
if (i > R) {
pre -= dp[i - R - 1];
}
if (pre > 0 && s.charAt(i) == '0')
dp[i] = 1;
else
dp[i] = 0;
}
// Return answer
return dp[s.length() - 1];
}
// Driver Code
public static void main (String[] args)
{
String S = "01101110";
int L = 2, R = 3;
if (canReach(S, L, R) == 1)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to check if it is possible
# to reach the end of the binary string
# using the given jumps
def canReach(s, L, R):
# Stores the DP states
dp = [0 for _ in range(len(s))]
# Initial state
dp[0] = 1
# Stores count of indices from which
# it is possible to reach index i
pre = 0
# Traverse the given string
for i in range(1, len(s)):
# Update the values of pre
# accordingly
if (i >= L):
pre += dp[i - L]
# If the jump size is out of
# the range [L, R]
if (i > R):
pre -= dp[i - R - 1]
dp[i] = (pre > 0) and (s[i] == '0')
# Return answer
return dp[len(s) - 1]
# Driver Code
if __name__ == "__main__":
S = "01101110"
L = 2
R = 3
if canReach(S, L, R):
print("Yes")
else:
print("No")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if it is possible
// to reach the end of the binary string
// using the given jumps
static int canReach(String s, int L, int R)
{
// Stores the DP states
int[] dp = new int[s.Length];
// Initial state
dp[0] = 1;
// Stores count of indices from which
// it is possible to reach index i
int pre = 0;
// Traverse the given string
for (int i = 1; i < s.Length; i++)
{
// Update the values of pre
// accordingly
if (i >= L)
{
pre += dp[i - L];
}
// If the jump size is out of
// the range [L, R]
if (i > R)
{
pre -= dp[i - R - 1];
}
if (pre > 0 && s[i] == '0')
dp[i] = 1;
else
dp[i] = 0;
}
// Return answer
return dp[s.Length - 1];
}
// Driver Code
public static void Main()
{
String S = "01101110";
int L = 2, R = 3;
if (canReach(S, L, R) == 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Saurabh
Javascript
输出:
No
时间复杂度: O(N)
辅助空间: O(N)