forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1051(AC).cpp
160 lines (146 loc) · 1.79 KB
/
POJ1051(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <string.h>
char dict[30][5] = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--..",
"..--",
"---.",
".-.-",
"----"
};
char GetChar(const char str[])
{
int i;
for(i = 0; i < 26; i++)
{
if(strcmp(str, dict[i]) == 0)
{
return i + 'A';
}
}
if(strcmp(str, dict[26]) == 0)
{
return '_';
}
else if(strcmp(str, dict[27]) == 0)
{
return '.';
}
else if(strcmp(str, dict[28]) == 0)
{
return ',';
}
else if(strcmp(str, dict[29]) == 0)
{
return '?';
}
else
{
return 0;
}
}
char *GetMorseCode(const char ch)
{
if(ch >= 'A' && ch <= 'Z')
{
return dict[ch - 'A'];
}
else if(ch == '_')
{
return dict[26];
}
else if(ch == '.')
{
return dict[27];
}
else if(ch == ',')
{
return dict[28];
}
else if(ch == '?')
{
return dict[29];
}
else
{
return NULL;
}
}
int main()
{
int n;
int i;
int j;
int k;
int t;
int len;
int pos;
char cht;
char line[500];
char output[500];
char code[2000];
char *tmp;
int lens[200];
while(scanf("%d", &n) != EOF)
{
getchar();
for(i = 0; i < n; i++)
{
gets(line);
len = strlen(line);
code[0] = 0;
for(j = 0; j < len; j++)
{
tmp = GetMorseCode(line[j]);
lens[j] = strlen(tmp);
strcat(code, tmp);
}
j = 0;
k = len - 1;
while(j < k)
{
t = lens[j];
lens[j] = lens[k];
lens[k] = t;
j++;
k--;
}
pos = 0;
for(j = 0; j < len; j++)
{
cht = code[pos + lens[j]];
code[pos + lens[j]] = 0;
output[j] = GetChar(code + pos);
code[pos + lens[j]] = cht;
pos = pos + lens[j];
}
output[j] = 0;
printf("%d: %s\n", i + 1, output);
}
}
return 0;
}