题目相关:
3878相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5526
Edward 打字员习惯于QWERTY键盘布局来打字(该键盘Caps Lock失效), 一天他需要把所打的文字从QWERTY键盘布局转为Dvorak键盘布局, 求转换程序.
两者的键盘布局如图所对比:
思路分析:
该题是个简单的模拟题, 重要的是理解题意.
我们所要做的是把两键盘的字符做个一一对应的映射.当然我们可以做个技巧, 不同键做映射, 相同的键可以忽略不做.
AC代码:
#include <cstdio> #include <cstring> #include <map> #include <cassert> std::map<char, char> kbmap; void init() { const char *key1 = "-=_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;‘ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?"; const char *key2 = "[]{}‘,.pyfgcrl/=\\\"<>PYFGCRL?+|aoeuidhtns-AOEUIDHTNS_;qjkxbmwvz:QJKXBMWVZ"; size_t key1_len = strlen(key1); size_t key2_len = strlen(key2); assert(key1_len == key2_len); for ( size_t i = 0; i < key1_len; i++ ) { kbmap[key1[i]] = key2[i]; } } void convert(char *buffer) { int len = strlen(buffer); for ( int i = 0; i < len; i++ ) { char ch = buffer[i]; if ( kbmap.find(ch) != kbmap.end() ) { buffer[i] = kbmap[ch]; } } } int main() { init(); char buffer[1024 * 2]; while ( fgets(buffer, sizeof(buffer), stdin) != NULL ) { convert(buffer); printf("%s", buffer); } return 0; }
时间: 2024-10-24 12:26:31