-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-508.cpp
63 lines (62 loc) · 1.23 KB
/
UVA-508.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
#include<iostream>
#include<string>
#include<map>
#include<set>
using namespace std;
int main() {
string s,st;
int i,j,k,tmp;
char c;
map<char, string> chars;
map<string, string> words;
set<string> se;
while(cin >> st) {
if(st[0] == '*')
break;
cin >> s;
chars[st[0]] = s;
}
while(cin >> st) {
if(st[0] == '*')
break;
s = "";
for(i=0; i!=st.size(); ++i) {
s += chars[st[i]];
}
if(words.find(s) == words.end())
words[s] = st;
else {
se.insert(s);
//if(words[s].size() > st.size() || (words[s].size() == st.size() && st < words[s]))
if(st < words[s])
words[s] = st;
}
}
while(cin >> st) {
if(st[0] == '*')
break;
if(words.find(st) != words.end()) {
cout << words[st];
if(se.find(st) != se.end())
cout << "!";
cout << endl;
} else {
k = 200;
s = words.begin()->second;
for(auto ip = words.begin(); ip != words.end(); ++ip) {
for(i=0; i<st.size(); ++i)
if(ip->first.size() == i || ip->first[i] != st[i])
break;
if(ip->first.size() != i && st.size() != i)
continue;
tmp = st.size()-i+ip->first.size()-i;
if(tmp < k || (k == tmp && ip->second < s)) {
s = ip->second;
k = tmp;
}
}
cout << s << "?" << endl;
}
}
return 0;
}