Dada una string cifrada str y el algoritmo de cifrado, la tarea es descifrar la string. El algoritmo de cifrado es el siguiente:
el 1er carácter de la string se repetirá una vez en la string cifrada, el 2º carácter se repetirá dos veces, …, el nº carácter se repetirá n veces . Por ejemplo, la string “abcd” se cifrará como “abbcccdddd” .
Ejemplos:
Entrada: str = “geeeeekkkksssss”
Salida: geeksEntrada: str = “abbcccdddd”
Salida: abcd
Enfoque: inicialice i = 0 e imprima str[i] .
Actualice i = i + 1 e imprima str[i] , luego actualice i = i + 2 e imprima str[i] y así sucesivamente mientras i < length(str) .
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 return the decrypted string string decryptString(string str, int n) { // Initial jump will be 1 int i = 0, jump = 1; string decryptedStr = ""; while (i < n) { decryptedStr += str[i]; i += jump; // Increment jump by 1 with every character jump++; } return decryptedStr; } // Driver code int main() { string str = "geeeeekkkksssss"; int n = str.length(); cout << decryptString(str, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the decrypted string static String decryptString(String str, int n) { // Initial jump will be 1 int i = 0, jump = 1; String decryptedStr = ""; while (i < n) { decryptedStr += str.charAt(i); i += jump; // Increment jump by 1 with every character jump++; } return decryptedStr; } // Driver code public static void main(String[] args) { String str = "geeeeekkkksssss"; int n = str.length(); System.out.println(decryptString(str, n)); } } // This code is contributed by Code_Mech
Python3
# Python 3 implementation of the approach # Function to return the decrypted string def decryptString(str, n): # Initial jump will be 1 i = 0 jump = 1 decryptedStr = "" while (i < n): decryptedStr += str[i]; i += jump # Increment jump by 1 with # every character jump += 1 return decryptedStr # Driver code if __name__ == '__main__': str = "geeeeekkkksssss" n = len(str) print(decryptString(str, n)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to return the decrypted string static string decryptString(string str, int n) { // Initial jump will be 1 int i = 0, jump = 1; string decryptedStr = ""; while (i < n) { decryptedStr += str[i]; i += jump; // Increment jump by 1 with every character jump++; } return decryptedStr; } // Driver code public static void Main() { string str = "geeeeekkkksssss"; int n = str.Length; Console.Write(decryptString(str, n)); } } // This code is contributed by ita_c
PHP
<?php // PHP implementation of the approach // Function to return the decrypted string function decryptString($str, $n) { // Initial jump will be 1 $i = 0 ; $jump = 1 ; $decryptedStr = ""; while ($i < $n) { $decryptedStr .= $str[$i]; $i += $jump; // Increment jump by 1 with // every character $jump++; } return $decryptedStr; } // Driver code $str = "geeeeekkkksssss"; $n = strlen($str); echo decryptString($str, $n); // This code is contributed by Ryuga ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the decrypted string function decryptString(str,n) { // Initial jump will be 1 let i = 0, jump = 1; let decryptedStr = ""; while (i < n) { decryptedStr += str[i]; i += jump; // Increment jump by 1 with every character jump++; } return decryptedStr; } // Driver code let str = "geeeeekkkksssss"; let n = str.length; document.write(decryptString(str, n)); // This code is contributed by rag2127 </script>
geeks
Publicación traducida automáticamente
Artículo escrito por Sumit bangar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA