根据给定的编码技术从结果字符串重建原始字符串
长度为N的二进制字符串S由N个字符的字符串P和整数X构成。 S的第i个字符的选择如下:
- 如果字符P i- X 存在且等于1,则S i为1
- 如果字符P i+ X存在且等于 1,则S i为 1
- 如果上述两个条件都为假,则S i为 0。
给定结果字符串S和整数X ,重构原始字符串P 。如果没有字符串P可以产生字符串S ,则输出 -1。
例子:
Input: S = “10011”, X = 2
Output: “01100”
Explanation: The input string S = “10011” can be constructed from the output string P = “01100”.
Input: S = “11101100111”, X = 3
Output: -1
Explanation: The input string S = “11101100111” cannot be constructed from any output string P.
方法:这个任务可以通过一个全为1的辅助字符串来解决。请按照以下步骤解决问题:
- 将字符串P初始化为所有字符为 '1'。
- 现在使用循环遍历字符串S并根据给定条件在P中的正确位置放置零:
- 如果S [i]等于'0'且P [i- X ]存在,i,e,(i- X )>=0,则令P [i- X ]='0'。
- 如果S [i] 等于 '0' 且P [i+ X ] 存在,即 (i+ X )< N ,则令P [i+ X ] = '0'。
- 初始化一个变量flag = 1 来判断字符串P是否存在。
- 要检查构造的字符串P的正确性,请使用 for 循环遍历字符串S :
- 如果S [i]=='1' 并且P [i- X ] 或P [i+ X ] 存在并且等于'1',那么字符串P到目前为止是正确的并且可以继续遍历。
- 如果S [i]=='1' 且P [i- X ] 或P [i+ X ] 不存在或不等于 1,则设置flag = 0,输出 -1 并退出循环。
- 如果遍历字符串S后flag等于 0,则输出原始字符串P。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the original string P
void findString(string S, int X)
{
// Stores the size of string S
int N = S.size();
// Each character of string
// P is set to '1'
string P(N, '1');
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S[i] == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S[i] == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0
&& P[i - X] == '1')
|| (i + X < N
&& P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
cout << -1;
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
cout << P;
}
}
// Driver Code
int main()
{
// Given input
string S = "10011";
int X = 2;
// Function Call
findString(S, X);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the original string P
static void findString(String S, int X)
{
// Stores the size of string S
int N = S.length();
// Each character of string
// converted to char Array P
// is set to '1'
char[] P = new char[N];
for (int i = 0; i < N; ++i) {
P[i] = '1';
}
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S.charAt(i) == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S.charAt(i) == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0 && P[i - X] == '1')
|| (i + X < N && P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
System.out.print(-1);
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
String p = new String(P);
System.out.print(p);
}
}
// Driver Code
public static void main(String args[])
{
// Given input
String S = "10011";
int X = 2;
// Function Call
findString(S, X);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to find the original string P
def findString(S, X):
# Stores the size of string S
N = len(S)
# Each character of string
# P is set to '1'
P = ['1'] * N
# Loop to put zeroes at
# the correct positions in P
for i in range(N):
# If the character is '0'
if (S[i] == '0'):
# If P[i-X] exists
if (i - X >= 0):
# Set P[i-X] to '0'
P[i - X] = '0';
# If P[i+X] exists
if (i + X < N):
# Set P[i+X] to '0'
P[i + X] = '0';
# Set flag to 1
flag = 1;
# Loop to cross check
# if P exists or not
for i in range(N):
# If the character is '1'
if (S[i] == '1'):
# If P[i-X] exists and
# is equal to '1' or if
# P[i+X] exists and
# is equal to '1'
if ((i - X >= 0 and P[i - X] == '1') or (i + X < N and P[i + X] == '1')):
continue;
else:
# Set flag to 0
flag = 0;
# Output -1
print(-1);
# Break from the loop
break;
# If flag is equal to 1
if (flag == 1):
# Output string P
print("".join(P));
# Driver Code
# Given input
S = "10011";
X = 2;
# Function Call
findString(S, X);
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to find the original string P
static void findString(string S, int X)
{
// Stores the size of string S
int N = S.Length;
// Each character of string
// converted to char Array P
// is set to '1'
char[] P = new char[N];
for (int i = 0; i < N; ++i) {
P[i] = '1';
}
// Loop to put zeroes at
// the correct positions in P
for (int i = 0; i < N; ++i) {
// If the character is '0'
if (S[i] == '0') {
// If P[i-X] exists
if (i - X >= 0) {
// Set P[i-X] to '0'
P[i - X] = '0';
}
// If P[i+X] exists
if (i + X < N) {
// Set P[i+X] to '0'
P[i + X] = '0';
}
}
}
// Set flag to 1
int flag = 1;
// Loop to cross check
// if P exists or not
for (int i = 0; i < N; ++i) {
// If the character is '1'
if (S[i] == '1') {
// If P[i-X] exists and
// is equal to '1' or if
// P[i+X] exists and
// is equal to '1'
if ((i - X >= 0 && P[i - X] == '1')
|| (i + X < N && P[i + X] == '1')) {
continue;
}
else {
// Set flag to 0
flag = 0;
// Output -1
Console.Write(-1);
// Break from the loop
break;
}
}
}
// If flag is equal to 1
if (flag == 1) {
// Output string P
string p = new string(P);
Console.Write(p);
}
}
// Driver Code
public static void Main()
{
// Given input
string S = "10011";
int X = 2;
// Function Call
findString(S, X);
}
}
// This code is contributed by Taranpreet
Javascript
输出:
01100
时间复杂度: O(N)
辅助空间: O(N)