Skip to content

Commit e14dc17

Browse files
committed
update ch5
1 parent ed9f8e4 commit e14dc17

File tree

22 files changed

+1637
-5
lines changed

22 files changed

+1637
-5
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ Algorithms, 4th edition textbook code (using c++)
9797

9898
## ch5. Strings
9999

100-
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
101-
| :---------------------------------------------------------: | :----------------------------------------------------------: | :----------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | :------------------: |
102-
| [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Alphabet.java](https://algs4.cs.princeton.edu/50strings/Alphabet.java.html) | alphabet | [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Count.java](https://algs4.cs.princeton.edu/50strings/Count.java.html) | alphabet client |
103-
| [5.1](https://algs4.cs.princeton.edu/51radix/index.php#5.1) | [LSD.h](ch5/head/LSD.h) | LSD radix sort | [5.2](https://algs4.cs.princeton.edu/51radix/index.php#5.2) | [MSD.h](ch5/head/MSD.h) | MSD radix sort |
104-
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [InplaceMSD.h](ch5/head/InplaceMSD.h) | In-place MSD radix sort1 | | | |
100+
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
101+
| :---------------------------------------------------------: | :----------------------------------------------------------: | :------------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: |
102+
| [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Alphabet.java](https://algs4.cs.princeton.edu/50strings/Alphabet.java.html) | alphabet | [-](https://algs4.cs.princeton.edu/50strings/index.php#-) | [Count.java](https://algs4.cs.princeton.edu/50strings/Count.java.html) | alphabet client |
103+
| [5.1](https://algs4.cs.princeton.edu/51radix/index.php#5.1) | [LSD.h](ch5/head/LSD.h) | LSD radix sort | [5.2](https://algs4.cs.princeton.edu/51radix/index.php#5.2) | [MSD.h](ch5/head/MSD.h) | MSD radix sort |
104+
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [InplaceMSD.h](ch5/head/InplaceMSD.h) | In-place MSD radix sort1 | [5.3](https://algs4.cs.princeton.edu/51radix/index.php#5.3) | [Quick3string.h](ch5/head/Quick3string.h) | 3-way string quicksort |
105+
| [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlag.h](ch5/head/AmericanFlag.h) | American flag sort1 | [-](https://algs4.cs.princeton.edu/51radix/index.php#-) | [AmericanFlagX.h](ch5/head/AmericanFlagX.h) | non-recursive American flag sort1 |
106+
| [5.4](https://algs4.cs.princeton.edu/52trie/index.php#5.4) | [TrieST.h](ch5/head/TrieST.h) | multiway trie symbol table | [-](https://algs4.cs.princeton.edu/52trie/index.php#-) | [TrieSET.h](ch5/head/TrieSET.h) | multiway trie set |
107+
| [5.5](https://algs4.cs.princeton.edu/52trie/index.php#5.5) | [TST.h](ch5/head/TrieSET.h) | ternary search trie | | | |
105108

ch5/10_TrieSET/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(10_TrieSET)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h ../head/AmericanFlag.h ../head/AmericanFlagX.h)
7+
add_executable(10_TrieSET ${SOURCE_FILES})

ch5/10_TrieSET/main.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "../head/TrieSET.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Unit tests the {@code TrieSET} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shellsST.txt";
14+
fstream in(file);
15+
string tmp;
16+
TrieSET st;
17+
vector<string> a;
18+
while (in >> tmp)
19+
st.add(tmp);
20+
21+
// print results
22+
if (st.size() < 100) {
23+
cout << "keys(\"\"):" << endl;
24+
auto res = st.keys();
25+
while (!res.empty()) {
26+
cout << res.front() << endl;
27+
res.pop();
28+
}
29+
cout << endl;
30+
}
31+
32+
cout << "longestPrefixOf(\"shellsort\"):" << endl;
33+
cout << st.longestPrefixOf("shellsort") << endl;
34+
cout << endl;
35+
36+
cout << "longestPrefixOf(\"quicksort\"):" << endl;
37+
cout << st.longestPrefixOf("quicksort") << endl;
38+
cout << endl;
39+
40+
cout << "keysWithPrefix(\"shor\"):" << endl;
41+
auto res = st.keysWithPrefix("shor");
42+
while (!res.empty()) {
43+
cout << res.front() << endl;
44+
res.pop();
45+
}
46+
cout << endl;
47+
48+
cout << "keysThatMatch(\".he.l.\"):" << endl;
49+
auto res2 = st.keysThatMatch(".he.l.");
50+
while (!res2.empty()) {
51+
cout << res2.front() << endl;
52+
res2.pop();
53+
}
54+
cout << endl;
55+
}

ch5/11_TST/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(11_TST)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h)
7+
add_executable(11_TST ${SOURCE_FILES})

ch5/11_TST/main.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "../head/TST.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Unit tests the {@code TST} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shellsST.txt";
14+
fstream in(file);
15+
string tmp;
16+
TST<int> st;
17+
int cnt = 0;
18+
vector<string> a;
19+
while (in >> tmp)
20+
st.put(tmp, cnt++);
21+
22+
// print results
23+
if (st.size() < 100) {
24+
cout << "keys(\"\"):" << endl;
25+
auto res = st.keys();
26+
while (!res.empty()) {
27+
cout << res.front() << endl;
28+
res.pop();
29+
}
30+
cout << endl;
31+
}
32+
33+
cout << "longestPrefixOf(\"shellsort\"):" << endl;
34+
cout << st.longestPrefixOf("shellsort") << endl;
35+
cout << endl;
36+
37+
cout << "longestPrefixOf(\"quicksort\"):" << endl;
38+
cout << st.longestPrefixOf("quicksort") << endl;
39+
cout << endl;
40+
41+
cout << "keysWithPrefix(\"shor\"):" << endl;
42+
auto res = st.keysWithPrefix("shor");
43+
while (!res.empty()) {
44+
cout << res.front() << endl;
45+
res.pop();
46+
}
47+
cout << endl;
48+
49+
cout << "keysThatMatch(\".he.l.\"):" << endl;
50+
auto res2 = st.keysThatMatch(".he.l.");
51+
while (!res2.empty()) {
52+
cout << res2.front() << endl;
53+
res2.pop();
54+
}
55+
cout << endl;
56+
}

ch5/6_Quick3string/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(6_Quick3string)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(6_Quick3string ${SOURCE_FILES})

ch5/6_Quick3string/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/Quick3string.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of fixed-length strings from standard input;
9+
* 3-way radix quicksorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shells.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
Quick3string::sort(a);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/7_AmericanFlag/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(7_AmericanFlag)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(7_AmericanFlag ${SOURCE_FILES})

ch5/7_AmericanFlag/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/AmericanFlag.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of extended ASCII strings or non-negative ints from standard input;
9+
* American flag sorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments: "int" to read input as non-negative integers
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shells.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
AmericanFlag::sort(a);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/8_AmericanFlagX/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(8_AmericanFlagX)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(8_AmericanFlagX ${SOURCE_FILES})

ch5/8_AmericanFlagX/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../head/AmericanFlagX.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Reads in a sequence of extended ASCII strings or non-negative ints from standard input;
9+
* American flag sorts them;
10+
* and prints them to standard output in ascending order.
11+
*
12+
* @param args the command-line arguments: "int" to read input as non-negative integers
13+
*/
14+
int main() {
15+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shells.txt";
16+
fstream in(file);
17+
string tmp;
18+
vector<string> a;
19+
while (in >> tmp)
20+
a.push_back(tmp);
21+
22+
int n = a.size();
23+
int w = a[0].length();
24+
25+
// sort the strings
26+
AmericanFlagX::sort(a);
27+
28+
// print results
29+
for (int i = 0; i < n; ++i)
30+
cout << a[i] << endl;
31+
}

ch5/9_TrieST/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(9_TrieST)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h)
7+
add_executable(9_TrieST ${SOURCE_FILES})

ch5/9_TrieST/main.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "../head/TrieST.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
/**
8+
* Unit tests the {@code TrieST} data type.
9+
*
10+
* @param args the command-line arguments
11+
*/
12+
int main() {
13+
string file = "/home/ace/AceDev/C++/algorithm/ch5/data/shellsST.txt";
14+
fstream in(file);
15+
string tmp;
16+
TrieST<int> st;
17+
vector<string> a;
18+
int cnt = 0;
19+
while (in >> tmp)
20+
st.put(tmp, cnt++);
21+
22+
// print results
23+
if (st.size() < 100) {
24+
cout << "keys(\"\"):" << endl;
25+
auto res = st.keys();
26+
while (!res.empty()) {
27+
cout << res.front() << " " << st.get(res.front()) << endl;
28+
res.pop();
29+
}
30+
cout << endl;
31+
}
32+
33+
cout << "longestPrefixOf(\"shellsort\"):" << endl;
34+
cout << st.longestPrefixOf("shellsort") << endl;
35+
cout << endl;
36+
37+
cout << "longestPrefixOf(\"quicksort\"):" << endl;
38+
cout << st.longestPrefixOf("quicksort") << endl;
39+
cout << endl;
40+
41+
cout << "keysWithPrefix(\"shor\"):" << endl;
42+
auto res = st.keysWithPrefix("shor");
43+
while (!res.empty()) {
44+
cout << res.front() << endl;
45+
res.pop();
46+
}
47+
cout << endl;
48+
49+
cout << "keysThatMatch(\".he.l.\"):" << endl;
50+
auto res2 = st.keysThatMatch(".he.l.");
51+
while (!res2.empty()) {
52+
cout << res2.front() << endl;
53+
res2.pop();
54+
}
55+
cout << endl;
56+
}

ch5/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@ include_directories(data)
99
add_subdirectory(3_LSD)
1010
add_subdirectory(4_MSD)
1111
add_subdirectory(5_InplaceMSD)
12+
add_subdirectory(6_Quick3string)
13+
add_subdirectory(7_AmericanFlag)
14+
add_subdirectory(8_AmericanFlagX)
15+
add_subdirectory(9_TrieST)
16+
add_subdirectory(10_TrieSET)
17+
add_subdirectory(11_TST)
1218

19+
20+
add_subdirectory(temp)

ch5/data/shellsST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
she sells sea shells by the sea shore

0 commit comments

Comments
 (0)