📌  相关文章
📜  每个右子串的中间字符的字符串,其后依次是左

📅  最后修改于: 2021-04-23 17:02:26             🧑  作者: 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".

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

此方法的详细步骤如下:

  • 初始化开始= 0,结束= N -1,表示字符串的第一个和最后一个字符。
  • 在字符串的中间打印字符,即mid =(start + end)/ 2。
  • 递归遍历其右子串(开始=中+1,结束),然后遍历其左子串(开始,中– 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


输出:
bcda
utpigy

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