Loading... # Master of Shuangpin **As you know, there are three kinds of Chinese input methods commonly used: Wubi, Pinyin and Shuangpin. With Shuangpin, you can type ****any** Chinese word by pressing keys only twice. **[h]** | **Pinyin** | **Sequence** | **Pinyin** | **Sequence** | | ------------------ | -------------- | ------------------- | -------------- | | **q, iu** | **q** | **f, en** | **f** | | **w, ei** | **w** | **g, eng** | **g** | | **e** | **e** | **h, ang** | **h** | | **r, uan** | **r** | **j, an** | **j** | | **t, ue** | **t** | **k, uai, ing** | **k** | | **y, un** | **y** | **l, uang, iang** | **l** | | **u, sh** | **u** | **z, ou** | **z** | | **i, ch** | **i** | **x, ia, ua** | **x** | | **o, uo** | **o** | **c, ao** | **c** | | **p, ie** | **p** | **v, zh, ui** | **v** | | **a** | **a** | **b, in** | **b** | | **s, ong, iong** | **s** | **n, iao** | **n** | | **d, ai** | **d** | **m, ian** | **m** | **Attention that:** * **For pinyin of length 1, you should repeat it in order to meet the conditions.** * **For those of length 2, just output the original pinyin.** * **For pinyin such as ***ang*, you should press the first character of it and then look up this whole pinyin in the table for the second key. * **For simplification, ****there is no character *v* in any input.** We believe that you, a Pinyin master, can tell *u* and *v* in any situations such as *lve* and *que*, so we **do not** challenge you here. **OK, now you are already a MASTER of Shuangpin! Please output the keys sequence to type the given sentences. For example, "ni hao shi jie" will be "ni hc ui jp".** **Input** **There are multiple test cases. Each line contains one.** **Each line is a sequence of pinyin separated by spaces.** **It is guaranteed that the number of test case is no more than 1000, the number of pinyin in one test case is no more than 500, and the number of pinyin in all the test cases is no more than 5000.** **Output** **The keys sequence separated by spaces.** **Sample 1** | **Input** | **Output** | | ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | | `rua ni xian qi po lan rang wo men dang qi shuang jiang cha na zhua zhu le wei lai zhe ti mian shen me wan yi` | `rx ni xm qi po lj rh wo mf dh qi ul jl ia na vx vu le ww ld ve ti mm uf me wj yi` | **Sample 2** | **Input** | **Output** | | ------------------------------------------------------------------------------ | ------------------------------------------------------------------ | | `ni you ben shi na lai mai a wo e le wo men chi shen me ang yang de dou zhi` | `ni yz bf ui na ld md aa wo ee le wo mf ii uf me ah yh de dz vi` | **题意:** **输入若干个字符串,由声母和韵母组成,按上述表格对字母进行替换,同时遵守以下规则** 1. **若拼音只有一个字符,输出两个该字符** 2. **若拼音由两个字符,输出原字符** 3. **若拼音只有纯韵母,则输出该韵母第一个字符,并将该韵母替换,如“ang”输出“ah”** **题解:** **思路是先判断输入字符串长度,若长度为一,输出两个该字符,若长度为二,输出原字符,若长度大于三,判断是否为韵母,若为韵母,输出第一个字符,再输出该字符串替换后的字符,否则判断声母部分字符是一个还是两个,先排除两个,再排除一个的。具体请看代码** **(这题说白了就是输入一个拼音,让你转换为两个字符,故为双拼)** ``` #include <iostream> #include <cstring> #include <string> #include <map> using namespace std; string a[] = {"iu", "ei", "uan", "ue", "un", "sh", "ch", "uo", "ie", "ong", "iong", "ai", "en", "eng", "ang", "an", "uai", "ing", "uang", "iang", "ou", "ia", "ua", "ao", "zh", "ui", "in", "iao", "ian", "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"}; string b = "qwrtyuiopssdfghjkkllzxxcvvbnmqwertyuiopasdfghjklzxcvbnm"; map<string, char> mp; int main(void) { for(int i = 0; i < b.length(); ++i) { mp[a[i]] = b[i]; } char ch; string str; while (cin >> str) { ch = getchar(); if (str.length() == 1) { cout << str << str; } else if (str.length() == 2) { cout << str; } else if (str[1] == 'h') { cout << mp[str.substr(0, 2)] << mp[str.substr(2)]; } else if (mp.count(str)){ cout << str[0] << mp[str]; } else { cout << str[0] << mp[str.substr(1)]; } cout << ch; } return 0; } ``` **反思:** 看网上代码非常复杂,实在看不下去,在同学的帮助下读懂题后,发现非常简单,花了短短的时间将题写了,一次性过,由此可见读题的重要性,花大部分时间来读题很由必要,故写此文,以警示自己。 最后修改:2022 年 07 月 17 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏