HJ29 字符串加解密

摘要
Title: HJ29 字符串加解密
Tag: 加密、解密
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

HJ29 字符串加解密

  • 题意

    对输入的字符串进行加解密,并输出。
    加密方法为:
    当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
    当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
    其他字符不做变化。
    解密方法为加密的逆过程。

  • 思路

    字符串加解密,可以直接用字典法解决,前提是知道加密和解密的固定字母,直接find到下标,在去另一个字符串找到即可

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #include <algorithm>
    #include <bits/stdc++.h>
    #include <cctype>
    #include <sstream>
    #include <vector>
    using namespace std;
    #define int long long
    #undef int

    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'

    const int N = 1e5 + 10, INF = 0x3f3f3f3f;

    int main() {
    string str1, str2;
    cin >> str1 >> str2;
    string a("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
    string b("BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890");
    for (int i = 0; i < str1.size(); ++i)
    str1[i] = b[a.find(str1[i])];
    for (int i = 0; i < str2.size(); ++i)
    str2[i] = a[b.find(str2[i])];
    cout << str1 << endl << str2;

    return 0;
    }
使用搜索:谷歌必应百度