-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_note.cpp
58 lines (49 loc) · 1.15 KB
/
Random_note.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
#include <iostream>
using namespace std;
bool canConstruct_bruteforce(std::string note, std::string magazine)
{
if(note.size() > magazine.size())
return false;
for (int i = 0; i < note.size(); i++)
{
if (0 == magazine.size())
return false;
std::size_t found = magazine.find(note[i]);
if (found == std::string::npos)
return false;
magazine.erase(found, 1);
}
return true;
}
bool canConstruct_map(std::string note, std::string magazine)
{
if(note.size() > magazine.size())
return false;
typedef std::map TypeDict;
TypeDict dict;
for (size_t i = 0; i < magazine.size(); i++)
{
TypeDict::iterator itDict = dict.find(magazine[i]);
if (itDict == dict.end())
dict.emplace(magazine[i], 1);
else
itDict->second++;
}
for (size_t i = 0; i < note.size(); i++)
{
TypeDict::iterator itDict = dict.find(note[i]);
if (itDict == dict.end())
return false;
else if(--itDict->second < 0)
return false;
}
return true;
}
int main()
{
std::string note = "aabac";
std::string magazine = "aabacd";
std::cout << canConstruct_bruteforce(note, magazine) << std::endl;
std::cout << canConstruct_map(note, magazine) << std::endl;
return 0;
}