【ACM】 字符串 加密解密 / 字符串变换类 问题
写在前面:
经常会遇到那种给一个字符串,然后按照对应的变换规则,将相应的字符进行一些变换,如不同种类的键盘,或者是加密解密的编码规则等等。
正文:
对于这种问题,一般有两种解体思路:
第一种就是按照题目中给的条件,将字符变换的规律找出,然后可以直接遍历转换
第二种也就是如果是实在没有规律那就打个表解决一下
建立两个字符数组(字符串)A1,A2 ; 一个代表原来的字符序列,另一个表示转换后的字符序列,然后在将字符A1中找到相应的位置,直接在A2中进行一系列的转换即可。
样例:ZOJ - 3878
Convert QWERTY to Dvorak Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, a poor copy typist, is a user of the Dvorak Layout. But now he has only a QWERTY Keyboard with a broken Caps Lock key, so Edward never presses the broken Caps Lock key. Luckily, all the other keys on the QWERTY keyboard work well. Every day, he has a lot of documents to type. Thus he needs a converter to translate QWERTY into Dvorak. Can you help him? The QWERTY Layout and the Dvorak Layout are in the following:
Input A QWERTY document Edward typed. The document has no more than 100 kibibytes. And there are no invalid characters in the document. Output The Dvorak document. Sample Input
Sample Output
|
题解:
#include<bits/stdc++.h>
using namespace std;
char s1[]= {"-=_+qwertyuiop[]QWERTYUIOP{}asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?"};
char s2[]= {"[]{}',.pyfgcrl/=\"<>PYFGCRL?+aoeuidhtns-AOEUIDHTNS_;qjkxbmwvz:QJKXBMWVZ"};
char print(char c)
{
for(int i=0; s1[i]; i++)
if(s1[i]==c)
return s2[i];
return c;
}
int main()
{
char c;
while (scanf("%c",&c)!=EOF)
printf("%c",print(c));
return 0;
}
注意:: 可以直接在输入时按照一个一个字符输入,并进行转换,可以节约空间复杂度和时间复杂度