-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP642.cpp
60 lines (56 loc) · 1.44 KB
/
P642.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
#include <iostream>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
typedef unsigned long ul;
typedef pair<ul,string> PP;
typedef map<ul,int> Map;
ul encode(char *line) {
string s(line);
ul ret = 0;
sort(s.begin(), s.end());
for(unsigned int i = 0; i < s.size(); ++i)
ret = ret*26 + (s[i]-'a');
return ret;
}
int main() {
PP dictionary[100];
int size = 0;
char line[9];
while(gets(line)) {
string s(line);
if(s.compare("XXXXXX") == 0)
break;
//cerr << "Read " << s << ", size " << s.size() << ", encoding: " << encode(line) << endl;
dictionary[size++] = PP(encode(line), s);
}
sort(dictionary, dictionary+size);
Map indices;
for(int i = 0; i < size; ++i) {
if(i == 0 || dictionary[i].first != dictionary[i-1].first)
indices.insert(make_pair(dictionary[i].first, i));
}
while(gets(line)) {
string s(line);
if(s.compare("XXXXXX") == 0)
break;
ul encoded = encode(line);
//cerr << "CMP " << s << ", size " << s.size() << ", encoding: " << encoded << endl;
bool output = false;
if(indices.find(encoded) != indices.end()) {
int i = indices[encoded];
while(i < size && dictionary[i].first == encoded) {
if(dictionary[i].second.size() == s.size()) {
output = true;
cout << dictionary[i].second << endl;
}
++i;
}
}
if(!output)
cout << "NOT A VALID WORD" << endl;
cout << "******" << endl;
}
return 0;
}