📜  根据给定的算法解密字符串

📅  最后修改于: 2021-09-03 14:37:37             🧑  作者: Mango

给定由字母和数字字符组成的加密字符串str ,任务是解密字符串并找到加密消息。
为了解密消息,找到代表单个字母字符的每个数字字符簇,可以通过计算数字与 26 的模数获得,并且可以将范围 [0, 25] 中的找到值与字符[ ‘a’, ‘z’]。
例如,如果str = “32ytAAcV4ui30hf10hj18”,那么数字字符的簇将是{32, 4, 30, 10, 18} ,在对 26 执行模数后给出{6, 4, 4, 10, 18}并且可以映射到{‘g’, ‘e’, ‘e’, ‘k’, ‘s’}
例子:

方法:思路是将字符串的每个字符一个一个遍历,然后检查它是否是数字字符。如果是,则将其连接成一个字符串。最后,用 26 对该数字字符串求模。
在做模运算时,我们不能简单地做 x % 26 因为这个数字可能太大并且会导致整数溢出。
为了解决这个问题,我们将一一处理所有数字并使用以下属性
(A * B) mod C = ((A mod C) * (B mod C)) mod C
然后,最后通过0,1,2,3,… 25 ‘A’, ‘B’, ‘C’,… ‘Z’,串联的所有字符解码回到每个整数字符和打印的最终结果。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
const int MOD = 26;
 
// Function that returns (num % 26)
int modulo_by_26(string num)
{
    // Initialize result
    int res = 0;
 
    // One by one process all digits of 'num'
    for (int i = 0; i < num.length(); i++)
        res = (res * 10 + (int)num[i] - '0') % MOD;
 
    return res;
}
 
// Function to return the decrypted string
string decrypt_message(string s)
{
 
    // To store the final decrypted answer
    string decrypted_str = "";
 
    string num_found_so_far = "";
 
    // One by one check for each character
    // if it is a numeric character
    for (int i = 0; i < s.length(); ++i) {
 
        if (s[i] >= '0' && s[i] <= '9') {
            num_found_so_far += s[i];
        }
        else if (num_found_so_far.length() > 0) {
 
            // Modulo the number found in the string by 26
            decrypted_str += 'a'
                             + modulo_by_26(num_found_so_far);
 
            num_found_so_far = "";
        }
    }
 
    if (num_found_so_far.length() > 0) {
        decrypted_str += 'a'
                         + modulo_by_26(num_found_so_far);
    }
 
    return decrypted_str;
}
 
// Driver code
int main()
{
    string s = "32ytAAcV4ui30hf10hj18";
 
    // Print the decrypted string
    cout << decrypt_message(s);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG{
     
static int MOD = 26;
 
// Function that returns (num % 26)
static int modulo_by_26(String num)
{
     
    // Initialize result
    int res = 0;
 
    // One by one process all digits of 'num'
    for(int i = 0; i < num.length(); i++)
    {
       res = ((res * 10 +
              (int)num.charAt(i) - '0') % MOD);
    }
    return res;
}
 
// Function to return the decrypted String
static String decrypt_message(String s)
{
 
    // To store the final decrypted answer
    String decrypted_str = "";
    String num_found_so_far = "";
 
    // One by one check for each character
    // if it is a numeric character
    for(int i = 0; i < s.length(); ++i)
    {
       if (s.charAt(i) >= '0' && s.charAt(i) <= '9')
       {
           num_found_so_far += s.charAt(i);
       }
       else if (num_found_so_far.length() > 0)
       {
            
           // Modulo the number found in the String by 26
           decrypted_str += (char)('a' +
                             modulo_by_26(num_found_so_far));
           num_found_so_far = "";
       }
    }
    if (num_found_so_far.length() > 0)
    {
        decrypted_str += (char)('a' +
                          modulo_by_26(num_found_so_far));
    }
    return decrypted_str;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "32ytAAcV4ui30hf10hj18";
 
    // Print the decrypted string
    System.out.print(decrypt_message(s));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation of
# the approach
MOD = 26
 
# Function that returns
# (num % 26)
def modulo_by_26(num):
 
    # Initialize result
    res = 0
 
    # One by one process all
    # digits of 'num'
    for i in range(len(num)):
        res = ((res * 10 + ord(num[i]) -
                ord('0')) % MOD)
 
    return res
 
# Function to return the
# decrypted string
def decrypt_message(s):
 
    # To store the final
    # decrypted answer
    decrypted_str = ""
 
    num_found_so_far = ""
 
    # One by one check for
    # each character if it
    # is a numeric character
    for i in range(len(s)):
 
        if (s[i] >= '0' and
            s[i] <= '9'):
            num_found_so_far += s[i]
        elif (len(num_found_so_far) > 0):
 
            # Modulo the number found
            # in the string by 26
            decrypted_str += chr(ord('a') +
                            (modulo_by_26(num_found_so_far)))
 
            num_found_so_far = ""
 
    if (len(num_found_so_far) > 0):
        decrypted_str += chr(ord('a') +
                        (modulo_by_26(num_found_so_far)))
 
    return decrypted_str
 
# Driver code
if __name__ == "__main__":
 
    s = "32ytAAcV4ui30hf10hj18"
 
    # Print the decrypted string
    print(decrypt_message(s))
 
# This code is contributed by Chitranayal


C#
// C# implementation of the approach
using System;
class GFG{
     
static int MOD = 26;
 
// Function that returns (num % 26)
static int modulo_by_26(String num)
{
     
    // Initialize result
    int res = 0;
 
    // One by one process all digits of 'num'
    for(int i = 0; i < num.Length; i++)
    {
        res = ((res * 10 +
               (int)num[i] - '0') % MOD);
    }
    return res;
}
 
// Function to return the decrypted String
static String decrypt_message(String s)
{
 
    // To store the readonly decrypted answer
    String decrypted_str = "";
    String num_found_so_far = "";
 
    // One by one check for each character
    // if it is a numeric character
    for(int i = 0; i < s.Length; ++i)
    {
        if (s[i] >= '0' && s[i] <= '9')
        {
            num_found_so_far += s[i];
        }
        else if (num_found_so_far.Length > 0)
        {
                 
            // Modulo the number found
            // in the String by 26
            decrypted_str += (char)('a' +
                              modulo_by_26(num_found_so_far));
            num_found_so_far = "";
        }
    }
    if (num_found_so_far.Length > 0)
    {
        decrypted_str += (char)('a' +
                          modulo_by_26(num_found_so_far));
    }
    return decrypted_str;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "32ytAAcV4ui30hf10hj18";
 
    // Print the decrypted string
    Console.Write(decrypt_message(s));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
geeks

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