POJ 2538 WERTYU(我的水题之路——键盘错位)

WERTYU
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6778   Accepted: 3152

Description

POJ 2538 WERTYU(我的水题之路——键盘错位)_第1张图片 
A common typing error is to place the hands on the keyboard one row to the right of the correct position. So "Q" is typed as "W" and "J" is typed as "K" and so on. You are to decode a message typed in this manner.

Input

Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.

Output

You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output. 

Sample Input

O S, GOMR YPFSU/

Sample Output

I AM FINE TODAY.

Source

Waterloo local 2001.01.27

一个键盘错位了,每个按键均向右边移动一格,即是按键‘W’输出‘Q’,按键‘E’输出‘W’……,现在输入许多行数据,要求你将其还原成正确文字。

这道题有三种解法,应该说是三种数据结构。
1)第一想到的是MAP,但是MAP用起来可能比较消耗时间,
2)用一个字符数组直接用每一个字符作为下标,然后将正确的字符作为这个数组的元素, 我用的这种方法。
3)如Discuss中某大牛给的数组char mapi[]={'`','1','2','3','4','5','6','7','8','9','0','-','=','Q','W','E','R','T','Y','U','I','O','P','[',']','\\','A','S','D','F','G','H','J','K','L',';','\'','Z','X','C','V','B','N','M',',','.','/'};可以搜索输入字符的位置,然后再输出其前一个,这个就比较浪费时间。
4)其实还有很多很多方法的- -。。。毕竟是水题。

注意点:
1)空格' '和回车'\n'两个字符不做转换,直接输出,Shirt、Ctrl、Backspace按键也是如此。

代码(1AC):
#include <cstdio>
#include <cstdlib>
#include <cstring>

char str[200];

void init(){
    int i;

    str['1'] = '`';
    for (i = 2; i <= 10; i++){
        str['0' + i % 10] = '0' + i - 1;
    }
    str['-'] = '0';
    str['='] = '-';
    str['W'] = 'Q';
    str['E'] = 'W';
    str['R'] = 'E';
    str['T'] = 'R';
    str['Y'] = 'T';
    str['U'] = 'Y';
    str['I'] = 'U';
    str['O'] = 'I';
    str['P'] = 'O';
    str['['] = 'P';
    str[']'] = '[';
    str['\\'] = ']';
    str['S'] = 'A';
    str['D'] = 'S';
    str['F'] = 'D';
    str['G'] = 'F';
    str['H'] = 'G';
    str['J'] = 'H';
    str['K'] = 'J';
    str['L'] = 'K';
    str[';'] = 'L';
    str['\''] = ';';
    str['X'] = 'Z';
    str['C'] = 'X';
    str['V'] = 'C';
    str['B'] = 'V';
    str['N'] = 'B';
    str['M'] = 'N';
    str[','] = 'M';
    str['.'] = ',';
    str['/'] = '.';
}

int main(void){
    char ch;

    init();
    while ((ch = getchar()) != EOF){
        if (ch != ' ' && ch != '\n'){
            putchar(str[ch]);
        }
        else{
            putchar(ch);
        }
    }
    return 0;
}


你可能感兴趣的:(POJ 2538 WERTYU(我的水题之路——键盘错位))