[Title] Huawei machine test string encryption 108

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011544909/article/details/79999092

Title Description
There is a skill you can encrypt the data, it uses a word as its key. Here's how it works: First, select a word as a key, such as TRAILBLAZERS. If the repeated word contains letters, retaining only the first one, the remaining few discarded. Now, that word belongs to modified following the alphabet, as follows:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

T R A I L B Z E S C D F G H J K M N O P Q U V W X Y

The above are filled up with the other remaining alphabet letters. When the information is encrypted, the information in each letter that is fixed to the top of the line and the line below the letters with the corresponding substituted original eleven letters (case alphabetic characters should be retained state). Thus, using this key, Attack AT DAWN (Dawn attack) will be encrypted as Tpptad TP ITVH.

Please implement the following interface specified by the ciphertext and the plaintext key.

Detailed Description:

Interface Description

prototype:

voidencrypt(char * key,char * data,char * encrypt);

Input parameters:

char * key: Key

char * data: plain text

Output parameters:

char * encrypt: ciphertext

return value:

void

Input Description:
to enter a character string to be encrypted and the key

Description Output:
string returned encrypted

Example 1
Input
NiHao
Ni
output
le


Ideas, we can come and re-use an array, then loop to the table with a plus for the rest of the letter, traversing key again, with the corresponding lookup table can be carried out.

#include <bits/stdc++.h>
using namespace std;

void encrypt(string &key,string &str)
{
    int flag[26]={0};
    string table;
    for(int i=0;i<key.size();i++)
    {
        char ch=toupper(key[i]);
        if(!flag[ch-'A'])
        {
            table.push_back(ch);
            flag[ch-'A']=1;
        }
    }
        string temp=table;
        for(int i='A';i<='Z';i++)
        {
            if(temp.find(i)==string::npos)
            {
                table.push_back(i);
            }
        }

        for(int i=0;i<str.size();i++)
        {
            if(islower(str[i]))
                str[i]=tolower(table[str[i]-'a']);
        }
}

int main()
{
    string key,str;
    while(cin>>key>>str)
    {
        encrypt(key,str);
        cout<<str<<endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/u011544909/article/details/79999092