您的位置:首页 > 其它

UVA10222 Decode the Mad man【编码】

2018-02-09 15:02 288 查看
Once in BUET, an old professor had gone completely mad. He started talking with some peculiarwords. Nobody could realize his speech and lectures. Finally the BUET authority fall in great trouble.There was no way left to keep that man working in university. Suddenly a student (definitely he wasa registered author at UVA ACM Chapter and hold a good rank on 24 hour-Online Judge) createda program that was able to decode that professor’s speech. After his invention, everyone got comfortagain and that old teacher started his everyday works as before.
  So, if you ever visit BUET and see a teacher talking with a microphone, which is connected to aIBM computer equipped with a voice recognition software and students are taking their lecture fromthe computer screen, don’t get thundered! Because now your job is to write the same program whichcan decode that mad teacher’s speech!
Input
The input file will contain only one test case i.e. the encoded message.
  The test case consists of one or more words.
Output
For the given test case, print a line containing the decoded words. However, it is not so hard task toreplace each letter or punctuation symbol by the two immediately to its left alphabet on your standardkeyboard.
Sample Input
k[r dyt I[o
Sample Output
how are you

问题链接UVA10222 Decode the Mad man
问题简述:(略)
问题分析:(略)
程序说明
  这是一个编码问题,键盘错位,错了2位。
  有两种方法可以实现,明显后一种方法是优化的:
  一是,做一张对照表,对于输入的字符,在表中查找,得到(计算)其编码。
  二是,事先做出编码表,对于输入的字符查一下。
题记:(略)
参考链接:(略)

AC的C++语言程序(先计算编码)如下:
/* UVA10222 Decode the Mad man */

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

using namespace std;

char code[] = "qwertyuiop[]asdfghjkl;'zxcvbnm,.";
char decode[256];

int main()
{
// 编码计算
memset(decode, 0, sizeof(decode));
for(int i=2; code[i]; i++)
decode[(int)code[i]] = code[i - 2];

char c;
while((c = getchar()) != EOF) {
if(isupper(c))
c = tolower(c);

if(decode[(int)c])
putchar(decode[(int)c]);
else
putchar(c);
}

return 0;
}

AC的C++语言程序(逐个字符查找)如下:
/* UVA10222 Decode the Mad man */

#include <iostream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

char code[] = "qwertyuiop[]asdfghjkl;'zxcvbnm,.";

int main()
{
char c;
int i;

while((c = getchar()) != EOF) {
if(isupper(c))
c = tolower(c);

for(i=2; code[i] && code[i] != c; i++)
;

if(code[i])
putchar(code[i - 2]);
else
putchar(c);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: