Decodificar la string codificada con el algoritmo dado

Dada una string decodificada str que se decodificó con el siguiente algoritmo de codificación: 
escriba el carácter central de la string, luego elimínelo y repita el proceso hasta que no queden caracteres. Por ejemplo, “abba” se codificará como “bbaa”
Tenga en cuenta que el carácter del medio es el primer carácter de los dos caracteres del medio cuando la longitud de la string es par.
Ejemplos:

Input: "ofrsgkeeeekgs"
Output: geeksforgeeks

Input: str = "bbaa"
Output: abba

Enfoque: se puede observar que mientras se decodifica la string, la primera letra de la string codificada se convierte en la mediana de la string decodificada. Entonces, primero, escriba el primer carácter de la string codificada y elimínelo de la string codificada, luego comience a agregar el primer carácter de la string codificada primero a la izquierda y luego a la derecha de la string decodificada y realice esta tarea repetidamente hasta que el codificado la string se vuelve vacía.
Por ejemplo: 

Encoded String          Decoded String
ofrsgkeeeekgs           o
frsgkeeeekgs            fo
rsgkeeeekgs             for
sgkeeeekgs              sfor
gkeeeekgs               sforg
keeeekgs                ksforg
eeeekgs                 ksforge
eeekgs                  eksforge
eekgs                   eksforgee
ekgs                    eeksforgee
kgs                     eeksforgeek
gs                      geeksgorgeek
s                       geeksforgeeks                     

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to decode and print
// the original string
void decodeStr(string str, int len)
{
 
    // To store the decoded string
    char c[len] = "";
    int med, pos = 1, k;
 
    // Getting the mid element
    if (len % 2 == 1)
        med = len / 2;
    else
        med = len / 2 - 1;
 
    // Storing the first element of the
    // string at the median position
    c[med] = str[0];
 
    // If the length is even then store
    // the second element also
    if (len % 2 == 0)
        c[med + 1] = str[1];
 
    // k represents the number of characters
    // that are already stored in the c[]
    if (len & 1)
        k = 1;
    else
        k = 2;
 
    for (int i = k; i < len; i += 2) {
        c[med - pos] = str[i];
 
        // If string length is odd
        if (len % 2 == 1)
            c[med + pos] = str[i + 1];
 
        // If it is even
        else
            c[med + pos + 1] = str[i + 1];
        pos++;
    }
 
    // Print the decoded string
    for (int i = 0; i < len; i++)
        cout << c[i];
}
 
// Driver code
int main()
{
    string str = "ofrsgkeeeekgs";
    int len = str.length();
 
    decodeStr(str, len);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG{
 
// Function to decode and print
// the original String
static void decodeStr(String str, int len)
{
 
    // To store the decoded String
    char []c = new char[len];
    int med, pos = 1, k;
 
    // Getting the mid element
    if (len % 2 == 1)
        med = len / 2;
    else
        med = len / 2 - 1;
 
    // Storing the first element of the
    // String at the median position
    c[med] = str.charAt(0);
 
    // If the length is even then store
    // the second element also
    if (len % 2 == 0)
        c[med + 1] = str.charAt(1);
 
    // k represents the number of characters
    // that are already stored in the c[]
    if (len % 2 == 1)
        k = 1;
    else
        k = 2;
 
    for(int i = k; i < len; i += 2)
    {
       c[med - pos] = str.charAt(i);
        
       // If String length is odd
       if (len % 2 == 1)
           c[med + pos] = str.charAt(i + 1);
            
       // If it is even
       else
           c[med + pos + 1] = str.charAt(i + 1);
       pos++;
    }
 
    // Print the decoded String
    for (int i = 0; i < len; i++)
        System.out.print(c[i]);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "ofrsgkeeeekgs";
    int len = str.length();
 
    decodeStr(str, len);
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of the
# above approach
 
# Function to decode and print
# the original string
def decodeStr(str, len):
 
    # To store the decoded string
    c = ["" for i in range(len)]
    pos = 1
 
    # Getting the mid element
    if(len % 2 == 1):
        med = int(len / 2)
    else:
        med = int(len / 2 - 1)
 
    # Storing the first element 
    # of the string at the
    # median position
    c[med] = str[0]
 
    # If the length is even
    # then store the second
    # element also
    if(len % 2 == 0):
        c[med + 1] = str[1]
 
    # k represents the number
    # of characters that are
    # already stored in the c[]
    if(len & 1):
        k = 1
    else:
        k = 2
 
    for i in range(k, len, 2):
        c[med - pos] = str[i]
 
        # If string length is odd
        if(len % 2 == 1):
            c[med + pos] = str[i + 1]
 
        # If it is even
        else:
            c[med + pos + 1] = str[i + 1]
        pos += 1
 
    # Print the decoded string
    print(*c, sep = "")
 
# Driver code
str = "ofrsgkeeeekgs"
len = len(str)
decodeStr(str, len)
 
# This code is contributed by avanitrachhadiya2155

C#

// C# implementation of the approach
using System;
 
class GFG{
 
// Function to decode and print
// the original String
static void decodeStr(String str, int len)
{
 
    // To store the decoded String
    char []c = new char[len];
     
    int med, pos = 1, k;
 
    // Getting the mid element
    if (len % 2 == 1)
        med = len / 2;
    else
        med = len / 2 - 1;
 
    // Storing the first element of the
    // String at the median position
    c[med] = str[0];
 
    // If the length is even then store
    // the second element also
    if (len % 2 == 0)
        c[med + 1] = str[1];
 
    // k represents the number of characters
    // that are already stored in the c[]
    if (len % 2 == 1)
        k = 1;
    else
        k = 2;
 
    for(int i = k; i < len; i += 2)
    {
       c[med - pos] = str[i];
        
       // If String length is odd
       if (len % 2 == 1)
           c[med + pos] = str[i + 1];
        
       // If it is even
       else
           c[med + pos + 1] = str[i + 1];
       pos++;
    }
 
    // Print the decoded String
    for(int i = 0; i < len; i++)
       Console.Write(c[i]);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "ofrsgkeeeekgs";
    int len = str.Length;
 
    decodeStr(str, len);
}
}
 
// This code is contributed by sapnasingh4991

Javascript

<script>
 
 
// JavaScript implementation of the approach
 
// Function to decode and print
// the original string
function decodeStr(str, len)
{
 
    // To store the decoded string
    var c = Array(len).fill("");
    var med, pos = 1, k;
 
    // Getting the mid element
    if (len % 2 == 1)
        med = parseInt(len / 2);
    else
        med = parseInt(len / 2) - 1;
 
    // Storing the first element of the
    // string at the median position
    c[med] = str[0];
 
    // If the length is even then store
    // the second element also
    if (len % 2 == 0)
        c[med + 1] = str[1];
 
    // k represents the number of characters
    // that are already stored in the c[]
    if (len & 1)
        k = 1;
    else
        k = 2;
 
    for (var i = k; i < len; i += 2) {
        c[med - pos] = str[i];
 
        // If string length is odd
        if (len % 2 == 1)
            c[med + pos] = str[i + 1];
 
        // If it is even
        else
            c[med + pos + 1] = str[i + 1];
        pos++;
    }
 
    // Print the decoded string
    for (var i = 0; i < len; i++)
    {
        document.write(c[i]);
    }
}
 
// Driver code
var str = "ofrsgkeeeekgs";
var len = str.length;
decodeStr(str, len);
 
</script>
Producción: 

geeksforgeeks

 

La Complejidad: O(n)
 

Publicación traducida automáticamente

Artículo escrito por kanakgoel36 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *