-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageMess.h
74 lines (59 loc) · 1.52 KB
/
MessageMess.h
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
#ifndef TOPCODER_MESSAGEMESS_H_
#define TOPCODER_MESSAGEMESS_H_
#include <unordered_set>
#include <list>
#include <string>
#include <vector>
class MessageMess
{
public:
std::string restore(std::vector<std::string> dictionary, std::string message);
private:
bool process(std::string message, std::string &ret);
std::unordered_set<std::string> dict;
};
std::string MessageMess::restore(std::vector<std::string> dictionary, std::string message)
{
dict.insert(dictionary.begin(), dictionary.end());
std::string result;
bool success = process(message, result);
if (success && result[result.size() - 1] == ' ')
{
result.erase(result.size() - 1);
}
if (!success)
{
result = "IMPOSSIBLE!";
}
else
{
int realcount = std::count(result.begin(), result.end(), ' ');
if (result.size() - realcount > message.size())
{
result = "AMBIGUOUS!";
}
}
return result;
}
inline bool MessageMess::process(std::string message, std::string &ret)
{
bool complete = false;
if (message.empty())
{
return true;
}
for (int i = 1; i <= message.size(); ++i)
{
if (dict.find(message.substr(0, i)) != dict.end())
{
bool temp = process(message.substr(i), ret);
complete = temp || complete;
if (complete)
{
ret = message.substr(0, i) + " " + ret;
}
}
}
return complete;
}
#endif TOPCODER_MESSAGEMESS_H_