Posted on 2008-03-29 00:35
superman 阅读(249)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
1 /* Accepted 1068 C++ 00:00.00 868K */
2 #include <map>
3 #include <stack>
4 #include <string>
5 #include <iostream>
6
7 using namespace std;
8
9 int main()
10 {
11 map <char, string> morse;
12 morse['A'] = ".-";
13 morse['B'] = "-...";
14 morse['C'] = "-.-.";
15 morse['D'] = "-..";
16 morse['E'] = ".";
17 morse['F'] = "..-.";
18 morse['G'] = "--.";
19 morse['H'] = "....";
20 morse['I'] = "..";
21 morse['J'] = ".---";
22 morse['K'] = "-.-";
23 morse['L'] = ".-..";
24 morse['M'] = "--";
25 morse['N'] = "-.";
26 morse['O'] = "---";
27 morse['P'] = ".--.";
28 morse['Q'] = "--.-";
29 morse['R'] = ".-.";
30 morse['S'] = "...";
31 morse['T'] = "-";
32 morse['U'] = "..-";
33 morse['V'] = "...-";
34 morse['W'] = ".--";
35 morse['X'] = "-..-";
36 morse['Y'] = "-.--";
37 morse['Z'] = "--..";
38 morse['_'] = "..--";
39 morse[','] = ".-.-";
40 morse['.'] = "---.";
41 morse['?'] = "----";
42
43 int n, count = 0; cin >> n;
44 string message;
45 while(cin >> message)
46 {
47 string code;
48 stack <int> len;
49 for(int i = 0; i < message.size(); i++)
50 {
51 code += morse[message[i]];
52 len.push(morse[message[i]].size());
53 }
54
55 cout << ++count << ':' << ' ';
56
57 int pos = 0;
58 string ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_,.?";
59 while(len.empty() == false)
60 {
61 for(int i = 0; i < 30; i++)
62 if(morse[ch[i]].size() == len.top())
63 if(code.find(morse[ch[i]], pos) == pos)
64 {
65 cout << ch[i];
66 break;
67 }
68 pos += len.top();
69 len.pop();
70 }
71 cout << endl;
72 }
73
74 return 0;
75 }
76