474A - Keyboard

本文探讨了一个有趣的问题:如何从错误输入中还原原始消息。假设一个盲人朋友在特殊键盘上输入信息时,手部位置偏移,导致每个字符都按到了左或右相邻的键。通过构建字符映射,我们解析了错误输入,成功找到了原始信息。这个过程涉及字符匹配和逆向工程,展示了编程解决实际问题的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Keyboardtime
limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way:
qwertyuiop
asdfghjkl;
zxcvbnm,./
Unfortunately Mole is blind, so sometimes it is problem for him to put his hands accurately. He accidentally moved both his hands with one position to the left or to the right. That means that now he presses not a button he wants, but one neighboring button (left or right, as specified in input).We have a sequence of characters he has typed and we want to find the original message.
Input
First line of the input contains one letter describing direction of shifting (‘L’ or ‘R’ respectively for left or right).Second line contains a sequence of characters written by Mole. The size of this sequence will be no more than 100. Sequence contains only symbols that appear on Mole’s keyboard. It doesn’t contain spaces as there is no space on Mole’s keyboard.
It is guaranteed that even though Mole hands are moved, he is still pressing buttons on keyboard and not hitting outside it.
Output
Print a line that contains the original message.
Examples
Input
R
s;;upimrrfod;pbr
OutputCopyallyouneedislove
注意键盘匹配,用map,以字符作为索引,输出左边或右边的字符

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
int main()
{
 map<char,char>ch1;
 map<char,char>ch2;
 int i=0,j,k;
 char t,ch3[101],b;
 ch1['a']='s';
 ch1['s']='d';
 ch1['d']='f';
 ch1['f']='g';
 ch1['g']='h';
 ch1['h']='j';
 ch1['j']='k';
 ch1['k']='l';
 ch1['l']=';';
 //ch1[';']=''';
 ch1['q']='w';
 ch1['w']='e';
 ch1['e']='r';
 ch1['r']='t';
 ch1['t']='y';
 ch1['y']='u';
 ch1['u']='i';
 ch1['i']='o';
 ch1['o']='p';
 //ch1['p']=;
 ch1['z']='x';
 ch1['x']='c';
 ch1['c']='v';
 ch1['v']='b';
 ch1['b']='n';
 ch1['n']='m';
 ch1['m']=',';
 ch1[',']='.';
 ch1['.']='/';
 
// ch2[''']=';';
 ch2[';']='l';
 ch2['l']='k';
 ch2['k']='j';
 ch2['j']='h';
 ch2['h']='g';
 ch2['g']='f';
 ch2['f']='d';
 ch2['d']='s';
 ch2['s']='a';
 ch2['[']='p';
 ch2['p']='o';
 ch2['o']='i';
 ch2['i']='u';
 ch2['u']='y';
 ch2['y']='t';
 ch2['t']='r';
 ch2['r']='e';
 ch2['e']='w';
 ch2['w']='q';
 ch2['/']='.';
 ch2['.']=',';
 ch2[',']='m';
 ch2['m']='n';
 ch2['n']='b';
 ch2['b']='v';
 ch2['v']='c';
 ch2['c']='x';
 ch2['x']='z';
 cin>>t;
 getchar();
 do
 {
  ch3[i]=getchar();
  i++;
 }while(ch3[i-1]!='\n');
 if(t=='R')
 {
  for(j=0;j<i-1;j++)
  {
   b=ch3[j];
   b=ch2[b];
   cout<<b;
  }
 }
 else
 {
  for(j=0;j<i-1;j++)//注意检索到小于i-1
  {
   b=ch3[j];
   b=ch1[b];
   cout<<b;
  }
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值