Skip to content

Commit a9cf3dd

Browse files
committed
update ch5
1 parent b98af12 commit a9cf3dd

File tree

14 files changed

+580
-10
lines changed

14 files changed

+580
-10
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ Working directory: youroot/algs4cplusplus/ch1
112112

113113
## ch5. Strings
114114

115-
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
116-
| :---------------------------------------------------------: | :----------------------------------------------------------: | :------------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | :-------------------------------: |
117-
| [-](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 |
118-
| [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 |
119-
| [-](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 |
120-
| [-](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 |
121-
| [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 |
122-
| [5.5](https://algs4.cs.princeton.edu/52trie/index.php#5.5) | [TST.h](ch5/head/TrieSET.h) | ternary search trie | | | |
115+
| REF | PROGRAM | DESCRIPTION / C++DOC | REF | PROGRAM | DESCRIPTION / C++DOC |
116+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :-----------------------------------: |
117+
| [-](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 |
118+
| [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 |
119+
| [-](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 |
120+
| [-](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 |
121+
| [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 |
122+
| [5.5](https://algs4.cs.princeton.edu/52trie/index.php#5.5) | [TST.h](ch5/head/TrieSET.h) | ternary search trie | [5.6](https://algs4.cs.princeton.edu/53substring/index.php#5.6) | [KMP.h](ch5/head/KMP.h) | substring search (Knuth–Morris–Pratt) |
123+
| [5.7](https://algs4.cs.princeton.edu/53substring/index.php#5.7) | [BoyerMoore.h](ch5/head/BoyerMoore.h) | substring search (Boyer–Moore) | [5.8](https://algs4.cs.princeton.edu/53substring/index.php#5.8) | [RabinKarp.h](ch5/head/RabinKarp.h) | substring search (Rabin–Karp) |
124+
| [5.9](https://algs4.cs.princeton.edu/54regexp/index.php#5.9) | [NFA.h](ch5/head/NFA.h) | NFA for regular expressions | [-](https://algs4.cs.princeton.edu/54regexp/index.php#-) | [GREP.cpp](ch5/16_GREP/main.cpp) | grep |
125+
| [-](https://algs4.cs.princeton.edu/55compression/index.php#-) | [BinaryDump.cpp](ch5/17_BinaryDump/main.cpp) | binary dump | | | |
123126

ch5/10_TrieSET/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(10_TrieSET)
33

44
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h ../head/AmericanFlag.h ../head/AmericanFlagX.h ../head/KMP.h ../head/BoyerMoore.h ../head/RabinKarp.h)
6+
set(SOURCE_FILES main.cpp ../head/LSD.h ../head/MSD.h ../head/TST.h ../head/AmericanFlag.h ../head/AmericanFlagX.h ../head/KMP.h ../head/BoyerMoore.h ../head/RabinKarp.h ../head/NFA.h)
77
add_executable(10_TrieSET ${SOURCE_FILES})

ch5/15_NFA/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(15_NFA)
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(15_NFA ${SOURCE_FILES})

ch5/15_NFA/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "../head/NFA.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Unit tests the {@code NFA} data type.
8+
*
9+
* @param args the command-line arguments
10+
*/
11+
int main() {
12+
string regexp = "(A*B|AC)D";
13+
string txt = "AAAACD";
14+
15+
NFA nfa(regexp);
16+
cout << nfa.recognizes(txt) << endl;
17+
}

ch5/16_GREP/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(16_GREP)
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(16_GREP ${SOURCE_FILES})

ch5/16_GREP/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../head/NFA.h"
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Interprets the command-line argument as a regular expression
8+
* (supporting closure, binary or, parentheses, and wildcard)
9+
* reads in lines from standard input; writes to standard output
10+
* those lines that contain a substring matching the regular
11+
* expression.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("./data/tinyL.txt");
17+
string regexp = "(.*(A*B|AC)D.*)";
18+
string line;
19+
NFA nfa(regexp);
20+
while (file >> line) {
21+
if (nfa.recognizes(line))
22+
cout << line << endl;
23+
}
24+
}

ch5/17_BinaryDump/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(17_BinaryDump)
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(17_BinaryDump ${SOURCE_FILES})

ch5/17_BinaryDump/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <fstream>
3+
4+
using namespace std;
5+
6+
/**
7+
* Reads in a sequence of bytes from standard input and writes
8+
* them to standard output in binary, k bits per line,
9+
* where k is given as a command-line integer (defaults
10+
* to 16 if no integer is specified); also writes the number
11+
* of bits.
12+
*
13+
* @param args the command-line arguments
14+
*/
15+
int main() {
16+
ifstream file("./data/abra.txt", ios::binary);
17+
int bitsPerLine = 16;
18+
char tmp;
19+
int cnt = 0;
20+
while (file >> tmp) {
21+
for (int i = 7; i >= 0; --i) {
22+
cout << ((tmp >> i) & 1);
23+
cnt++;
24+
if (cnt % bitsPerLine == 0 && cnt != 0)cout << endl;
25+
}
26+
}
27+
if (bitsPerLine != 0) cout << endl;
28+
cout << cnt << " bits" << endl;
29+
}

ch5/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ add_subdirectory(11_TST)
1818
add_subdirectory(12_KMP)
1919
add_subdirectory(13_BoyerMoore)
2020
add_subdirectory(14_RabinKarp)
21+
add_subdirectory(15_NFA)
22+
add_subdirectory(16_GREP)
23+
add_subdirectory(17_BinaryDump)
2124

2225

23-
add_subdirectory(temp)
26+
# add_subdirectory(temp)

ch5/data/abra.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ABRACADABRA!

0 commit comments

Comments
 (0)