📌  相关文章
📜  由每个右子串的中间字符后跟左顺序形成的字符串

📅  最后修改于: 2021-09-07 02:30:42             🧑  作者: Mango

给定长度为N的字符串str ,任务是使用一组给定的解密规则对其进行解密并打印解密后的字符串。
解密规则如下:

  • 从字符串str的中间字符开始并打印它。
  • 重复遍历右子串并打印其中间字符。
  • 对左子串也重复相同的过程。

例子:

Input: N = 4, str = "abcd" 
Output: bcda
Explanation:
              abcd  ------ b
              / \
             a   cd ------ c
            /     \
           a       d ----- d
          /
         a --------------- a
Hence, the final string is "bcda".

Input: N = 6, str = "gyuitp"
Output: utpigy
Explanation:
            gyuitp ------- u
             / \
           gy  itp ------- t
          /    /\
         gy   i  p ------ p
         /   /
        gy  i ----------- i
        /
       gy --------------- g
       \
        y  -------------- y
Hence, the final string is "utpigy".

方法:
主要思想是使用递归。继续将整个字符串分成左右子串,并打印每个这样子串的中间元素,直到该字符串只剩下一个字符且无法进一步划分为止。
这种方法的详细步骤如下:

  • 初始化 start = 0, end = N -1,表示字符串的第一个和最后一个字符。
  • 在所述字符串的中间,即=中期(开始+端)/ 2打印的字符。
  • 递归遍历它的右子串(start = mid +1, end),然后是它的左子串(start, mid – 1)。
  • 对遍历的每个子串重复上述步骤。继续直到遍历整个字符串并打印给定的字符串。

下面是上述方法的实现:

C++
// C++ implementation of
// the above appraoch
 
#include 
using namespace std;
 
// Function to decrypt and
// print the new string
void decrypt(string Str,
             int Start, int End)
{
    // If the whole string
    // has been traversed
    if (Start > End) {
        return;
    }
 
    // To calculate middle
    // index of the string
    int mid = (Start + End) >> 1;
 
    // Print the character
    // at middle index
    cout << Str[mid];
 
    // Recursively call
    // for right-substring
    decrypt(Str, mid + 1, End);
 
    // Recursive call
    // for left-substring
    decrypt(Str, Start, mid - 1);
}
 
// Driver Code
int main()
{
 
    int N = 4;
    string Str = "abcd";
    decrypt(Str, 0, N - 1);
    cout << "\n";
 
    N = 6;
    Str = "gyuitp";
    decrypt(Str, 0, N - 1);
 
    return 0;
}


Java
// Java implementation of
// the above appraoch
class GFG{
 
// Function to decrypt and
// print the new String
static void decrypt(String Str,
            int Start, int End)
{
    // If the whole String
    // has been traversed
    if (Start > End)
    {
        return;
    }
 
    // To calculate middle
    // index of the String
    int mid = (Start + End) >> 1;
 
    // Print the character
    // at middle index
    System.out.print(Str.charAt(mid));
 
    // Recursively call
    // for right-subString
    decrypt(Str, mid + 1, End);
 
    // Recursive call
    // for left-subString
    decrypt(Str, Start, mid - 1);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
    String Str = "abcd";
    decrypt(Str, 0, N - 1);
    System.out.print("\n");
 
    N = 6;
    Str = "gyuitp";
    decrypt(Str, 0, N - 1);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of
# the above appraoch
 
# Function to decrypt and
# print the new string
def decrypt(Str, Start, End):
 
    # If the whole string
    # has been traversed
    if (Start > End):
        return;
     
    # To calculate middle
    # index of the string
    mid = (Start + End) >> 1;
 
    # Print the character
    # at middle index
    print(Str[mid], end = "");
 
    # Recursively call
    # for right-substring
    decrypt(Str, mid + 1, End);
 
    # Recursive call
    # for left-substring
    decrypt(Str, Start, mid - 1);
 
# Driver Code
N = 4;
Str = "abcd";
decrypt(Str, 0, N - 1);
print();
 
N = 6;
Str = "gyuitp";
decrypt(Str, 0, N - 1);
 
# This code is contributed by Code_Mech


C#
// C# implementation of
// the above appraoch
using System;
class GFG{
 
// Function to decrypt and
// print the new String
static void decrypt(String Str,
            int Start, int End)
{
    // If the whole String
    // has been traversed
    if (Start > End)
    {
        return;
    }
 
    // To calculate middle
    // index of the String
    int mid = (Start + End) >> 1;
 
    // Print the character
    // at middle index
    Console.Write(Str[mid]);
 
    // Recursively call
    // for right-subString
    decrypt(Str, mid + 1, End);
 
    // Recursive call
    // for left-subString
    decrypt(Str, Start, mid - 1);
}
 
// Driver Code
public static void Main()
{
    int N = 4;
    String Str = "abcd";
    decrypt(Str, 0, N - 1);
    Console.Write("\n");
 
    N = 6;
    Str = "gyuitp";
    decrypt(Str, 0, N - 1);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
bcda
utpigy

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live