forked from Dungeonlx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_word.cpp
98 lines (79 loc) · 2.55 KB
/
find_word.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
#include <iostream>
#include <fstream>
#include <vector>
#include <locale>
#include <algorithm>
#include <time.h>
namespace String
{
// string tolower
std::string ToLower(std::string str)
{
std::string ret = "";
std::locale loc;
for (std::string::size_type i = 0; i < str.size(); ++i)
ret += std::tolower(str[i], loc);
return ret;
}
bool CompareByLength(const std::string & l, const std::string & r)
{
if (l.size() < r.size())
return true;
return false;
}
};
namespace Time
{
void ToElapse(unsigned elapsed)
{
int sec = elapsed / CLOCKS_PER_SEC;
std::cout << "elapsed: " << sec << "." << elapsed - sec * CLOCKS_PER_SEC << std::endl;
}
};
class Dict
{
public:
Dict(const char* DictFileName, bool isToLower = true, bool SortByLength = true)
{
std::ifstream DictFile(DictFileName);
std::string strLine;
while (std::getline(DictFile, strLine))
{
if (isToLower)
m_WordList.push_back(String::ToLower(strLine));
else
m_WordList.push_back(strLine);
}
if (SortByLength)
{
std::sort(m_WordList.begin(), m_WordList.end(), String::CompareByLength);
std::reverse(m_WordList.begin(), m_WordList.end());
}
std::cout << m_WordList.size() << " words" << std::endl;
}
virtual ~Dict()
{
}
public:
// 4 DEBUG
void printWordList()
{
for (int i = 0; i < m_WordList.size(); i++)
std::cout << m_WordList[i] << std::endl;
}
private:
std::vector<std::string> m_WordList;
};
int main(int argc, char* argv[])
{
unsigned t0 = clock();
// START
//=========================================================================
Dict objDict("dict.txt");
//objDict.printWordList();
// END
//=========================================================================
unsigned elapsed = clock() - t0;
Time::ToElapse(elapsed);
return 0;
}