diff --git a/912v1.0.tex b/912v1.0.tex deleted file mode 100644 index f915285..0000000 --- a/912v1.0.tex +++ /dev/null @@ -1,102 +0,0 @@ -\documentclass[UTF8,12pt]{ctexart} -\usepackage{ctex} -\usepackage{amsmath} -\usepackage{graphicx} -\CTEXsetup[format={\Large\bfseries}]{section} -\title{\kaishu 912回忆版} -\author{by Shine Wong} -\date{12/22} - -\begin{document} -\maketitle - - \section{数据结构} - - \subsection{判断题} - - \begin{itemize} - - \item[1)]$log^nn = \Omega(n^{logn})$。 - \item[2)]对一棵AVL树进行插入,则至多会引起$\Omega(logn)$次局部调整操作。 - \item[3)]对一个理想随机输入的序列进行快速排序,则在平均情况下以及最坏情况下都可以达到$O(logn)$的时间复杂度性能。 - \item[4)]在理想随机输入的情况下,尽管完全二叉堆的删除操作的最坏时间复杂度有$O(logn)$,平均时间复杂度仅为$O(1)$而已。 - \item[5)]跳转表每一个节点所对应的塔的平均高度为$O(logn)$。\\ - \item[6)]采用基于比较的算法,可以在$O(n)$的时间内找出序列的前10\%大的元素。 - \item[7)]对一有向图进行DFS,共有$k$条边被标记为 BACKWARD,则该图中未必有$k$个环路。\\ - \item[8)]败者树相对于胜者树,具有更优的渐进时间复杂度性能。\\ - \item[9)]相对于闭散列,开散列可以更好地利用数据的局部性。\\ - \item[10)]...remain to be added - - - \end{itemize} - - \subsection{单向选择题} - - \begin{itemize} - - \item[1)]对一有向无环图,该图的拓扑排序序列恰好是DFS的$\underline{\hbox to 10mm{}}$\\ - A.\ 被发现的顺序\\ - B.\ 被发现的逆序\\ - C.\ 回溯的顺序\\ - D.\ 回溯的逆序 - - \item[2)]如果基数排序底层采用不稳定的排序算法,则所得的结果$\underline{\hbox to 10mm{}}$,并且基数排序的稳定性$\underline{\hbox to 10mm{}}$\\ - A.\ 不再正确 \ 不再保持\\ - B.\ 不再正确 \ 仍然保持\\ - C.\ 仍然正确 \ 不再保持\\ - D.\ 仍然正确 \ 仍然保持 - - \item[3)]逆波兰表达式$Blalala$的结果为2019,则中间缺失的操作符为\\ - A \ + \\ - B \ - \\ - C \ * \\ - D \ / \\ - E \ \^ \\ - F \ ! - - \item[4)]对于一个权重分别是1,1,2,3,5,8,13,21的字符集构造Huffman编码树,其中最大的深度为\\ - A.\ 6\\ - B.\ 7\\ - C.\ 8\\ - D.\ 9 - - \item[5)]含有$\underline{\hbox to 10mm{}}$个节点的真二叉树的数量,与2019对括号构成的合法表达式数量相同。\\ - A.\ 1009\\ - B.\ 1010\\ - C.\ 2019\\ - D.\ 4039 - - \item[6)]对一模式串HHBFHHBFHHBFSHH,考虑改进的next数组,则$next[14] - next[0] = \underline{\hbox to 10mm{}}$\\ - A.\ 2\\ - B.\ 3\\ - C.\ 4\\ - D.\ 5 - - \end{itemize} - - \subsection{证明题} - - 已知一棵二叉搜索树的先序和后序遍历序列,是否可以构造出它的层次遍历序列?是则给出证明,否则给出一个反例。(5分) - - \subsection{程序设计题} - - 给出二叉树节点BinNode的定义如下:\\ - - \noindent class BinNode{\\ - public:\\ - \indent BinNode* parent;\\ - \indent BinNode* lc;\\ - \indent BinNode* rc;\\ - \indent int lsize;\\ - - \indent BinNode* zig(BinNode* x);//绕当前节点顺时针旋转,仍然返回旋转后根节点的左子树\\ - \indent BinNode* zag(BinNode* x);//绕当前节点顺时针旋转,仍然返回旋转后根节点的右子树\\ - } - - \begin{itemize} - - \item[] - - \end{itemize} - -\end{document} diff --git a/c++ note/chp10/chapter 10.cpp b/c++ note/chp10/chapter 10.cpp deleted file mode 100644 index 8b627b8..0000000 --- a/c++ note/chp10/chapter 10.cpp +++ /dev/null @@ -1,144 +0,0 @@ -//Exercise 10.2 -#include -#include -#include -#include -#define NUM 10 -using namespace std; -int main(){ - list strList; - string tmp; - for (int ix = 0; ix != NUM; ix++) { - cin >> tmp; - strList.push_back(tmp); - } - string val; - cin >> val; - cout << count(strList.begin(), strList.end(), val) << endl; - system("pause"); - return 0; -} - -//Exercise 10.3 -#include -#include -#include -using namespace std; -int main(){ - vector intVec{ 5,4,3,1,2 }; - cout << accumulate(intVec.cbegin(), intVec.cend(), 0) << endl; - system("pause"); - return 0; -} - -//Exercise 10.6 -#include -#include -#include -#include -#define NUM 10 -using namespace std; -int main(){ - vector intVec; - fill_n(back_inserter(intVec), 10, 0); - for (auto It = intVec.cbegin(); It != intVec.cend(); It++) - cout << *It << ' '; - cout << endl; - system("pause"); - return 0; -} - -//Exercise 10.9 -#include -#include -#include -#include -using namespace std; -void print(vector strVec); -int main(){ - vector strVec; - string word; - while (cin >> word) - strVec.push_back(word); - print(strVec); - sort(strVec.begin(), strVec.end()); - print(strVec); - auto unique_end = unique(strVec.begin(), strVec.end()); - print(strVec); - strVec.erase(unique_end, strVec.end()); - print(strVec); - - system("pause"); - return 0; -} - -void print(vector strVec){ - if(!strVec.empty()){ - for (auto It = strVec.cbegin(); It != strVec.cend(); It++) - cout << *It << ' '; - } - cout << endl; -} - -//Exercise 10.13 -#include -#include -#include -#include -using namespace std; -void print(vector strVec); -bool have_five_char(string word){ - return word.size() >= 5; -} -int main(){ - vector words; - string word; - while (cin >> word) - words.push_back(word); - auto end = partition(words.begin(), words.end(), have_five_char); - print(words); - words.erase(end, words.end()); - print(words); - system("pause"); - return 0; -} -void print(vector strVec) { - if (!strVec.empty()) { - for (auto It = strVec.cbegin(); It != strVec.cend(); It++) - cout << *It << ' '; - } - cout << endl; -} - -//Exercise 10.14 -auto func = [](int a, int b){return a+b;}; - -//Exercise 10.15 -int a = 42; -auto func = [a](int b){return a+b;}; - -//Exercise 10.20 -#include -#include -#include -#include -#define LENGTH 6 -using namespace std; -int main(){ - vector strVec; - string word; - while (cin >> word) - strVec.push_back(word); - auto count = count_if(strVec.cbegin(), strVec.cend(), [](string str) {return str.size() > LENGTH; }); - cout << count << endl; - system("pause"); - return 0; -} - -//Exercise 10.21 -auto func = [arg]() mutable ->bool{ - if(arg == 0) return True; - //else - while(arg-- != 0); - return False; -} \ No newline at end of file diff --git a/c++ note/chp13/chp13.cpp b/c++ note/chp13/chp13.cpp deleted file mode 100644 index 9d5bf30..0000000 --- a/c++ note/chp13/chp13.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; - -class HasPtr { -public: - explicit HasPtr(const std::string &s = std::string()) : - ps(new std::string(s)), i(0) {} - - //copy constructor - HasPtr(const HasPtr& ptr); - - //copy assignment operator - HasPtr& operator=(const HasPtr& ptr); - - //destructor - ~HasPtr(){ - delete ps; - } -private: - std::string *ps; - int i; -}; - - -HasPtr::HasPtr(const HasPtr& ptr){ - ps = new string(*ptr.ps); - i = ptr.i; -} - -HasPtr& HasPtr::operator=(const HasPtr& ptr){ - delete ps; - ps = new string(*ptr.ps); - i = ptr.i; - return *this; -} - -HasPtr f(HasPtr fp){ - HasPtr ret = fp; - return ret; -} - -int main(){ - HasPtr ptr1("2020"); - HasPtr ptr2("2019"); - HasPtr ptr3 = ptr2; - forward_list li({ 4, 5, 6 }); - array arr = { 1 }; - arr = { 1, 2, 3, 4}; - arr.assign(1); - arr.fill(4); - for (auto e : arr) - cout << e << " "; - cout << endl; - - for (int e : li) - cout << e << " "; - ptr2 = ptr1; - ptr3 = ptr1; - f(ptr1); - system("pause"); - return 0; -} diff --git a/c++ note/chp13/chp13.md b/c++ note/chp13/chp13.md deleted file mode 100644 index 509f8b9..0000000 --- a/c++ note/chp13/chp13.md +++ /dev/null @@ -1,317 +0,0 @@ -Constructor and Copy Control -============================ - -## 目录 - -+ 构造函数 - - 初始化列表 - - 委派构造函数 - - 转换构造函数 -+ 拷贝控制 - - 拷贝构造函数 - - 拷贝赋值运算符 - - 析构函数以及三者之间的联系 - -## 构造函数 - -### 初始化列表 - -初始化列表(Constructor Initializer List)其实是我比较常用的,但是我之前的理解有很多误区,这里对它进行一些深入的讨论。 - -初始化列表的功能与在构造函数体内直接赋值是没有任何区别的,比如说下面用初始化列表的代码: - -```cpp -Sales_data::Sales_data(const string &s, unsigned int cnt, double price): bookNo(s), units_sold(cnt), revenue(cnt * price){} -``` - -与 - -```cpp -Sales_data::Sales_data(const string &s, unsigned int cnt, double price){ - bookNo = s; - units_sold = cnt; - revenue = price * cnt; -} -``` - -在功能上没有任何区别。我之前也是认为两者完全等价,甚至还更加偏向于第二种写法,因为感觉格式要好看一点。实际上,两者在性能上是不相同的。 - -构造函数的执行顺序,是首先执行初始化列表,继而执行构造函数体内的赋值语句。需要注意的是,即使初始化列表为空,编译器仍然会首先对各个成员变量进行默认的初始化操作,因此相对于第一种版本,第二种版本不仅没有省下执行初始化列表的时间,还增加了函数体内的变量赋值的开销。如果成员变量是一个较大的类类型的话,两者的时间开销差别还是蛮大的。 - -因此,应该尽量使用初始化列表来对成员变量进行初始化操作,这样可以获得更优的性能。实际上,在一些情况下,必须使用初始化列表。 - -对于某一些类型的变量,比如常量变量(const),或者(左值)引用变量,必须在声明的时候同时对它们初始化。如果一个类含有这些成员变量,则必须使用初始化列表对它们进行初始化。此外,如果一个成员变量是没有默认构造函数的类类型,也必须使用初始化列表对它进行初始化。从这里应该可以看出,尽管在多数情况下都可以忽略,但是初始化和赋值其实是两种不同的操作。 - -> 初始化列表的执行顺序问题。 - -初始化列表的执行顺序并非是按列表中变量的先后次序,而是按照变量在类定义的次序。因此,下面的代码是存在问题的: - -```cpp -class X { -private: - int i; - int j; -public: - // undefined: i is initialized before j - X(int val): j(val), i(j) { } -}; -``` - -其中,初始化列表中`i`首先被初始化为`j`,然后再对`j`进行初始化,但是对`i`进行初始化时`j`还是未定义的,就有可能导致后续的错误。 - -基于上面的讨论,初始化列表的次序最好与成员变量的定义次序相同,并且最好不要用一个成员变量去初始化另一个成员变量,这样就可以避免可能的错误。比如说下面的代码就不存在初始化列表的执行次序问题了: - -```cpp -X(int val): j(val), i(val){} -``` - -### 委派构造函数 - -在我写代码的时候,也经常存在这样的需要,即一个构造函数是另一个更加详细的构造函数的子集,或者部分操作。这个时候我就不想全部重新写一遍,看起来也不好看,最好的办法就是在后者中调用前者,这就是委派构造函数(delegating constructor)——这个名字应该是指将一部分工作委派给另一个构造函数完成。 - -委派构造函数的语法也很简单,只需要在`:`后面,本来初始化列表的位置调用被委派的构造函数即可。被委派的构造函数(delegated constructor)的初始化列表和函数体都会相继被执行,之后再执行委派构造函数(delegating constructor)的剩余部分。下面的代码就是委派构造函数的一个例子: - -```cpp -class Sales_data { -public: - // nondelegating constructor initializes members from corresponding arguments - Sales_data(std::string s, unsigned cnt, double price): bookNo(s), units_sold(cnt), revenue(cnt*price) {} - - // remaining constructors all delegate to another constructor - Sales_data(): Sales_data("", 0, 0) {} - Sales_data(std::string s): Sales_data(s, 0,0) {} - Sales_data(std::istream &is): Sales_data(){ read(is, *this); } -}; -``` - -### 转化构造函数 - -转化构造函数是一类特殊的构造函数,它只含有一个参数,参数的类型可以是类本身(此时就是拷贝构造函数了,将在后面提到),也可以是其他类型。它的特殊性在于,转化构造函数定义了隐式的类型转化方法,即从参数的类型转化到当前的类类型。 - -比如说我可以定义一个`Mystring`类型,它基本沿用`std::string`的基本方法,但是增加一个构造函数,将输入的整型转化为对应的字符串,如下: - -```cpp -class Mystring{ -public: - string str; - - Mystring() = default; - //converting constructor - Mystring(int num): str(to_string(num)){} -} -``` - -这样,在任何期望一个`Mystring`类型变量的位置,都可以传入一个`int`类型,编译器会自动调用上面定义的转换构造函数,从该`int`类型构造出一个`Mystring`类对象。 - -例如存在一个外部函数,接收一个`Mystring`类型的对象作为参数,打印出其中的字符串信息: - -```cpp -void print(Mystring mystr){ - cout << mystr.str << endl; -} - -print(2020);//this call is perfectly legal -``` - -完全可以对该函数传入一个整型的变量,此时编译器会自动调用转换构造函数,从该整型变量构造出一个`Mystring`类型变量,作为函数的参数。实际上,在`Mystring`对象初始化的时候,还可以采用下面的方式: - -```cpp -Mystring mystr = 2020;//copy initialization -``` - -这种初始化方式称为拷贝初始化(copy initialization),与直接初始化存在一些区别。在上面的语句中,编译器也会调用转化构造函数,因此该语句与 - -```cpp -Mystring mystr(2020);//direct initialization -``` - -这样的直接初始化完全等效。与上面类似,甚至可以直接使用`int`型变量对`Mystring`对象进行赋值。 - -从上面也可以看出,转化构造函数是存在一些隐患的,比如说`Mystring str = 2020;`这样的语句看起来多少让人看起来有一些迷惑。为了避免这种歧义性,可以手动避免转化构造函数的隐式类型转化,只需要在转化构造函数的声明前面添加`explicit`关键字即可。 - -```cpp -class Mystring{ -public: - string str; - explicit Mystring(int num): str(to_string(num)){} - //other functions -``` - -这样,就可以避免隐式类型转化了。如果需要将`int`转化为`Mystring`类型,就必须显式调用(explicit)转化构造函数才行。实际上,之前我已经多次遇到转化构造函数了,只是我当时并不知道而已。 - -例如`std::string`存在一个将`const char*`转化为`string`的构造函数,因此存在下面的`string`初始化与函数调用语句: - -```cpp -string str = "Study hard tomorrow."; //legal -int num = stoi("2020"); //legal, implicit convert "2020" to string. -``` - -同理,`vector`类中存在一个构造函数,可以指定`vector`的初始大小,传入的参数是一个`int`变量。但是并不可以这样初始化`vector`: - -```cpp -vector iVec = 2020; //illegal, this constructor is explicit -vector iVec(2020); //legal, explicit initialization -``` - -用一个整型变量给`vector`赋值,这看起来也太怪了,存在不少的歧义性,因此这个构造函数被标记为`explicit`了。 - -需要注意的是,`explicit`关键字只能出现在类构造函数声明的位置,如果该构造函数在类声明体外部被定义,则不能再次添加`explicit`关键字了。 - -```cpp -//error: explicit allowed only on a constructor declaration in a class header -explicit Mystring::Mystring(int num){ - ... -} -``` - -## 拷贝控制 - -拷贝控制(copy control)是指一个类控制它的对象在拷贝,移动,赋值,析构操作下所进行的活动。其中包含五个函数(操作),即拷贝构造函数(copy constructor),拷贝赋值运算符(copy assignment operator),移动构造函数(move constructor),移动赋值运算符(move assignment operator)以及析构函数(destructor)。下面将对它们分别进行讨论。 - -### 拷贝构造函数 - -拷贝构造函数是一类特殊的构造函数,它的参数为同类对象的一个引用,该引用通常是一个常量(const)引用,尽管非常量也是可以的。 - -```cpp -class Mystring{ -public: - string str; - - //default constructor - Mystring() = default; - //copy constructor - Mystring(const Mystring& right); - //converting constructor - Mystring(int num); -} -``` - -顾名思义,拷贝构造函数的作用,就是在对象被拷贝初始化(copy initialization)的时候被调用,它规定了新的对象应该如何从被拷贝对象进行复制: - -```cpp -Mystring str1; //default initialization -Mystring str2 = str1; //copy initialization, call copy constructor -``` - -如果不自己定义拷贝构造函数,那么编译器会自动添加一个默认拷贝构造函数(synthesized copy constructor),它的函数体是,将类的所有可拷贝成员,逐个复制到新的对象当中,即浅拷贝(shallow copy)。 - -因此,如果一个类的成员变量包含指针类型,默认拷贝构造函数就可以导致隐患的错误——将一个对象的指针简单拷贝到另一个对象,两个指针指向同一个变量,如果一个对象释放了该指针,则另一个对象也将不再能访问该变量。在这种情况下,就有必要手动定义拷贝构造函数,为目标指针分配一个新的存储空间: - -```cpp -class HasPtr{ -private: - std::string *ps; - int i; -public: - HasPtr() = default; - //copy constructor - HasPtr(const HasPtr& hp): ps(new string(*hp.ps)), i(hp.i){} -} -``` - -在上面的例子中,就为目标对象的指针重新分配了存储空间,即深拷贝(deep copy),从而避免了默认拷贝构造函数可能存在的问题。需要注意的是,这里直接访问了另一个对象`hp`的私有成员变量,这是因为同一类的对象互为友元(friend),因此可以相互访问对象的私有变量。 - -下面以一个例子,来说明拷贝初始化过程中发生的操作: - -```cpp -std::string str = "Study hard tomorrow."; -``` - -这个例子在前面转换构造函数的部分已经提过了,它会首先调用转换构造函数,将`const char*`类型的`"Study hard tomorrow"`转换为`std::string`类型,继而再调用拷贝构造函数,拷贝初始化`str`。 - -这里的两次构造函数调用看起来多少有些冗余,实际上,在上面这种情况下,一些编译器可能会省略掉(omit)拷贝构造函数的调用,将上面的拷贝初始化优化为直接初始化: - -```cpp -std::string str("Study hard tomorrow."); -``` - -此时就只会调用一次转换构造函数了。 - -实际上,除了拷贝初始化以外,拷贝构造函数还在多个场合会被隐式调用: - -+ 函数调用的参数为对象的非引用类型(non-reference type)。此时为了将实参转化为形参传入函数内部,编译器会自动调用拷贝构造函数构造一个新的对象(形参)。 -+ 函数调用的返回值为对象的非引用类型。此时并不能简单返回函数体内的局部变量,因为函数执行完毕后所有局部变量都将被回收,因此编译器会自动调用拷贝构造函数复制出一个新的对象作为返回结果。 -+ 初始化数组等复合类型时,将被拷贝的数组的每个对象逐个复制到新的数组当中,此时对拷贝的每个对象编译器都会自动调用拷贝构造函数。 -+ std的容器进行拷贝初始化的时候,对复制的每一个对象都自动调用拷贝构造函数。 - -由于拷贝构造函数有这么多隐式调用的场合,所以绝对不可以用`explicit`关键字去修饰拷贝构造函数。 - -现在也可以说明为什么拷贝构造函数的参数必须是同类对象的一个`引用`了。如果拷贝构造函数的参数是非引用类型,在进行拷贝构造时,对于传入的非引用类型的实参(即被拷贝的对象),需要调用拷贝构造函数构造出一个新的形参,这个过程就将不断持续下去,无法终止。 - -### 拷贝赋值运算符 - -顾名思义,拷贝赋值运算符在对象被赋值时被调用。和拷贝构造函数一样,如果用户没有定义拷贝赋值运算符,编译器将添加默认的拷贝赋值运算符(synthesized copy assignment operator),并且该默认的运算符也是进行浅拷贝。 - -因此,如果类含有类型为指针的成员变量,则往往需要自己定义拷贝复制运算符。这里需要注意的是,拷贝赋值运算符的参数通常也是同类对象的常量引用`const reference type`,尽管不是引用类型也是可以的。在这种情况下,进行赋值操作时,编译器会首先调用拷贝构造函数构造出新的形参,随后再执行拷贝赋值运算符内的操作。 - -由于c++内置类型的赋值操作,其返回值都是左侧操作数的引用。为了保持与内置类型的一致性,自己定义的拷贝赋值运算符也应该返回左侧操作数的引用。 - -```cpp -HasPtr& HasPtr::operator=(const HasPtr& hp){ - delete ps; //free ps first - ps = new string(*hp.ps); - i = hp.i; - return *this; //return a reference to left-hand operand -} -``` - -可以看到,在拷贝赋值运算符中,应该首先释放掉原有指针的空间,防止内存泄露。 - -### 析构函数以及三者之间的关系 - -关于析构函数我觉得我还是挺熟悉的,这里就不做详细的说明了。 - -和上面两个函数一样,如果用户没有自己定义析构函数,编译器将自动为类添加一个默认析构函数(synthesized destructor),它的函数体是空的,如下: - -```cpp -HasPtr{ -private: - ... -public: - //other member functions - - ~HasPtr(){} //default destructor -} -``` - -但这并不是说默认析构函数什么工作都没有进行。实际上,析构函数体并不直接释放对象的成员变量,对象的成员变量是在执行完析构函数体之后隐式被释放的。因此析构函数体主要进行的应该是指针成员变量的释放,释放动态分配的内存空间。 - -下面重点讲解拷贝构造函数,拷贝赋值运算符以及析构函数三者的关系(the rule of three)。 - -通常说来,我们并不需要手动定义全部的三个函数,而是视情况定义部分函数就可以了。这里存在一些经验规则: - -> 定义了析构函数的类往往需要同时定义拷贝构造函数和拷贝赋值运算符。 - -仍然以上面的`HasPtr`类为例,假设它只定义了析构函数: - -```cpp -HasPtr{ -private: - string *ps; - int i; -public: - ~HasPtr(){ - delete ps; - } - //other member functions -} -``` - -析构函数体内释放`ps`指向的内存空间,随后成员变量`ps`和`i`被隐式回收。考虑一种情况,存在一个外部函数`f`,它的函数体如下所示: - -```cpp -HasPtr f(HasPtr hp){ - HasPtr ret = hp; - return ret; -} -``` - -如果传入一个`HasPtr`类型的对象,在实参形参转换时会第一次调用拷贝构造函数,浅拷贝出形参`hp`;在`ret = hp`处调用拷贝赋值运算符,浅拷贝出`ret`;函数返回时,会第二次调用拷贝构造函数,随后两个局部变量`hp`和`ret`都会被销毁。需要注意的是,这里所有对象,他们的成员变量`ps`都指向同一个内存区域,因此在销毁过程中,该内存区域会被释放两次,这是一个内存错误。 - -此外,这样一次调用后,原来的实参指向的`string`将失效,因为该区域已经被释放了。从这个例子可以看出,如果定义了析构函数,往往还需要手动定义拷贝构造函数和拷贝赋值运算符。 - -> 拷贝构造函数和拷贝赋值运算符往往需要同时被定义,或者同时不被定义。 - -换言之,如果定义了一个,往往还需要定义另一个。但是定义了拷贝构造函数和拷贝赋值运算符的类并不一定需要定义析构函数。 - -作为总结,理解这里三个函数的关键,在于把握它们各自的调用时机,即在什么语句中,参数的形式是什么样的时候会被调用,这样就可以避免不少潜在的错误。 diff --git a/c++ note/chp13/chp13_exercises.md b/c++ note/chp13/chp13_exercises.md deleted file mode 100644 index 7ab826b..0000000 --- a/c++ note/chp13/chp13_exercises.md +++ /dev/null @@ -1,80 +0,0 @@ -CPP Primer Chp13 Exercises -========================== - -> Exercise 13.1: What is a copy constuctor? When is it used? - -拷贝构造函数(copy constructor)是在用拷贝的方式初始化对象的时候,被调用的构造函数,它的形参必须是`const T&`类型,其中`T`为当前类的类名。它会在三种情形下被使用: - -+ 拷贝初始化(copy initialization)。比如说 - -```cpp -myClass object1();//default construction -myClass object2 = object1;//copy initialization -``` - -+ 函数的形参是一个对象而非对象的引用。此时需要调用拷贝构造函数,从实参拷贝构造一个局部的形参传递到函数体的内部。 -+ 函数的返回值是一个对象而非对象的引用。由于函数内部的变量具有临时性,函数返回后就会被销毁,因此需要拷贝构造一个新的对象传递给函数的调用者。 - -> Exercise 13.2: Explain why the following declaration is illegal: - -```cpp -Sales_data::Sales_data(Sales_data rhs); -``` - -拷贝构造函数的参数必须是对对象的引用。这是因为拷贝构造函数的一个作用,就是在函数的参数为对象的非引用类型时,将实参拷贝构造到形参。如果如上面的声明,对于传入的`rhs`的实参,由于并非是引用类型,需要调用拷贝构造函数将其转化为形参,这个过程将一直持续下去,无法终止。 - -> Exercise 13.4: Assuming Point is a class type with a public copy constructor, identify each use of the copy constructor in this program fragment: - -```cpp -Point global; -Point foo_bar(Point arg) -{ - Point local = arg, *heap = new Point(global); - *heap = local; - Point pa[4] = { local, *heap }; - return *heap; -} -``` - -0. `arg`形参并非引用类型,参数传递对应了第一次调用。 -1. `Point local = arg`。拷贝初始化(copy initialization)时调用拷贝构造函数。 -2. `heap = new Point(global)`。显式调用拷贝构造函数。 -3. `Point pa[4] = { local, *heap };`这里有两次调用。 -4. `return *heap;`返回值并非引用类型,需要调用拷贝构造函数。 - -> Given the following sketch of a class, write a copy constructor that copies all the members. Your constructor should dynamically allocate a new string and copy the object to which ps points, rather than copying ps itself. - -```cpp -class HasPtr{ -public: - HasPtr(const std::string &s = std::string()): - ps(new std::string(s)), i(0){} -private: - std::string *ps; - int i; -}; -``` - -```cpp -//copy constructor -HasPtr::HasPtr(const HasPtr& ptr){ - ps = new string(*ptr.ps); - i = ptr.i; -} -``` - -> Exercise 13.6: What is a copy-assignment operator? When is this operator used? What does the synthesized copy-assignment operator do? When is it synthesized? - -拷贝赋值运算符是一个函数,用于将形参对象的值拷贝到当前对象中;当需要对对象进行赋值时,应该使用拷贝复制运算符;默认的拷贝赋值运算符将传入对象的所有非静态(nonstatic)成员拷贝目标对象当中,可以看出这是一个浅拷贝(shallow copy),当成员变量有指针类型时就会出现问题;如果用户没有定义自己的拷贝赋值运算符,编译器就会生成默认的拷贝赋值运算符。 - -> Exercise 13.8: Write the assignment operator for the HasPtr class from exercise 13.5 in. As with the copy constructor, your -assignment operator should copy the object to which ps points. - -```cpp -HasPtr& HasPtr::operator=(const HasPtr& ptr){ - delete ps; - ps = new string(*ptr.ps) - i = ptr.i; - return *this; -} -``` diff --git a/c++ note/chp7/chp7answers.md b/c++ note/chp7/chp7answers.md deleted file mode 100644 index e0a9845..0000000 --- a/c++ note/chp7/chp7answers.md +++ /dev/null @@ -1,174 +0,0 @@ -Solutions to Exercises in Chapter Seven -======================================= - -> Exercise 7.1: Write a version of the transaction-processing program from §1.6 (p. 24) using the Sales_data class you defined for the exercises in §2.6.1 (p. 72). - -[exercise7_1.cpp](exercise7_1.cpp) - -> Exercise 7.2: Add the combine and isbn members to the Sales_data class you wrote for the exercises in § 2.6.2 (p. 76). - -[exercise7_2.h](exercise7_2.h) - -> Exercise 7.3: Revise your transaction-processing program from § 7.1.1 (p.256) to use these members. - -[exercsie7_3.cpp](exercise7_3.cpp) - -> Exercise 7.4: Write a class named Person that represents the name and address of a person. Use a string to hold each of these elements. Subsequent exercises will incrementally add features to this class. - -```cpp -#ifndef EXERCISE7_4_H_ -#define EXERCISE7_4_H_ - -#include -using std::string; - -struct Person{ - string name; - string address; -}; - -#endif -``` - -> Exercise 7.5: Provide operations in your Person class to return the name and address. Should these functions be const? Explain your choice. - -```cpp -#ifndef EXERCISE7_4_H_ -#define EXERCISE7_4_H_ - -#include -using std::string; - -struct Person{ - string name; - string address; - string getName() const { return name; } - string getAddr() const { return address; } -}; - -#endif -``` - -These two functions should be `const`. Because these two functions do not modify the object's members, and `const` objects of this class should be able to call these two functions. - -> Exercise 7.6: Define your own versions of the add, read, and print functions. - -It seems I have done this back in Exercise 7.1. - -> Exercise 7.7: Rewrite the transaction-processing program you wrote for the exercises in § 7.1.2 (p. 260) to use these new functions. - -It seems I have done this back in Exercise 7.1. - -> Exercise 7.8: Why does `read` define its Sales_data parameter as a plain reference and `print` define its parameter as a reference to const? - -Because in `read`, the `Sales_data` object will be modified. While in `print`, this function do not modify the object. - -> Exercise 7.9: Add operations to `read` and `print` Person objects to the code you wrote for the exercises in § 7.1.2 (p. 260). - -```cpp -istream& read(istream &is, Person &person){ - is >> person.name >> person.address; - return is; -} - -ostream& print(ostream &os, Person const &person){ - os << person.name << ' ' << person.address; - return os; -} -``` - -> Exercise 7.10: What does the condition in the following if statement do? -`if (read(read(cin, data1), data2))` - -Take in two objects(data1 and data2) at the same time, and step into the `if` block only when both objects are valid. - -> Exercise 7.11: Add constructors to your Sales_data class and write a program to use each of the constructors. - -[exercise7_11.h](exercise7_11.h) - -[exercise7_11.cpp](exercise7_11.cpp) - -> Exercise 7.12: Move the definition of the Sales_data constructor that takes an istream into the body of the Sales_data class. - -```cpp -struct Sales_data { - string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; - - //constructors - Sales_data() = default; - Sales_data(istream &is){ - double price; - is << bookNo << units_sold << price; - revenue = price * units_sold; - } - Sales_data(string isbn) : bookNo(isbn) {}; - Sales_data(string isbn, unsigned n, double price) : bookNo(isbn), units_sold(n), revenue(n * price) {}; - - string isbn() const { return bookNo; } - double avg_price() const { return revenue / units_sold; } - Sales_data& combine(Sales_data const &data); //behave same as the `+=` operator, thus return a left-value -}; -``` - -> Exercise 7.13: Rewrite the program from page 255 to use the istream constructor. - -```cpp -int main(){ - Sales_data total(cin); // variable to hold the running sum - if (cin) { // read the first transaction - Sales_data trans(cin); // variable to hold data for the next transaction - while (cin) { // read the remaining transactions - if (total.isbn() == trans.isbn()) // check the isbns - total.combine(trans); // update the running total - else { - print(cout, total) << endl; // print the results - total = trans; // process the next book - } - trans = Sales_data(cin); - } - print(cout, total) << endl; // print the last transaction - } - else { // there was no input - cerr << "No data?!" << endl; // notify the user - } - - system("pause"); - return 0; -} -``` - -This can be weird, for the `cin` operation is seperated from the `if-condition`. - -> Exercise 7.14: Write a version of the default constructor that explicitly initializes the members to the values we have provided as in-class initializers. - -```cpp -struct Sales_data { - string bookNo; - unsigned units_sold; - double revenue; - - //constructors - Sales_data(){units_sold = 0, revenue = 0.0;} - Sales_data(istream &is){ - double price; - is << bookNo << units_sold << price; - revenue = price * units_sold; - } - Sales_data(string isbn) : bookNo(isbn) {}; - Sales_data(string isbn, unsigned n, double price) : bookNo(isbn), units_sold(n), revenue(n * price) {}; - - string isbn() const { return bookNo; } - double avg_price() const { return revenue / units_sold; } - Sales_data& combine(Sales_data const &data); //behave same as the `+=` operator, thus return a left-value -}; -``` - -But there's a drawback in this manner. For example, in constructor3, `Sales_data(string isbn)`, we also have to explicitly initializes all the members, which causes redundance. - -> Exercise 7.15: Add appropriate constructors to your Person class. - -[exercise7_15.h](exercise7_15.h) - -[exercise7_15.cpp](exercise7_15.cpp) diff --git a/c++ note/chp7/chp7conclusion.md b/c++ note/chp7/chp7conclusion.md deleted file mode 100644 index 5879a3d..0000000 --- a/c++ note/chp7/chp7conclusion.md +++ /dev/null @@ -1,55 +0,0 @@ -Conlusions on Chapter Seven -=========================== - -## 7.1 Defining Abstract Data Type - -> 什么是抽象数据类型? - -主要可以参考邓公的网课和教材,基本概念和邓公讲的是一致的。抽象数据类型的关键是两个概念,即封装和数据抽象。为此,需要弄清楚`interface`和`implementation`之间的区别与联系。 - -### 7.1.2 Defining Member Functions - -> 首先思考一个问题,当我们调用类的成员函数的时候,如`cppstring Sales_data::isbn() const { return bookNo;}`成员函数是怎么找到当前对象的`bookNo`成员变量的呢? - -`this`指针。成员函数实际上是有一个隐性的变量,即`this`。当调用成员函数的时候,`this`指针会被初始化为指向被调用对象的地址。比如说,当调用 - -```cpp -total.isbn(); -``` - -的时候,编译器将`this`指针设置为`total`的地址。这就好像`python`一样,所以这个成员函数的调用可以写成 - -```cpp -Sales_data::isbn(&total); -``` - -这样,利用`this`指针,就可以随机索引当前对象的任何一个成员变量了。 - -> 可以看到,上面的`isbn`成员函数有一个修饰符`const`,这个`const`修饰符有什么作用? - -这个可以有两个理解方法。 - -+ 从语言的功能层面,一个`const`的对象只能调用它的标记为`const`的成员函数。这是很自然的,因为编译器会认为没有`const`标记的成员函数会修改对象的成员变量。所以,对于只读的成员函数,尽量都标记为`const`,以便其`const`的对象也可以调用这些函数。 - -+ 从变量类型的方面。对于一般的对象,如`Sales_data total`这种,`this`指针的类型为`Sales_data* const`,即`this`指针是`const pointer to non-const object`,在这种情况下,`this`指针是不能指向`const`的对象的。为了指定`this`指针的类型为`const Sales_data* const`,即`const pointer to const object`,就需要在函数的参数列表和具体实现之间加上`const`修饰符。实际上,这也是`const`对象只能调用`const`成员函数在语言层面的原因。 - -- class scope and defining a member function outside the class -- a function to return `this` object - -### 7.1.3 defining nonmember class-related function -- read and print function. io classes - -### 7.1.4 constructors -- constructors cannot be `const` -- synthesized default constructor and in-class intializers -- classes with members of built-in or compound types should not rely on the synthesized default constructor - + so we must define the default constructor for three reasons -- =default -- constructor initializer list -- Defining a Constructor outside the Class Body - -### 7.1.5. Copy, Assignment, and Destruction -- Synthesized Versions -- cases then Synthesized Versions cannot be used - + classes that allocate resources that reside outside the class objects themselves - + vertor and string members diff --git a/c++ note/chp7/exercise7_1.cpp b/c++ note/chp7/exercise7_1.cpp deleted file mode 100644 index e3d3012..0000000 --- a/c++ note/chp7/exercise7_1.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -using namespace std; - -struct Sales_data{ - string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; - - string isbn() const { return bookNo; } - void combine(Sales_data const &data){ - units_sold += data.units_sold; - revenue += data.revenue; - } -}; - -Sales_data add(Sales_data const &one, Sales_data const &two){ - Sales_data res; - res.bookNo = one.bookNo; - res.units_sold = one.units_sold + two.units_sold; - res.revenue = one.revenue + two.revenue; - return res; -} - -istream& read(istream &is, Sales_data &data){ - is >> data.bookNo; - is >> data.units_sold; - is >> data.revenue; - return is; -} - -ostream& print(ostream &os, Sales_data const &data){ - os << "book number: " << data.bookNo << endl; - os << "units sold: " << data.units_sold << endl; - os << "total revenue: " << data.revenue << endl; - return os; -} - -int main(){ - Sales_data total; // variable to hold the running sum - if (read(cin, total)) { // read the first transaction - Sales_data trans; // variable to hold data for the next transaction - while (read(cin, trans)) { // read the remaining transactions - if (total.isbn() == trans.isbn()) // check the isbns - total.combine(trans); // update the running total - else { - print(cout, total) << endl; // print the results - total = trans; // process the next book - } - } - print(cout, total) << endl; // print the last transaction - } - else { // there was no input - cerr << "No data?!" << endl; // notify the user - } - - system("pause"); - return 0; -} - diff --git a/c++ note/chp7/exercise7_11.cpp b/c++ note/chp7/exercise7_11.cpp deleted file mode 100644 index dfacb96..0000000 --- a/c++ note/chp7/exercise7_11.cpp +++ /dev/null @@ -1,43 +0,0 @@ -//test constructors -#include "exercise7_11.h" -#include - -using std::cin; -using std::cout; -using std::cerr; - -void test_constructor(); - -int main(){ - test_constructor(); - - system("pause"); - return 0; -} - -void test_constructor(){ - //test default constructor - Sales_data data1; - assert(data1.units_sold == 0); - assert(data1.revenue == 0.0); - - //test constructor2 - Sales_data data2(cin); // cin >> 123456789 2 20.5 - assert(data2.bookNo == "123456789"); - assert(data2.revenue == 41.0); - assert(data2.units_sold == 2); - - //test constructor3 - Sales_data data3("123456789"); - assert(data3.bookNo == "123456789"); - assert(data3.revenue == 0.0); - assert(data3.units_sold == 0); - - //test constructor4 - Sales_data data4("123456789", 2, 20.5); - assert(data4.bookNo == "123456789"); - assert(data4.revenue == 41.0); - assert(data4.units_sold == 2); - - cout << "All tests passed." << endl; -} diff --git a/c++ note/chp7/exercise7_11.h b/c++ note/chp7/exercise7_11.h deleted file mode 100644 index 87723a3..0000000 --- a/c++ note/chp7/exercise7_11.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef EXERCISE7_11_H_ -#define EXERCISE7_11_H_ - -#include -#include - -using std::istream; -using std::ostream; -using std::endl; -using std::string; - -struct Sales_data { - string bookNo; - unsigned units_sold; - double revenue; - - //constructors - Sales_data() { units_sold = 0, revenue = 0.0; } - Sales_data(istream &is); - Sales_data(string isbn) : bookNo(isbn) {}; - Sales_data(string isbn, unsigned n, double price) : bookNo(isbn), units_sold(n), revenue(n * price) {}; - - string isbn() const { return bookNo; } - double avg_price() const { return revenue / units_sold; } - Sales_data& combine(Sales_data const &data); //behave same as the `+=` operator, thus return a left-value -}; - -//constructors -Sales_data::Sales_data(istream &is){ - double price; - is >> bookNo >> units_sold >> price; - revenue = price * units_sold; -} - -Sales_data& Sales_data::combine(Sales_data const &data) { - units_sold += data.units_sold; - revenue += data.revenue; - return *this; -} - -Sales_data add(Sales_data const &one, Sales_data const &two) { - Sales_data res = one; - return res.combine(two); -} - -istream& read(istream &is, Sales_data &data) { - is >> data.bookNo; - is >> data.units_sold; - is >> data.revenue; - return is; -} - -ostream& print(ostream &os, Sales_data const data) { - os << "book number: " << data.bookNo << endl; - os << "units sold: " << data.units_sold << endl; - os << "total revenue: " << data.revenue << endl; - os << "averge price: " << data.avg_price(); // no endl at the end - return os; -} - -#endif diff --git a/c++ note/chp7/exercise7_15.cpp b/c++ note/chp7/exercise7_15.cpp deleted file mode 100644 index da121c7..0000000 --- a/c++ note/chp7/exercise7_15.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// test construtors -#include "exercise7_15.h" -#include - -using std::cout; -using std::cin; -using std::endl; - -int main(){ - //test constructorss - Person person1; - // here we see non-builtin type(such as string) would not have undefined value - assert(person1.getAddr() == string()); - assert(person1.getName() == string()); - - Person person2("shine", "shanghai"); - // and string can directly be compare with literal character string - assert(person2.getName() == "shine"); - assert(person2.getAddr() == string("shanghai")); - - Person person3(cin); // cin >> shine >> shanghai; - assert(person2.getName() == "shine"); - assert(person2.getAddr() == string("shanghai")); - - cout << "All tests passed." << endl; - system("pause"); - return 0; -} diff --git a/c++ note/chp7/exercise7_15.h b/c++ note/chp7/exercise7_15.h deleted file mode 100644 index 091b83f..0000000 --- a/c++ note/chp7/exercise7_15.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef EXERCISE7_15_H_ -#define EXERCISE7_15_H_ - -#include -#include -using std::string; -using std::istream; -using std::ostream; - -struct Person; - -istream& read(istream &is, Person &person); -ostream& print(ostream &os, Person const &person); - -struct Person { - string name; - string address; - - //construtors - Person() = default; - Person(istream& is) { read(is, *this); } - Person(string name, string address) : name(name), address(address) {}; - - string getName() const { return name; } - string getAddr() const { return address; } -}; - -istream& read(istream &is, Person &person) { - is >> person.name >> person.address; - return is; -} - -ostream& print(ostream &os, Person const &person) { - os << person.name << ' ' << person.address; - return os; -} - -#endif diff --git a/c++ note/chp7/exercise7_2.h b/c++ note/chp7/exercise7_2.h deleted file mode 100644 index 147e50d..0000000 --- a/c++ note/chp7/exercise7_2.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef EXERCISE7_2_H_ -#define EXERCISE7_2_H_ - -#include -#include -using namespace std; - -struct Sales_data { - string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; - - string isbn() const { return bookNo; } - double avg_price() const { return revenue / units_sold; } - Sales_data& combine(Sales_data const &data); //behave same as the `+=` operator, thus return a left-value -}; - -Sales_data& Sales_data::combine(Sales_data const &data){ - units_sold += data.units_sold; - revenue += data.revenue; - return *this; -} - -Sales_data add(Sales_data const &one, Sales_data const &two) { - Sales_data res = one; - return res.combine(two); -} - -istream& read(istream &is, Sales_data &data) { - is >> data.bookNo; - is >> data.units_sold; - is >> data.revenue; - return is; -} - -ostream& print(ostream &os, Sales_data const data) { - os << "book number: " << data.bookNo << endl; - os << "units sold: " << data.units_sold << endl; - os << "total revenue: " << data.revenue << endl; - os << "averge price: " << data.avg_price(); // no endl at the end - return os; -} - -#endif diff --git a/c++ note/chp7/exercise7_3.cpp b/c++ note/chp7/exercise7_3.cpp deleted file mode 100644 index 4d6333a..0000000 --- a/c++ note/chp7/exercise7_3.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "exercise7_2.h" - -int main() { - Sales_data total; // variable to hold the running sum - if (read(cin, total)) { // read the first transaction - Sales_data trans; // variable to hold data for the next transaction - while (read(cin, trans)) { // read the remaining transactions - if (total.isbn() == trans.isbn()) // check the isbns - total = total.combine(trans); // update the running total - else { - print(cout, total) << endl; // print the results - total = trans; // process the next book - } - } - print(cout, total) << endl; // print the last transaction - } - else { // there was no input - cerr << "No data?!" << endl; // notify the user - } - - system("pause"); - return 0; -} diff --git a/c++ note/chp7/exercise7_4.h b/c++ note/chp7/exercise7_4.h deleted file mode 100644 index 9a5fb6f..0000000 --- a/c++ note/chp7/exercise7_4.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef EXERCISE7_4_H_ -#define EXERCISE7_4_H_ - -#include -#include -using std::string; -using std::istream; -using std::ostream; - -struct Person{ - string name; - string address; - string getName() const { return name; } - string getAddr() const { return address; } -}; - -istream& read(istream &is, Person &person){ - is >> person.name >> person.address; - return is; -} - -ostream& print(ostream &os, Person const &person){ - os << person.name << ' ' << person.address; - return os; -} - -#endif diff --git a/c++ note/chp9/chapter 9.cpp b/c++ note/chp9/chapter 9.cpp deleted file mode 100644 index 533537b..0000000 --- a/c++ note/chp9/chapter 9.cpp +++ /dev/null @@ -1,500 +0,0 @@ -#include -#include - -//Exercise 9.2 -std::list > List; - -//Exercise 9.4 -using std::vector; -bool find(vector::iterator front, vector::iterator behind, int target){ - for(;front::iterator find(vector::iterator front, vector::iterator behind, int target){ - for(;front while(iter1 != iter2)/* ... */ -_list DO NOT support `<` operator_ - -//Exercise 9.18 -#include -#include -#include -using namespace std; -int main(){ - deque strdq; - string currStr; - while(cin >> currStr){ - strdq.push_back(currStr); - } - deque::iterator dqIt; - for(dqIt = strdq.begin(); dqIt != strdq.end(); dqIt++){ - cout << *dqIt; - } - return 0; -} - -//Exercise 9.19 -#include -#include -#include -using namespace std; -int main() { - list strdq; - string currStr; - while (cin >> currStr) { - strdq.push_back(currStr); - } - list::const_iterator dqIt; - for (dqIt = strdq.cbegin(); dqIt != strdq.cend(); dqIt++) { - cout << *dqIt << endl; - } - - system("pause"); - return 0; -} - -//Exercise 9.20 -#include -#include -#include -#include -using namespace std; -int main(){ - list iList = {8,9,5,3,1,2,6,4,7,0}; - deque evendq, odddq; - for(auto listIt = iList.cbegin(); listIt != iList.cend(); listIt++){ - //if odd - if(*listIt % 2) odddq.push_back(*listIt); - else evendq.push_back(*listIt); - } - - for(auto dqIt = odddq.cbegin(); dqIt != odddq.cend(); cout << *dqIt << ' ', dqIt++); - cout << endl; - for(auto dqIt = evendq.cbegin(); dqIt != evnedq.cend(); cout << *dqIt << ' ', dqIt++); - cout << endl; - - return 0; -} - -//Exercise 9.24 -#include -#include -#include -using namespace std; -int main(){ - vector emptyVector; - try { - //cout << *emptyVector.begin() << endl; - //cout << emptyVector.front() << endl; - //cout << emptyVector[0] << endl; - cout << emptyVector.at(0) << endl; - }catch(out_of_range err){ - cout << err.what() << endl; - cout << "Press to exit" << endl; - system("pause"); - } - return 0; -} - -//Exercise 9.26 -#include -#include -#include -using namespace std; -int main(){ - int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; - vector ivec; - list ilst; - for(int i = 0; i != 11; i++){ - ivec.push_back(ia[i]); - ilst.push_back(ia[i]); - } - for(auto vIt = ivec.begin(); vIt != ivec.end();){ - if (*vIt % 2 == 0) vIt = ivec.erase(vIt); //even - else vIt += 1; - } - for(auto lIt = ilst.begin(); lIt != ilst.end();){ - if (*lIt % 2) lIt = ilst.erase(lIt); // odd - else lIt++; - } - cout << "result vector:" << endl; - for (auto It = ivec.begin(); It != ivec.end(); It++) - cout << *It << ' '; - cout << "\nresult list" << endl; - for (auto It = ilst.begin(); It != ilst.end(); It++) - cout << *It << ' '; - cout << endl; - system("pause"); - return 0; -} - -//Exercise 9.27 -#include -#include -#include -using namespace std; -int main(){ - forward_list ilst = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; - auto lIt = ilst.begin(); - auto prevlIt = ilst.before_begin(); - for(; lIt != ilst.end();){ - if (*lIt % 2) lIt = ilst.erase_after(prevlIt); // odd - else{ - lIt++, prevlIt++; - } - } - cout << "\nresult list" << endl; - for (auto It = ilst.begin(); It != ilst.end(); It++) - cout << *It << ' '; - cout << endl; - system("pause"); - return 0; -} - - -//Exercise 9.28 -#include -#include -#include -using namespace std; -forward_list insertStr(forward_list src, string pos, string destStr); -int main(){ - forward_list strLst = { "This", "is", "not", "TRUE", "happiness", "!" }; - auto resultLst = insertStr(strLst, string("is"), string("true")); - for (auto It = resultLst.cbegin(); It != resultLst.cend(); It++) - cout << *It << ' '; - cout << endl; - system("pause"); - return 0; -} - -forward_list insertStr(forward_list src, string pos, string destStr){ - auto It = src.begin(); - auto prev = src.before_begin(); - while(It != src.end()){ - if (*It == pos) { - src.insert_after(It, destStr); - break; - } - ++prev; - ++It; - } - if (It == src.end()) src.insert_after(prev, destStr); - return src; -} - -//Exercise 9.43 -#include -#include -using namespace std; -string strReplace(string s, string oldVal, string newVal); -string::iterator find(string &s, string dest, string::iterator start); -int main(){ - string src("thothothohelloworld"), oldVal("hell"), newVal("world"); - src = strReplace(src, oldVal, newVal); - cout << src << endl; - system("pause"); - return 0; -} -string strReplace(string s, string oldVal, string newVal){ - auto pos = s.begin(); - while (pos != s.end()) { - pos = find(s, oldVal, pos); - if (pos == s.end()) break; - pos = s.erase(pos, oldVal.size() + pos); - pos = s.insert(pos, newVal.begin(), newVal.end()); - pos += newVal.size(); - } - return s; -} -string::iterator find(string &s, string dest, string::iterator start){ - string::iterator pos = start, local, tmp; - for(; pos <= s.end() - dest.size(); pos++){ - tmp = dest.begin(); - local = pos; - while(*tmp == *local){ - tmp++, local++; - if (tmp == dest.end()) return pos; - } - } - return s.end(); -} - - -// Exercise 9.44 -#include -#include -using namespace std; -string strReplace(string s, string oldVal, string newVal); -int main(){ - string src("thothotho"), oldVal("tho"), newVal("though"); - src = strReplace(src, oldVal, newVal); - cout << src << endl; - system("pause"); - return 0; -} -string strReplace(string s, string oldVal, string newVal){ - string::size_type pos = 0; - while (pos != s.size()) { - pos = s.find(oldVal, pos); - if (pos == string::npos) break; - s.replace(pos, oldVal.size(), newVal); - pos += newVal.size(); - } - return s; -} - -//Exercise 9.45 -#include -#include -using namespace std; -string completeName(string name, string prefix, string suffix); -int main(){ - string name, prefix, suffix; - cin >> name >> prefix >> suffix; - string fullname = completeNam(name, prefix, suffix); - cout << fullname << endl; - system("pause"); - return 0; -} -string completeName(string name, string prefix, string suffix){ - name.insert(name.begin(), prefix.begin(), prefix.end()); - name.append(suffix); - return name; -} - -//Exercise 9.46: using index and length, and insert function only -string completeName(string name, string prefix, string suffix){ - name.insert(0, prefix); - name.insert(name.size(), suffix); - return name; -} - -//Exercise 9.47 -#include -#include -using namespace std; -int main(){ - string s("ab2c3d7R4E6"); - string numbers("0123456789"); - string::size_type pos = 0; - while((pos = s.find_first_of(numbers, pos)) != s.npos){ - cout << s[pos] << ' '; - pos++; - } - cout << endl; - pos = 0; - while ((pos = s.find_first_not_of(numbers, pos)) != s.npos) { - cout << s[pos] << ' '; - pos++; - } - cout << endl; - system("pause"); - return 0; -} - -//Exercise 9.49 -#include -#include -#include -using namespace std; -int main(){ - string ascender("bdfhjklt"), descender("gjpqy"); - string invalid(ascender.append(descender)); - vector text; - string word; - while(cin >> word){ - text.push_back(word); - } - string::size_type maxlen = 0; - vector::const_iterator pos; - for(auto It = text.cbegin();It != text.cend(); It++){ - if((*It).find_first_of(invalid) == (*It).npos){ - if ((*It).size() > maxlen) { - maxlen = (*It).size(); - pos = It; - } - } - } - cout << *pos << endl; - system("pause"); - return 0; -} - -//Exercise 9.50.a -#include -#include -#include -using namespace std; -int main() { - int N; - cin >> N; - vector s(N); - for (int ix = 0; ix != N; ix++) - cin >> s[ix]; - int sum = 0; - for (auto It = s.cbegin(); It != s.cend(); It++) { - sum += stoi(*It); - } - cout << sum << endl; - system("pause"); - return 0; -} - -//Exercise 9.50.b -#include -#include -#include -using namespace std; -int main() { - int N; - cin >> N; - vector s(N); - for (int ix = 0; ix != N; ix++) - cin >> s[ix]; - double sum = 0; - for (auto It = s.cbegin(); It != s.cend(); It++) { - sum += stod(*It); - } - cout << sum << endl; - system("pause"); - return 0; -} - -//Exercise 9.51 -#include -#include -#include -#include -#define NUM_OF_MONTH 12 -using namespace std; -class date{ -private: - unsigned year; - unsigned month; - unsigned day; - const char* months[12]{"January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December" }; - const char* alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" }; -public: - date(string strDate); - date(); - unsigned get_year(){ - return year; - } - //get_month();get_day(); - void print_date(int format = 0); -}; - -//default constructor -date::date(){ - year = 1998; - month = 1; - day = 1; -} - -//constructor taking a string -date::date(string strDate){ - string strMonth, strYear, strDay; - if (strDate.find(' ') != strDate.npos) { - strMonth = strDate.substr(0, strDate.find(' ')); - int ix; - try { - for (ix = 0; ix != NUM_OF_MONTH; ix++) { - if (!strMonth.compare(months[ix]) || !strMonth.compare(string(months[ix], 3))) { - month = ix + 1; - break; - } - } - if (ix == NUM_OF_MONTH) throw runtime_error("input error: invalid month"); - strYear = strDate.substr(strDate.size() - 4); - if (strYear.find_first_of(alphabet) != strYear.npos) throw runtime_error("input error: invalid year"); - else year = stoi(strYear); - strDay = strDate.substr(strDate.find(' ') + 1, strDate.size() - 6); - if (strDay.find_first_of(alphabet) != strDay.npos) throw runtime_error("input error: invalid day"); - else day = stoi(strDay); - } - catch(runtime_error err){ - cout << err.what() << endl; - cout << "continue or not(y/n): "; - char c; - cin >> c; - if (c == 'n' || c == 'N') exit(0); - } - }else{//format 1/1/1998 - try { - strMonth = strDate.substr(0, strDate.find('/')); - if (strMonth.find_first_of(alphabet) != strMonth.npos) throw runtime_error("input error: invalid month"); - else month = stoi(strMonth); - strYear = strDate.substr(strDate.size() - 4); - if (strYear.find_first_of(alphabet) != strYear.npos) throw runtime_error("input error: invalid year"); - else year = stoi(strYear); - strDay = strDate.substr(strDate.find('/') + 1, strDate.size() - 5); - if (strDay.find_first_of(alphabet) != strDay.npos) throw runtime_error("input error: invalid day"); - else day = stoi(strDay); - } - catch(runtime_error err){ - cout << err.what() << endl; - cout << "continue or not(y/n): "; - char c; - cin >> c; - if (c == 'n' || c == 'N') exit(0); - } - } -} - -//print current date with various formats -void date::print_date(int format){ - switch(format){ - default: - case 0: - cout << months[month-1] << ' ' << day << ", " << year << endl;; - break; - case 1: - cout << string(months[month-1], 3) << ' ' << day << ", " << year << endl; - break; - case 2: - cout << month << '/' << day << '/' << year << endl; - break; - } -} - -//Exercise 9.52 -#include -#include -#include -using namespace std; -int main(){ - string expr{ "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over" }; - const char repl = '#'; - stack charStack; - int seen = 0; - for(auto It = expr.cbegin(); It != expr.cend(); It++){ - charStack.push(*It); - if (*It == '(') seen += 1; - if(*It == ')' && seen > 0){ - seen -= 1; - while (charStack.top() != '(') charStack.pop(); - charStack.pop(); - charStack.push(repl); - } - } - //output the result - string output; - for (; !charStack.empty(); charStack.pop()) - output.insert(output.begin(), charStack.top()); - cout << output << endl; - system("pause"); - return 0; -} diff --git a/c++ note/chp9/chp9.md b/c++ note/chp9/chp9.md deleted file mode 100644 index e11dd11..0000000 --- a/c++ note/chp9/chp9.md +++ /dev/null @@ -1,310 +0,0 @@ -Conclusions on Sequential Containers -==================================== - -## 目录 -+ 综述 -+ 线性容器基本操作 - - 构造函数 - - 拷贝/赋值/交换 - - 插入与删除 - - 访问 - - 容量 - - 遍历 -+ string的额外操作 -+ 容器适配器 - - 栈 - - 队列 - - 优先队列 - -## 综述 - -这篇文章讨论c++标准库(stl)中的线性容器(`Sequential Container`),它们包括`vector`,`deque`,`list`,`forward_list`,`array`以及`string`。每个容器提供的成员函数之间具有一些共性,同时也会提供一些特殊的成员函数,理解它们的关键在于了解每个容器背后的基本原理(这应该是数据结构的基础知识),以及相关成员函数的设计思想。 - -六个线性容器又可以分为两个阵营,向量、双端队列、 string 和 array是第一个阵营,它们在物理上都是连续存储的;列表和单向列表是第二个阵营,它们是采用动态的存储策略。存储策略的不同会导致不同容器的静态操作与动态操作具有不同的性能,因此提供的成员函数也有区别,例如 vector 并不提供在容器的前端插入或者删除元素的成员函数`push_front`和`pop_front`,因为这将需要`O(n)`的时间复杂度。在后面还将详细地阐述这一点。 - -此外,六个容器中还有两朵奇葩,即 array 和单向列表。 array 是一个容量固定的容器,而所有其他容器都是可以动态扩容的,这决定了 array 的许多成员函数都与其他容器不同,例如不提供插入或者删除元素的接口;单向列表的设计思想是要保证最优的性能,因此单向列表削减了一些成员变量,比如单向列表不提供`size`接口,因为`O(1)`的`size`接口需要维护一个额外的 size 变量,在每次插入或者删除时都需要对该变量进行修改,这会在一定程度上降低性能。此外,由于单向列表结构的特殊性,它提供的插入和删除接口都与其他容器不同,这也会在后面详细阐述。 - -在实际应用中,对字符串的处理是相当常见的,为了使这部分工作变得简单,string 提供了大量额外的成员函数,本文将对其中比较常用的进行介绍。 - -最后,本文介绍了容器适配器的概念,并且就其中最常见的三个,即栈,队列,优先队列进行说明。 - -## 线性容器基本操作 - -任何一个线性容器都包含一些共有的基本操作,包括初始化,拷贝/赋值/交换,插入元素,删除元素,访问元素,遍历整个容器,以及获得容器的容量信息。下面将分别进行阐述。 - -### 初始化 - -线性容器提供了多种构造函数: - -+ 默认构造函数。 -+ 拷贝构造函数。从另一个**同类型**的容器进行拷贝。这里需要注意的是,对于 array ,它的容量也是它的类型,因此 array 只能从相同元素类型并且容量也相同的 array 进行拷贝构造。 - -```cpp -Container c1; -Container c2(c1); //c2 is a copy of c1 - -//for array type -array arr1; //arr1 has 6 elements with value 0 -array arr2 = arr1; //arr2 is a copy of arr1 -array arr3 = arr2; //error, different size -``` - -+ 指定容量的构造函数,该构造函数还可以指定每个元素的初始值。需要注意的是这个一个 `explicit` 的构造函数。显然 array 不提供该构造函数,否则就会有两个容量信息了。 - -```cpp -vector ivec1(10); //ivec1 has 10 elements valued 0 -vector ivec2 = 10; //error, explicit constructor -vector ive3(10, 5); //ivec3 has 10 elements valued 5 - -forward_list flist(5); -deque ddeque(20, 3.14); -``` - -+ 初始化列表(`initializer list`)。需要注意,对于 array ,列表中的元素个数不可以多于 array 的容量,当列表元素少于 array 的容量时,其余的元素被默认初始化,这其实和内置类型的数组是一致的。 - -```cpp -array arr1 = {1, 2, 3, 4, 5}; -array arr2 = {1, 2, 3, 4, 5, 6}; //error -array arr3 = {1}; //other elements are default initialized to 0 - -forward_list f_list = {1.2, 3.4, 5.6};//f_list has size 3 -deque ddeque = {5.0, 6.9}; //ddeque has size 2 -``` - -+ 通过两个**其他容器**的迭代器指定拷贝的范围。这种方法使得可以从其他类型的容器拷贝。array 是不支持这种方式的,因为指定的范围难以保证与 array 的容量一致。 - -```cpp -list ilist = {1, 2, 3, 4, 5}; -vector ivec(ilist.begin(), ilist.end()); //valid, ivec = {1, 2, 3, 4, 5} -deque ideque(ivec.begin() + 1, ivec.end());//valid, ideque = {2, 3, 4, 5} -``` - -### 拷贝/赋值/交换 - -stl中提供了三种拷贝/赋值的方式: - -+ 拷贝运算符(copy assignment operator)。和构造函数一样,这种方式也只能支持同类型容器之间的拷贝(或者赋值)。 - -```cpp -array arr1 = {42}; -array arr2 = {1, 2}; -array arr3; -arr2 = arr1; //valid, replace elements in arr2 -arr3 = arr2; //error, different size - -vector ivec1 = {1, 2, 3, 4, 5}; -vector ivec2(10, 1); -ivec2 = ivec1; //valid -``` - -+ `assign`成员函数。`assign`成员函数有多种重载形式,它们的参数其实就类似于对应的构造函数的参数,比如说可以通过两个迭代器来指定拷贝的范围,同样这实现了在不同类型容器之间的拷贝;可以传入一个初始化列表,也可以传入一个正整数代表 size,以及这些元素的值。显然,array 并不提供`assign`成员函数,但是有一个类似的`fill`函数,可以对所有元素赋值。 - -```cpp -vector ivec = {1, 2, 3, 4, 5}; -list ilist1(10, 1), ilist2, ilist3; -ilist1.assign(ivec.begin(), ivec.end()); //ilist1 now has 5 elements -ilsit2.assign({1, 2, 3, 4, 5}); //so does ilist2 -ilist3.assign(5, 1); //and ilist3 - -array arr = {1}; -arr.assign(ivec.begin(), ivec.end()); //error -arr.assign(5, 2); //error -arr.assign({1, 2, 3, 4, 5}); //error -arr.fill(5); //arr has 5 elements valued 5 -``` - -+ 通过一个初始化列表进行拷贝。对于 array 而言,初始化列表的长度不能超过 array 的容量,当列表元素少于容量时,其他元素被默认初始化。这与构造函数也是一致的。 - -```cpp -vector ivec(5, 1); -ivec = {2, 3, 4, 5}; //ivec has 4 elements - -array arr; -arr = {1, 2, 3 ,4, 5}; //valid -arr = {1, 2, 3, 4, 5, 6}; //error, to many elements -``` - -此外,stl 还提供了**交换**两个**同类型**容器的方法`swap`。对于除了 array 以外的其他容器,`swap`并不进行元素的拷贝,赋值或者移动等,而是简单地交换两个容器的指针而已,因此可以保证`O(1)`的时间复杂度。然而,交换两个 array 却要做实质的元素移动操作,因此交换 array 的时间复杂度为`O(n)`。 - -```cpp -vector svec1(10); -vector svec2(24); -swap(svec1, svec2); //swap two vectors in constant time -svec1.swap(svec2); //same as the above statement - -array arr1 = {1}, arr2 = {2}; -array arr3; -swap(arr1, arr2); //O(n) swap -swap(arr1, arr3); //error: different size -``` - -### 插入与删除元素 - -除 array 外的其他容器都提供了若干插入与删除元素的成员函数,而 array 由于它的定长特性,并不允许元素的插入与删除。除了单向列表以外,这些插入和删除的成员函数都有相同的接口形式,因而更加便于记忆与把握;而单向列表由于它结构上的特殊性,提供了独特的一套插入删除接口,也会在后面涉及到。 - -> `push_back`与`pop_back` - -`push_back`与`pop_back`的设计思想是提供一个高效地在容器后端插入或者删除元素的接口,它的时间复杂度仅为`O(1)`。显然,单向列表并不提供这一接口,因为在单向列表后端插入或删除元素需要`O(n)`的时间复杂度。另外四个容器,即向量,双端队列以及 string以及列表都提供这两个成员函数。 - -需要注意的是,`push_back`与`pop_back`都没有返回值。 - -> `push_front`与`pop_front` - -与`push_back`和`pop_back`类似,`push_front`与`pop_front`提供在`O(1)`时间内在容器的**前端**插入或者删除元素的接口。对于向量与 string 而言,在前端插入或者删除元素涉及到其他所有元素的移动,时间复杂度为`O(n)`,因而它们并不提供这一接口。双向队列,列表与单向列表都提供这两个成员函数。 - -这两个函数也没有返回值。 - -> `insert`与`erase` - -`insert`与`erase`是更加一般地插入或者删除元素的接口,可以在任意位置进行插入或者删除。 - -对于`insert`而言,有若干接口形式,所有这些接口的第一个参数都是一个迭代器指示的位置信息,表示元素应该被插入的位置。需要注意的是,元素实际上是被插入到该迭代器的前面一个位置,这样当传入的参数是`c.begin()`的时候就可以将新的元素插入到容器的最前端,传入的参数时`c.end()`的时候,将新的元素插入到容器的最后端。因此,`insert`函数,实际上是`insert_before`接口。 - -+ `insert(p, t)`:将新的元素`t`插入到位置`p`的前面。 -+ `insert(p, begin, end)`:将**另一容器**从迭代器`begin`到`end`的元素插入到位置`p`的前面。 -+ `insert(p, n, t)`:将n个相同的元素`t`插入到位置`p`的前面。 -+ `insert(p, il)`:将初始化列表`il`中的元素插入到位置`p`的前面。 - -需要注意的是,`insert`返回指向第一个被插入元素的迭代器,如果没有元素被插入的话(比如`begin = end`,则返回第一个参数。利用`insert`的返回值可以实现反复将新的元素插入到容器的最前端 - -```cpp -list slist; -auto it = slist.begin(); -while(cin >> word){ - it = slist.insert(it, word); -} -``` - -上面的代码与下面这个版本完全等效 - -```cpp -list slist; -while(cin >> word) - slist.push_front(word); -``` - -对于`erase`而言,也提供了两种接口: - -+ `erase(p)`:删除迭代器`p`指向的元素。 -+ `erase(begin, end)`:删除迭代器`begin`到`end`范围内的所有元素。 - -两种接口都返回指向被删除的最后一个元素的后一个位置的迭代器。除此以外,stl 还提供了`clear`成员函数,可以将所有元素清除,其效果等价于 - -```cpp -c.clear();//is equivalent to -c.erase(c.begin(), c.end()); -``` - -> `emplace`家族 - -除了`insert`家族以外,`emplace`家族也可以实现将元素插入到容器中的功能。与`insert`不同,`emplace`的作用并非将传入的元素拷贝到容器中,而是**构造**一个新的元素然后再插入到容器中,因此它的参数并非容器的`element_type`,而是`element_type`的构造函数的参数。`emplace`成员函数首先调用元素类型的构造函数,然后将新构造的元素插入到容器中。 - -与`insert`相同,`emplace`家族也提供了相似的三种成员函数: - -+ `emplace_back`:将新构造的元素插入到容器的尾部,显然单向列表并不提供该接口。 -+ `emplace_front`:将新构造的元素插入到容器的首部,显然向量和 string 不提供该接口。 -+ `emplace`:将新构造的元素插入到容器中的任何位置,与`insert`一样,该接口实际上是`emplace_before`。 - -`emplace`家族的参数与`insert`家族是类似的,只不过`emplace`并不支持同时插入多个元素,它们的返回值也是一样的,即指向被插入的(第一个)元素的迭代器。 - -> 单向列表的插入与删除 - -此前所提及的`insert`,`erase`以及`emplace`接口,都不被单向列表实现。这是由单向列表结构的特殊性决定的。 - -考虑一种情况,单向列表使用`insert(p, t)`插入元素`t`,为了将元素插入到迭代器`p`的前面,不得不从头遍历单向列表,找到迭代器`p`前面一个位置,然后才能调整指针完成实质的插入操作,该算法的时间复杂度为`O(n)`,而列表这种动态结构的插入与删除理论上是只需要`O(1)`的时间的,该性能显然不能令人满意。对于元素的删除也有同样的问题,因为将迭代器指示的元素释放后,还需要调整它前后的指针,为了找到被删除元素的前驱,也需要耗费`O(n)`的时间。 - -容易看到,上面操作非常低效,实质上是因为找到某个元素的前驱需要耗费大量的时间。因此单向列表定义了特殊的插入与删除成员函数,规避了这种无意义的查找,即`insert_after`,`erase_after`以及`emplace_after`。 - -对于`insert_after`而言,由于在插入到迭代器指示位置的后面,迭代器本身就是被插入元素的前驱,在`O(1)`时间内就能实现指针的修改。对于`erase_after`也是同样的道理,被删除元素的前驱就是传入的参数,因此可以在`O(1)`时间内完成指针的修改。 - -单向列表提供的插入与删除接口,与`insert`,`erase`完全相同: - -+ `insert_after(p, t)` -+ `insert_after(p, begin, end)` -+ `insert_after(p, n, t)` -+ `insert_after(p, it)` - -所有这些接口都返回指向被插入的**最后**一个元素的迭代器,需要注意`insert`是返回指向被插入的第一个元素的迭代器。如果没有元素被插入的话,仍然是返回`p`。 - -`erase_after`的两个接口 - -+ `erase_after(p)` -+ `erase_after(begin, end)`:将从`begin`之后的第一个元素,到(不包括)`end`之间的元素删除。 - -它们的返回值仍然是指向被删除元素的后一个元素的迭代器。 - -在这几个接口的语义下,会产生一些新的问题,比如如果想要插入到单向列表的最前面,应该怎么实现呢?因此单向列表还定义了特殊的迭代器`forward_list::before_begin`以及`forward_list::cbefore_begin`,利用这两个迭代器就可以插入或者删除最前面的元素了。 - -### 元素访问 - -对线性容器中的元素访问有四种不同的方式: - -+ `c.front()`:返回容器中第一个元素的**引用**; -+ `c.back()`:返回容器中最后一个元素的**引用**; -+ `c[n]`:返回容器中第 n 个元素的引用; -+ `c.at(n)`:返回容器中第 n 个元素的引用。 - -并非所有六个容器都支持这些方式,对这一点的理解还是要基于容器本身的结构。比如单向列表并不支持 `c.back()` 这种方式,因为需要 `O(n)` 的时间才能访问到最后一个元素;同理,不支持随机访问的结构,即单向列表与列表,都不支持 `c[n]`以及`c.at(n)` 这两个接口,因为这两个接口都是基于容器内元素的物理连续性,从而可以利用偏移量 n 快速计算出元素的地址。 - -### 容量 - -在前面已经提到过,除了 forward_list 以外,所有其他线性容器都支持复杂度为 `O(1)` 的 `size` 接口,返回当前容器的元素个数。此外,string 还有一个 `length` 成员函数,它的作用与 `size` 是完全相同的。 - -此外,所有线性容器都提供了 `resize(n)` 接口,使得容器含有 n 个元素。如果容器原先的元素个数大于 n,则将后面的元素删除;而如果原先的元素个数小于 n,新添加的元素被默认初始化。此外,还可以通过 `resize(n, t)` 使得新添加的元素被初始化为 `t`。 - -对于 vector 和 string 而言,需要更加精细的容量控制。这是因为 forward_list 与 list 都是采用非连续的存储策略,插入元素时只需要分配一个元素的存储空间即可;而 vector 和 string 中元素个数一旦超过了为其预先分配的空间,则不得不重新在内存中为它开辟一个更大的空间,并且将元素依次拷贝过去,整个过程的时间复杂度为`O(n)`。 - -因此,vector 和 string 需要同时管理 size 和 capacity 信息。两者都可以利用`c.reserve(n)`成员函数,预先在内存中开辟大小为 n (个元素)的空间,执行后有 `size = 0, capacity = n * sizeof(element_type)`,这样在插入 n 个元素前,就都不会进行自动扩容了。 - -需要注意的是,deque 是没有这样的问题的,因为它的底层结构并非完全连续的,而是由若干连续内存区段组成的,这些段之间并非连续。因此,deque 自动扩容时,只需分配一个区段的存储空间即可,并不会进行元素的拷贝,开销相对较小。 - - -### 遍历 - -遍历则相对简单。所有的线性容器(乃至关联容器)都提供了一组相同的接口: - -+ `c.begin()`, `c.end()`。 -+ `c.cbegin()`, `c.cend()`。 -+ `c.rbegin()`, `c.rend()`。用于反向遍历容器的场合,显然 forward_list 并不提供该接口。 -+ `c.crbegin()`, `c.crend()`。返回常量反向迭代器。 - -## string 的额外操作 - -stl 为 string 提供了若干额外的操作接口,在相当程度上简化了 string 的操作。 - -+ substr(begin, len) -+ extra constructor - - string str = "c style string"; - - string str -+ find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of - - find(pattern, pos) -+ append, replace -+ convert - - to_string - - stod(double) - - stoi(integer) -+ isdigit, isalpha, isalnum, iscontrl -+ tolower(c), toupper(c) - -## 容器适配器 - -栈和队列默认是使用 deque 实现的,而优先队列默认是使用 vector 来实现。三个容器适配器都支持的操作: - -+ push() -+ emplace() -+ pop() -+ size() -+ empty - -获取适配器中顶端(末端)元素 - -+ stack.top() -+ queue.front(), queue.back() -+ priority_queue.top(), priority_queue.front() - -实际上,`algorithm` 头文件中提供了一组与堆相关的函数,我感觉比 priority_queue 灵活很多: - -+ `make_heap(begin, end, cmp)`: 对一个 vector 进行建堆。 -+ `pop_heap(begin, end, cmp)`: 将堆顶元素与最后一个元素交换,然后对新的堆顶做下滤操作。堆的容量会减少1,但是 vector 的容量不变,需要实质地调用 c.pop_back() 才能删除原先的堆顶。 -+ `push_heap(begin, end, cmp)`:调用 c.push_back() 后调用该函数,对插入到尾部的元素进行上滤操作。 -+ `sort_heap(begin, end, cmp)`:堆排序,默认是升序。 diff --git a/c++ note/common_bugs.md b/c++ note/common_bugs.md deleted file mode 100644 index 36dc708..0000000 --- a/c++ note/common_bugs.md +++ /dev/null @@ -1,13 +0,0 @@ -Some Common Bugs -================ - -为了更好的debug,我觉得还是有必要把平时出现的有点难度的bug记录下来,以让自己知道你自己写的究竟是些啥??? - -> 在调用了构造函数,进行了必要的初始化,之后的操作却出现了空指针? - -因为我嵌套调用了构造函数。在构造函数中调用其他构造函数,只会产生一个匿名的对象,并不会帮助你初始化当前的对象。 - -> 写了一个`read`函数,接受一个`istream`以及一个对象作为参数,即`read(istream is, myClass object)`。在函数体中利用输入流将数据读入对象中,并返回该`istream`作为进一步的判断或者使用。结果`object`没有得到赋值?! - - -因为我是个傻逼。传入的`object`肯定要是引用啊。 \ No newline at end of file diff --git a/c++ note/exception.md b/c++ note/exception.md deleted file mode 100644 index a2761be..0000000 --- a/c++ note/exception.md +++ /dev/null @@ -1,38 +0,0 @@ -## exception - -+ 基本结构`try-catch` - - try{ - expression - }catch(errorType err){ - expression - }catch(errorType err){ - expression - }... -+ 文件支持 - - #include - //#include - using std::runtime_error -+ 具体使用方法 - - try block内放可能会产生异常的语句,并抛出(throw)相应的异常 - - try{ - if(...) throw runtime_error("fatal error: dividing zero"); - if(...) throw ... - //else - normal expression - } - - 在catch中检查可能会出现的异常,若异常与catch的异常类型对应,则进入相应的异常处理程序 - - try{ - ... - }catch(runtime_error err){ - cout << err.what() << endl;//err.what()返回抛出异常时初始化的字符串 - //other handling - cout << "continue or not(y/n): "; - char c; - cin >> c; - if(c == 'n' || c == 'N') break; - //else continue - } \ No newline at end of file diff --git a/ml/linear_regression/images/gd_diff_path.jpg b/ml/linear_regression/images/gd_diff_path.jpg deleted file mode 100644 index fe68626..0000000 Binary files a/ml/linear_regression/images/gd_diff_path.jpg and /dev/null differ diff --git a/ml/linear_regression/images/overfitting.jpeg b/ml/linear_regression/images/overfitting.jpeg deleted file mode 100644 index 921b676..0000000 Binary files a/ml/linear_regression/images/overfitting.jpeg and /dev/null differ diff --git a/ml/linear_regression/images/select_alpha.png b/ml/linear_regression/images/select_alpha.png deleted file mode 100644 index 67855aa..0000000 Binary files a/ml/linear_regression/images/select_alpha.png and /dev/null differ diff --git a/ml/linear_regression/linear_regression.md b/ml/linear_regression/linear_regression.md deleted file mode 100644 index d32a6b9..0000000 --- a/ml/linear_regression/linear_regression.md +++ /dev/null @@ -1,503 +0,0 @@ -线性回归问题总结 -=============== - -## 目录 -+ 摘要 -+ 线性回归概述 - - 假设函数与平方损失函数 - - 为什么是最小二乘法 - - 多项式回归 -+ 梯度下降法 - - 一般描述 - - 学习率的选择 - - 在多变量下存在的问题 - + 特征缩放 - + 均值归一化 - - 局部最优 - + 平方损失函数是凸函数 -+ 规范方程法 - - 直觉理解 - - 严格数学推导 - - 不可逆 -+ 过拟合 - - 过拟合概述与产生原因,解决方案 - - 正则化的损失函数 - - 规范方程法:一定是可逆阵 - - 梯度下降法:仍然是凸函数 - -## 摘要 - -这篇文章主要介绍线性回归问题。任何一种统计学习方法都由三个要素组成,即**模型**,**策略**以及**算法**。所谓模型,就是本文中的**假设函数**(`hypothesis function`);策略或者评价准则,即本文中的**损失函数**;而算法,则是选取最优模型的方法。本文将从这三个方面对线性回归问题进行总结。 - -第一部分阐述了线性回归问题的基本概念,给出了它的假设函数以及损失函数的数学表达式,并且指出**平方损失函数**在本质上与**最大似然估计**无异。接下来的两个部分,分别阐述了求解该模型的两种算法,即**梯度下降法**与**规范方程法**,给出了两种方法的数学推导与数学表达式,并就实际应用中可能存在的问题进行讨论。最后,介绍了在统计学习中常见的一个问题——**过拟合**,并且就解决过拟合问题的常见方法——**正则化**(`regularization`)进行了深入的探讨。 - -这篇文章的重点在于:平方损失函数与最大似然估计的联系,平方损失函数是凸函数的证明,规范方程法的数学推导,正则化后的平方损失函数仍然是凸函数的证明以及正则化的规范方程一定可逆的证明。 - -## 线性回归概述 - -> 假设函数与平方损失函数。 - -线性回归问题是这样一个问题,对于输入$x$($x$是一个$n$维向量,表示$n$个特征),假设输出$y$是$x$的线性函数。不妨设假设函数为 - -$$ -\hat{y} = h_\theta(x) = \theta_0 + \theta_1x_1 + \cdots + \theta_nx_n -$$ - -为了简单起见,不妨引入一个$x_0 = 1$,这样 - -$$ -\hat{y} = \Sigma_{i = 0}^n \theta_ix_i = \theta^Tx -$$ - -这里$\hat{y}$表示对$y$的估计值。因此,线性回归的任务,就是找到一个合适的列向量$\theta$,使得假设函数尽可能反映输入样本的特征。为了衡量假设函数与输入样本的符合程度,需要定义一个新的函数,这里称为损失函数或者代价函数(`cost function`)。 - -在线性回归问题中,常用的损失函数为平方损失函数(`square cost function`),定义为$J(\theta)$ - -$$ -J(\theta) = \frac{1}{2m}\Sigma_{i = 1}^m (\hat{y}^{(i)} - y^{(i)})^2 = \frac{1}{2m}\Sigma_{i = 1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 -$$ - -容易注意到,平方损失函数的主体是用最小二乘法给出的,它反映了样本预测值与实际值之间的偏离程度。假设函数越符合样本,则损失函数的值就会越小;反之,假设函数越偏离样本,损失函数的值就会越大。因此可以用损失函数的取值来定量衡量参数$\theta$的好坏,最优的一组$\theta$值,应该恰好使损失函数取到最小值。这样,就将参数优化问题,转化为了求解最值问题。 - -> 为什么使用平方损失函数? - -使用平方损失函数并非偶然,而是由最大似然估计推导而出的。下面给出推导过程。 - -在线性回归问题中,模型假设实际上是输出$y$乃以$h_\theta(x)$为均值的**正态分布**,即 - -$$ -y|x = h_\theta(x) + \epsilon,\ \epsilon \sim N(0, \sigma^2) -$$ - -所以有 - -$$ -y|x \sim N(h_\theta(x), \sigma^2)\\\\ -f(y | x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(y - \theta^Tx)^2}{2\sigma^2}} -$$ - -因此可以写出似然函数$L(\theta)$ - -$$ -L(\theta) = \Pi_{i = 1}^m f(y^{(i)}|x^{(i)}) = (\frac{1}{\sqrt{2\pi}\sigma})^m\cdot e^{-\frac{1}{2\sigma^2}\Sigma_{i = 1}^m (y^{(i)} - \theta^Tx^{(i)})^2}\\\\ -lnL(\theta) = -mln(\sqrt{2\pi}\sigma) - \frac{1}{2\sigma^2}\Sigma_{i = 1}^m(y^{(i)} - \theta^Tx^{(i)})^2 -$$ - -这样,对似然函数求最大值,就等价于求 - -$$ -\Sigma_{i = 1}^m(y^{(i)} - \theta^Tx^{(i)})^2 -$$ - -的最小值,此时上面的平方损失函数也取到最小值。因此可以看到,平方损失函数实际上与最大似然估计是一脉相承的。 - -> 多项式回归。 - -实际上,线性回归还可以应用在非线性的场合,比方说有一个非线性的假设函数 - -$$ -h_\theta(x) = \theta_0 + \theta_1x + \theta_2x^2 -$$ - -不妨设$x_1 = x, x_2 = x^2$,这样就可以将上面的假设函数写作 - -$$ -h_\theta(x_1, x_2) = \theta_0 + \theta_1x_1 + \theta_2x_2 -$$ - -这样,就把非线性的假设函数转化为了线性回归问题,从而可以利用线性回归的方法来解决。该问题被称为**多项式回归**问题(`polynomial regression`)。 - -理论上来讲,一般的可导函数总是可以通过泰勒展开,写成多项式的形式,因此多项式回归理论上可以解决很多复杂的问题。然而,在实际应用中,当选择的多项式形式过于复杂时,可能会产生过拟合(`overfitting`)的问题,将会在后面阐述。 - -## 梯度下降法 - -> 梯度下降法的基本表述。 - -梯度下降法(`gradient descent`)是求解最优化问题的常见数值方法。它的基本思想是在每次迭代过程中,使得被优化的参数沿着函数下降速度最快的方向(即梯度的方向)移动一个微小的步长,随着迭代的进行,被优化函数的取值会越来越小,直至收敛至极小值点。 - -对于任意被优化函数$J(\theta)$,梯度下降法的参数更新公式为 - -$$ -\theta_j := \theta_j - \alpha\frac{\partial}{\partial \theta_j}J(\theta) -$$ - -这里的$\alpha$称为**学习率**(`learning rate`),它其实就是梯度下降法采用的步长。采用梯度下降法来求解平方损失函数的最小值,则有 - -$$ -\theta_j := \theta_j - \frac{\alpha}{m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} -$$ - -通过选择合适的迭代次数和学习率,就可以得到使$J(\theta)$最小的$\theta$了。 - -### 梯度下降法存在的问题若干 - -> 学习率$\alpha$的选择。 - -学习率的选择在梯度下降法中至关重要。如果学习率太小,则需要更多次迭代才能找到最优解,需要较长的学习时间;而如果学习率太大,也有可能导致收敛速度慢,甚至可能会发散。这是因为学习率太大时,不再满足全微分方程中微小变化量的条件,因此每次迭代后被优化函数都未必可以取到比上一次迭代更小的值。学习率的选择对$J(\theta)$收敛速度的影响如下图所示: - - - -可以看到,当$\alpha$很小时(如图中红色曲线所示),损失函数$J(\theta)$收敛速度较慢。随着$\alpha$逐渐增大,曲线逐渐变得陡峭,收敛速度越来越快。可是当$\alpha$很大时($\alpha = 1.3$,图中黑色曲线)收敛速度反而相对此前变慢了;$\alpha$继续增大将导致代价函数发散(图中洋红色曲线)。 - -因此,理想的方案是选择这样一个$\alpha$,它应该处于收敛与发散的临界点,一方面保证被优化函数最终收敛,另一方面还要有较快的收敛速度。 - -> 局部最优解。 - -从上面的讨论中可以看出,梯度下降法最终将收敛到某个局部最优点,而无法保证收敛到全局最优点。实际上,当选择不同的初始值时,使用梯度下降法往往会收敛到不同的极值点,如下图所示: - - - -因此,使用梯度下降法无法保证得到的结果一定是全局最优解。然而,需要指出,对于线性回归与平方损失函数,则不存在这个问题,因为平方损失函数是**凸函数**。证明如下: - -平方损失函数$J(\theta)$满足 - -$$ -J(\theta) = \frac{1}{2m}\Sigma_{i = 1}^m (h_\theta(x^{(i)}) - y^{(i)})^2 -$$ - -所以 - -$$ -\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})x_j^{(i)}\\\\ -\frac{\partial^2}{\partial \theta_j\partial \theta_k}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mx_j^{(i)}x_k^{(i)} -$$ - -因此,可以写出`Hessian`矩阵$H$: - -$$ -H = \frac{1}{m}\left[ - \begin{matrix} - \Sigma_{i = 1}^mx_0^{(i)^2} & \Sigma_{i = 1}^mx_0^{(i)}x_1^{(i)} & \cdots & \Sigma_{i = 1}^mx_0^{(i)}x_n^{(i)}\\\\ - \Sigma_{i = 1}^mx_1^{(i)}x_0^{(i)} & \Sigma_{i = 1}^mx_1^{(i)^2} & \cdots & \Sigma_{i = 1}^mx_1^{(i)}x_n^{(i)}\\\\ - \vdots & \vdots & & \vdots&\\\\ - \Sigma_{i = 1}^mx_n^{(i)}x_0^{(i)} & \Sigma_{i = 1}^mx_n^{(i)}x_1^{(i)} & \cdots & \Sigma_{i = 1}^mx_n^{(i)^2}\\\\ - \end{matrix} -\right] -$$ - -容易看出, - -$$ -H = \frac{1}{m}X^TX -$$ - -因此$H$显然是一个**半正定矩阵**,根据凸函数判别的充分条件,可以得出平方损失函数为凸函数。 - -凸函数具有一系列的优良性质,比如说只有一个极值点,即全局最小值点。因此对凸函数使用梯度下降法总是可以收敛至全局最优点。 - -> 多特征的线性回归 - -在实际应用中,一个问题往往会有成百上千个特征,甚至更多。这些不同特征的有截然不同的取值范围,如果不同特征取值范围差别很大,则会导致梯度下降法收敛速度慢的问题。 - -考虑存在两个特征$x_1, x_2$,它们各自的取值范围分别是$0 < x_1 < 1$,$0 < x_2 < 1000$,两者相差了1000倍。在使用梯度下降法时,参数$\theta_2$的变化量也将大约是$\theta_1$的1000倍,这将导致损失函数等高线图呈现扁平的椭圆状,梯度下降路径相对取值范围更大的特征对应的参数来回波动,从而导致收敛速度缓慢。 - -为了解决上面的问题,需要对取值相差比较大的特征进行**特征缩放**(`feature scaling`)与**均值规范**(`mean normalization`),具体的操作是求出每一个特征的均值$\mu$以及范围(最大值减去最小值),随后对每一个特征点进行以下更新 - -$$ -x_j^{(i)} := \frac{x_j^{(i)} - \mu^{(i)}}{range^{(i)}} -$$ - -在实际应用中,也可以用**标准差**(`standard deviation`)来代替范围进行更新。这样更新后的特征取值范围都在$[-1, 1]$之间,绘出的等高线图接近于(高维)球形,收敛速度就要快多了。 - -## 规范方程 - -> 规范方程的直觉理解 - -前面提到假设函数的表达式为 - -$$ -h_\theta(x) = \theta^Tx = x^T\theta -$$ - -设样本容量为$m$,则输入$X$以及对应的输出$Y$都可以写成矩阵的形式 - -$$ -X = \left[ - \begin{matrix} - x^{(1)^T}\\\\ - x^{(2)^T}\\\\ - \vdots\\\\ - x^{(m)^T} - \end{matrix} -\right],\ \ -Y = \left[ - \begin{matrix} - y^{(1)}\\\\ - y^{(2)}\\\\ - \vdots\\\\ - y^{(m)} - \end{matrix} -\right],\ \ -\theta = \left[ - \begin{matrix} - \theta_0\\\\ - \theta_1\\\\ - \vdots\\\\ - \theta_n - \end{matrix} -\right] -$$ - -这样,就可以把假设函数也写成矩阵形式 - -$$ -Y = X\theta -$$ - -为了得到$\theta$,只需求解该矩阵方程,为此将等式的两边同时乘以$X^T$ - -$$ -X^TY = X^TX\theta -$$ - -所以 - -$$ -\theta = (X^TX)^{-1}X^TY -$$ - -这就是规范方程法的数学表述。以下就这个结论给出严格推导。 - -> 规范方程法的推导 - -平方损失函数(cost function)$J(\theta)$可以写作 - -$$ -J(\theta) = \frac{1}{2m}\Sigma_{i = 1}^m[h_\theta(x^{(i)}) - y^{(i)}]^2 -$$ - -参数$\theta$应该是使$J(\theta)$最小的一组列向量。由多元函数极值可知,$\theta$应该满足: - -$$ -\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^m[h_\theta(x^{(i)}) - y^{(i)}]x_j^{(i)} = 0 -$$ - -解上述方程即可以得到$\theta$的解析解。 - -为了简单起见,下面用矩阵形式来对$J(\theta)$进行表述。所以$J(\theta)$可以写作 - -$$ -J(\theta) = \frac{1}{2m}(X\theta - Y)^T(X\theta - Y) = \frac{1}{2m}(\theta^TX^TX\theta - 2Y^TX\theta + Y^TY) -$$ - -对$\theta$求导,所以 - -$$ -\frac{d}{d\theta}J(\theta) = \frac{1}{2m}(2X^TX\theta - 2X^TY) = 0 -$$ - -从而可得 - -$$ -\theta = (X^TX)^{-1}X^TY -$$ - -该结论与上面的直觉理解完全一致。 - -> 规范方程法存在的问题 - -需要注意的是,在实际应用中,$X^TX$往往是不可逆的,比如当特征的数量多于样本容量时。因此,往往是通过对$X^TX$求伪逆,来代替$(X^TX)^{-1}$。 - -> 梯度下降法与规范方程法的比较 - -在通常情况下,当样本的特征比较少时,比如特征数少于10000时,选择规范方程法会有比较好的效果;而当样本数量太多,则应该选择梯度下降法来对模型进行求解。 - -这是因为,在规范方程法中,涉及到矩阵的求逆运算,其时间复杂度是$O(n^3)$,因此当模型的特征太多时,该逆矩阵的计算会消耗大量的时间。反之,即使特征数量非常多,梯度下降法也仍然具有较好的性能。 - -## 过拟合 - -> 过拟合概述 - -过拟合是统计学习中非常常见的一个问题,它是指模型的选择过于复杂,因而可以在训练集上得到很好的训练效果,使得损失函数接近于零,可是却难以将该模型**泛化**(`generalize`)到其他应用场合,比如说在一个新的测试集上预测准确率就会非常低下。**欠拟合**,正常拟合,以及过拟合的关系如下图所示 - -![overfitting](images/overfitting.jpeg) - -可以看到,第一张图中的模型选择过于简单,用线性函数去拟合一个多项式回归问题,发生了欠拟合;第三张图的模型则过于复杂,用了一个非常高阶的多项式来拟合一个低阶问题,发生了过拟合。 - -关于过拟合,我想到这样一个直观的理解:训练模型的过程就好比一个人复习考试的过程,一种复习方案是深入理解题目背后的原理和知识点,另一种复习方案则是把每个题目都记忆下来。显然后者在训练集上会有出色的表现,如果记忆力足够好,甚至可以达到100%的正确率,可是在实际考试过程中,遇到的都是他从未记忆过的题目,此时就束手无策了。第二种复习方案就是这里的过拟合。 - -过拟合会发生,主要是因为模型选择过于复杂,选择的特征太多。因此,一种解决过拟合的方案,就是手动剔除一些不重要的特征,简化模型,或者可以使用**模型选择算法**(`model selection algorithm`),后者将在后面的文章中提到。另一种更加常见的方案,就是下面要重点阐述的正则化。 - -> 正则化的损失函数 - -考虑这样一种情况,当前模型的假设函数为 - -$$ -h_\theta(x) = \theta_0 + \theta_1x + \theta_2x^2 + \theta_3x^3 + \theta_4x^4 -$$ - -为了避免发生过拟合,我可以限制$\theta_3, \theta_4$的大小,使得高阶项具有相对更小的权重。具体的方案是在损失函数中对$\theta_3, \theta_4$添加**罚项**(`penalty`),比如 - -$$ -J(\theta) = \frac{1}{2m}\Sigma_{i = 1}^m[h_\theta(x^{(i)}) - y^{(i)}]^2 + 1000\theta_3^2 + 1000\theta_4^2 -$$ - -这样,为了使$J(\theta)$取到最小值,$\theta_3, \theta_4$的取值必然相对较小,因此在一定程度上简化了模型。 - -在实际应用中,很难知道哪一些特征会导致模型过拟合,因此常见的方案是对每一个$\theta_j$,都添加一个上述的罚项,也称为**正则化项**(`regularized term`)。这样,正则化的损失函数就有以下的形式: - -$$ -J(\theta) = \frac{1}{2m}[\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})^2 + \lambda\Sigma_{i = 1}^n\theta_i^2] -$$ - -需要注意的是,这里并没有对$\theta_0$引入正则化项,老师说这是一种约定俗成,并没有特别的原因。使用梯度下降法或者规范方程法,就可以求得在正则化下的最优参数$\theta$了。 - -> 引入正则化项的规范方程推导 - -为了令引入正则化项的$J(\theta)$取到最小值,同样可以利用多元函数极值,令 - -$$ -\frac{\partial}{\partial \theta_j}J(\theta) = 0 -$$ - -逐个求解出$\theta_j$,就可以得到最优的一组$\theta$,在此不再赘述。 - -为了简化问题,仍然用矩阵形式表示$J(\theta)$,所以 - -$$ -\begin{aligned} -J(\theta) = &\frac{1}{2m}[(X\theta - Y)^T(X\theta - Y) + \lambda(I\theta)^TI\theta]\\\\ - = &\frac{1}{2m}(\theta^TX^TX\theta - 2Y^TX\theta + Y^TY + \lambda(I\theta)^TI\theta] - \end{aligned} -$$ -其中 - -$$ -I = \left[ -\begin{matrix} -0 & 0 & \cdots & 0\\\\ -0 & 1 & \cdots & 0\\\\ -\vdots & \vdots & & \vdots\\\\ -0 & 0 & \cdots & 1 -\end{matrix} -\right]. -$$ - -所以 - -$$ -\frac{\partial}{\partial \theta}J(\theta) = \frac{1}{2m}(2X^TX\theta - 2X^TY + 2\lambda I\theta) = 0 -$$ - -从而可以解出 - -$$ -\theta = (X^TX + \lambda I)^{-1}X^TY = (X^TX + \lambda \left[ -\begin{matrix} -0 & 0 & \cdots & 0\\\\ -0 & 1 & \cdots & 0\\\\ -\vdots & \vdots & & \vdots\\\\ -0 & 0 & \cdots & 1 -\end{matrix} -\right])^{-1}X^TY -$$ - -需要说明的是,正则化项的引入不仅解决了过拟合的问题,还使得新的矩阵 -$$ -X^TX + \lambda I -$$ - -一定是可逆矩阵,而此前的 - -$$ -X^TX -$$ - -则不能保证它的可逆性。下面给出证明。 - -前面已经指出 - -$$ -X = \left[ - \begin{matrix} - x^{(1)^T}\\\\ - x^{(2)^T}\\\\ - \vdots\\\\ - x^{(m)^T} - \end{matrix} -\right] = \left[ -\begin{matrix} -x_0^{(1)} & x_1^{(1)} & \cdots & x_n^{(1)}\\\\ -x_0^{(2)} & x_1^{(2)} & \cdots & x_n^{(2)}\\\\ -\vdots & \vdots & & \vdots\\\\ -x_0^{(m)} & x_1^{(m)} & \cdots & x_n^{(m)}\\\\ -\end{matrix} -\right] -$$ - -所以 - -$$ -X^T = \left[ -\begin{matrix} -x_0^{(1)} & x_0^{(2)} & \cdots & x_0^{(m)}\\\\ -x_1^{(1)} & x_1^{(2)} & \cdots & x_1^{(m)}\\\\ -\vdots & \vdots & & \vdots\\\\ -x_n^{(1)} & x_n^{(2)} & \cdots & x_n^{(m)}\\\\ -\end{matrix} -\right] -$$ - -容易注意到,$X^TX$的对角线元素一定大于或者等于零。且对角线上第一个元素为 - -$$ -a_{11} = \Sigma_{i = 1}^mx_0^{(i)}\cdot x_0^{(i)} = m -$$ - -因此$X^T + \lambda I$的对角线元素$a_{ii}$一定大于$0$,对于任意的$\lambda > 0$成立。所以 - -$$ -\left| X^T + \lambda I \right| = \Pi_{i = 1}^{n + 1}a_{ii} > 0 -$$ - -因此$X^T + \lambda I$一定是一个可逆矩阵。实际上,还可以得到更进一步的结论,即$X^T + \lambda I$一定是一个正定矩阵。这是因为$X^TX$和$I$都是半正定矩阵,即对于任意的非零向量$\eta$,都有 - -$$ -\eta^TX^TX\eta \ge 0,\ \eta^TI\eta \ge 0 -$$ - -因此两者之和也必定是一个半正定矩阵。由于此前已经得到$X^T + \lambda I$可逆,故不存在为零的特征值,所以所有特征值都必定大于零,因此$X^T + \lambda I$是一个正定矩阵。 - -> 引入正则化项的梯度下降法 - -关于梯度下降法自然不必多说,这里主要证明,引入正则化项后的平方损失函数仍然是凸函数,因此使用梯度下降法仍然可以收敛到全局最优点。 - -再次给出$J(\theta)$的表达式 - -$$ -J(\theta) = \frac{1}{2m}[\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})^2 + \lambda\Sigma_{i = 1}^n\theta_i^2] -$$ - -所以有 - -$$ -\frac{\partial}{\partial \theta_0}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})x_0^{(i)}\\\\ -\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} + \frac{1}{m}\theta_j\ (j \ne 0) -$$ - -$$ -\frac{\partial^2}{\partial \theta_0^2}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mx_0^{(i)}x_0^{(i)}\\\\ -\frac{\partial^2}{\partial \theta_j^2}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mx_j^{(i)}x_j^{(i)} + \frac{\lambda}{m}\ (j \ne 0)\\\\ -\frac{\partial^2}{\partial \theta_j\partial \theta_k}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mx_j^{(i)}x_k^{(i)} \ (j \ne k) -$$ - -因此,可以写出`Hessian`矩阵 - -$$ -H = \frac{1}{m}\left[ - \begin{matrix} - \Sigma_{i = 1}^mx_0^{(i)^2} & \Sigma_{i = 1}^mx_0^{(i)}x_1^{(i)} & \cdots & \Sigma_{i = 1}^mx_0^{(i)}x_n^{(i)}\\\\ - \Sigma_{i = 1}^mx_1^{(i)}x_0^{(i)} & \Sigma_{i = 1}^mx_1^{(i)^2} + \lambda & \cdots & \Sigma_{i = 1}^mx_1^{(i)}x_n^{(i)} \\\\ - \vdots & \vdots & & \vdots&\\\\ - \Sigma_{i = 1}^mx_n^{(i)}x_0^{(i)} & \Sigma_{i = 1}^mx_n^{(i)}x_1^{(i)} & \cdots & \Sigma_{i = 1}^mx_n^{(i)^2} + \lambda\\\\ - \end{matrix} -\right] -$$ - -容易看出, - -$$ -H = \frac{1}{m}(X^TX + \lambda I) -$$ - -上面已经证明了,该矩阵是一个正定矩阵。根据凸函数的充分条件,可以得到正则化后的平方损失函数仍然是一个凸函数。 diff --git a/ml/logistic_regression/images/fig1.png b/ml/logistic_regression/images/fig1.png deleted file mode 100644 index 5115b69..0000000 Binary files a/ml/logistic_regression/images/fig1.png and /dev/null differ diff --git a/ml/logistic_regression/images/fig2.png b/ml/logistic_regression/images/fig2.png deleted file mode 100644 index c4438e2..0000000 Binary files a/ml/logistic_regression/images/fig2.png and /dev/null differ diff --git a/ml/logistic_regression/images/initial_data.png b/ml/logistic_regression/images/initial_data.png deleted file mode 100644 index 1eb44b0..0000000 Binary files a/ml/logistic_regression/images/initial_data.png and /dev/null differ diff --git a/ml/logistic_regression/images/justfit.png b/ml/logistic_regression/images/justfit.png deleted file mode 100644 index ed58969..0000000 Binary files a/ml/logistic_regression/images/justfit.png and /dev/null differ diff --git a/ml/logistic_regression/images/overfit.png b/ml/logistic_regression/images/overfit.png deleted file mode 100644 index 6ac98b2..0000000 Binary files a/ml/logistic_regression/images/overfit.png and /dev/null differ diff --git a/ml/logistic_regression/images/sigmoid.png b/ml/logistic_regression/images/sigmoid.png deleted file mode 100644 index 30921af..0000000 Binary files a/ml/logistic_regression/images/sigmoid.png and /dev/null differ diff --git a/ml/logistic_regression/logistic_regression.md b/ml/logistic_regression/logistic_regression.md deleted file mode 100644 index 7db6b62..0000000 --- a/ml/logistic_regression/logistic_regression.md +++ /dev/null @@ -1,292 +0,0 @@ -逻辑回归问题总结 -============== - -## 目录 -+ 摘要 -+ 逻辑回归概述 - - 逻辑回归的假设函数 - + 假设函数的意义 - + 决策边界 - - 损失函数 - + 为什么对数似然函数 - + 对数似然函数是凸函数 - - 多分类问题 -+ 过拟合 - - 正则化的损失函数 - -## 逻辑回归概述 - -在 [线性回归总结](../linear_regression/linear_regression.md) 中,简单介绍了线性回归问题,这里将要阐述的逻辑回归,却并不是回归问题,而是一个分类问题(`classification problem`)。分类问题与回归问题的主要界限,在于分类问题的输出是离散值,而回归问题的输出是连续的。根据分类问题输出取值数量的不同,又可以分为**二分类问题**和**多分类问题**,这里主要阐述二分类问题。 - -二分类问题的输出一般为0或者1,表示某一特定的事件不发生或者发生,比如辅助诊断系统判断患者患病或者不患病,邮件分类系统判断邮件是否是垃圾邮件,都是典型的二分类问题。 - -为了训练二分类问题,一种直觉上的方法是直接套用此前的线性回归模型,这样就可以得到模型的假设函数为 - -$$ -h_\theta(x) = \theta^Tx -$$ - -仍然使用此前的平方损失函数以及梯度下降法(或者规范方程法)来对模型进行求解,对于某个简单的数据集,可以得到下面的结果 - -![fig1](images/fig1.png) - -此时$h_\theta(x)$的输出值仍然是连续值,而二分类问题要求模型的输出是0或者1,因此需要对假设函数的输出进行进一步处理。这里可以简单地令$\hat{y} = 1$当且仅当$h_\theta(x) >= 0.5$,令$\hat{y} = 0$当且仅当$h_\theta(x) < 0.5$。可以看到,采用这种策略时,对上图的数据具有很高的预测正确率。 - -然而这种方法却也有局限性,考虑在训练集中增加一个样本点$X = 80, y = 1$,此时线性回归模型的结果如下图所示: - -![fig2](images/fig2.png) - -可以看到,此时回归直线相对此前向右偏移,模型对训练集的预测能力下降了不少。可以想象,如果训练集中还有更多这样的极端数据,回归的直线将继续向右偏移,模型的预测能力将继续下降。 - -此外,线性回归模型的假设函数,其值域是$(-\infty, +\infty)$,而分类问题的输出$y$无非是0或者1而已,当$h_\theta(x) > 1$或者$h_\theta(x) < 0$时不具有意义。可见,简单套用线性回归的方法来解决分类问题是行不通的。 - -### 逻辑回归的假设函数 - -可以做一些简单的修改,使得假设函数的值域限制在$[0, 1]$。这里引入**sigmoid函数**,它的表达式$g(z)$满足 - -$$ -g(z) = \frac{1}{1 + e^{-z}} -$$ - -可以看出,它的取值范围恰好在$(0, 1)$之间,实际上,它的图像如下所示: - -![sigmoid](images/sigmoid.png) - -因此,可以把 sigmoid 函数作用到此前的线性回归模型上,就得到了新的假设函数 - -$$ -h_\theta(x) = sigmoid(\theta^Tx) -$$ - -此时,它的函数值,就可以被理解成在输入为$x$的条件下,$y = 1$的概率,即 - -$$ -P(y = 1| x) = sigmoid(\theta x) -$$ - -> 决策边界 - -以下对 sigmoid 函数进行进一步的讨论。前面已经指出,假设函数的返回值,表示的是预测样本为正(`positive`)的概率。一般地,当$h_\theta(x) >= 0.5$时,预测$\hat{y} = 1$;当$h_\theta(y) < 0.5$时,预测$\hat{y} = 0$。这样,$h_\theta(x) = 0.5$就成为正负样本的边界,称为**决策边界**(`decision boundary`)。 - -由 - -$$ -h_\theta(x) = sigmoid(\theta^Tx) = 0.5 -$$ - -根据 sigmoid 函数的图像,恰好可以得到 - -$$ -\theta^Tx = \theta_0x_0 + \theta_1x_1 + \theta_2x_2 + \cdots + \theta_nx_n = 0 -$$ - -容易看出,这是一个(高维)平面的方程,预测为正负的样本,分别分布在该平面的两侧。因此,逻辑回归的假设函数,本质上就是找到这样一个高维平面对样本点进行划分,达到尽可能高的划分正确率。 - -利用**多项式回归**,可以得到更加复杂的决策边界。比如令 - -$$ -h_\theta(x) = sigmoid(\theta_0 + \theta_1x_1 + \theta_2x_2 + \theta_3x_1^2 + \theta_4x_2^2) -$$ - -则得到的决策边界为 - -$$ -\theta_0 + \theta_1x_1 + \theta_2x_2 + \theta_3x_1^2 + \theta_4x_2^2 = 0 -$$ - -容易看出,这是一个圆锥曲线的方程。因此,通过构造更高阶的多项式,可以得到相当复杂的决策边界,从而将逻辑回归应用到更加复杂的分类问题当中。 - -### 逻辑回归的损失函数 - -在线性回归当中,是使用平方损失函数来作为损失函数,逻辑回归也可以沿用这种方式,即定义$J(\theta)$满足 - -$$ -J(\theta) = \frac{1}{2m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})^2 -$$ - -为了找到一组最优的$\theta$,应该使平方损失函数具有最小值,这里仍然沿用**梯度下降法**来求解。然而,在逻辑回归中,上面的平方损失函数却并非一个凸函数,使用梯度下降法无法确保收敛到全局最优点,等以后我变强了可以考虑给个证明。 - -因此,实际上并不是使用平方损失函数,而是**对数损失函数**,来作为逻辑回归的损失函数。此时,单个样本的损失函数$cost(h_\theta(x), y)$满足 - -$$ -cost(h_\theta(x), y) = - \left\\{ - \begin{aligned} - -log(h_\theta(x))&, y = 1\\\\ - -log(1 - h_\theta(x))&, y = 0 - \end{aligned} -\right. -$$ - -可以分别画出$y = 0$与$y = 1$的损失函数的图像,以获得一个比较直观的理解。容易看到,当$y = 1$时,对输出的预测值$h_\theta(x)$越接近一,则损失函数的值越小;预测值越接近零,则损失函数的值越大。并且当$h_\theta(x) = 1$时,$cost(h_\theta(x), 1) = 0$,当$h_\theta(x) = 0$时,$cost(h_\theta(x), 1) = +\infty$。该性质是容易理解的,即预测值与真值越接近,则相应的损失就越小。$y = 0$时也有类似的性质。 - -可以用一种更加简单的方法来表示$cost(h_\theta(x), y)$,即 - -$$ -cost(h_\theta(x), y) = -ylog(h_\theta(x)) - (1-y)log(1 - h_\theta(x)) -$$ - -因此,整个样本的损失函数为 - -$$ -J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mcost(h_\theta(x^{(i)}), y^{(i)}) = -\frac{1}{m}[\Sigma_{i = 1}^my^{(i)}log(h_\theta(x^{(i)})) + (1-y^{(i)})log(1 - h_\theta(x^{(i)}))] -$$ - -> 为什么使用对数似然函数? - -使用对数似然函数并非偶然,而是由**最大似然估计**推导而出的。下面给出推导过程: - -假设函数$h_\theta(x)$满足 - -$$ -h_\theta(x) = sigmoid(\theta^T \cdot x) -$$ - -它的意义是在输入为$x$的条件下,输出$y = 1$的概率,即 - -$$ -P(y = 1| x) = sigmoid(\theta^T x) -$$ - -因此$y | x$服从这样一个两点分布,其中 - -$$ -\begin{aligned} -&P(y = 1| x) = sigmoid(\theta^T x)\\\\ -&P(y = 0| x) = 1 - sigmoid(\theta^T x) -\end{aligned} -$$ - -设样本容量为$m$,其中$y = 1$的样本有$k$个,所以可以写出似然函数 - -$$ -L(\theta) = \Pi_{y^{(i)} = 1}sigmoid(\theta x^{(i)}) \cdot \Pi_{y^{(j) } = 0}(1 - sigmoid(\theta x^{(j)})) -$$ - -对似然函数进行一些化简 - -$$ -L(\theta) = \Pi_{i = 1}^m \{[h_\theta(x^{(i)})]^{y^{(i)}} \cdot [(1 - h_\theta(x^{(i)}))]^{1-y^{(i)}}\} -$$ - -从而可以得到 - -$$ -lnL(\theta) = \Sigma_{i = 1}^m [y^{(i)}lnh_\theta(x^{(i)}) + (1 - y^{(i)})ln(1 - h_\theta(x^{(i)})] -$$ - -这样,求似然函数的最大值,就等价于求 - -$$ -J(\theta) = -\frac{1}{m}[\Sigma_{i = 1}^my^{(i)}ln(h_\theta(x^{(i)})) + (1-y^{(i)})ln(1 - h_\theta(x^{(i)}))] -$$ - -的最小值。可见,对数损失函数,实际上与最大似然估计是一脉相承的。 - -> 对数损失函数是凸函数 - -相较于平方损失函数,对数似然函数还具有一个优良性质,即它是一个凸函数。证明如下: - -再次给出$J(\theta)$的表达式: - -$$ -J(\theta) = -\frac{1}{m}[\Sigma_{i = 1}^my^{(i)}log(h_\theta(x^{(i)})) + (1-y^{(i)})log(1 - h_\theta(x^{(i)}))] -$$ - -所以 - -$$ -\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^m(h_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} -$$ - -这里应该看到,对数损失函数以及平方损失函数的一阶偏导的形式是完全相同的(尽管其中的$h_\theta(x)$不同),据说这并不是巧合,而是存在一些深层次的原因,有机会我会回来说明一下。 - -因此可以给出二阶偏导的表达式 - -$$ -\frac{\partial^2}{\partial \theta_j \partial \theta_k}J(\theta) = \frac{1}{m}\Sigma_{i = 1}^mx_j^{(i)}\frac{e^{-z}}{(1 + e^{-z})^2}x_k^{(i)},\ z = \theta^Tx^{(i)} -$$ - -可以看出,$\frac{e^{-z}}{(1 + e^{-z})^2}$一定是大于零的。不妨设$g(x)$为 - -$$ -g(x) = \sqrt{\frac{e^{-\theta^Tx}}{(1 + e^{-\theta^Tx})^2}} -$$ - -构造矩阵$G$为 - -$$ -G = \left[ -\begin{matrix} -g(x^{(1)}) & & &\\\\ -& g(x^{(2)}) & &\\\\ -& & \ddots & \\\\ -& & & g(x^{(m)}) -\end{matrix} -\right] -$$ - -所以对数损失函数的 Hessian 矩阵为 - -$$ -H = \frac{1}{m}\left[ -\begin{matrix} -\frac{\partial^2}{\partial \theta_0^2}J(\theta) & \frac{\partial^2}{\partial \theta_0 \partial \theta_1}J(\theta) & \cdots & \frac{\partial^2}{\partial \theta_0 \partial \theta_n}J(\theta)\\\\ -\frac{\partial^2}{\partial \theta_1 \partial \theta_0}J(\theta) & \frac{\partial^2}{\partial \theta_1^2}J(\theta) - & \cdots & \frac{\partial^2}{\partial \theta_1 \partial \theta_n}J(\theta)\\\\ -\vdots & \vdots & & \vdots\\\\ -\frac{\partial^2}{\partial \theta_n \partial \theta_0}J(\theta) & \frac{\partial^2}{\partial \theta_n \partial \theta_1}J(\theta) & \cdots & \frac{\partial^2}{\partial \theta_n^2}J(\theta) -\end{matrix} -\right] -$$ - -容易看出(? - -$$ -H = \frac{1}{m}(GX)^TGX -$$ - -因此,Hessian 矩阵是一个半正定矩阵,从而对数损失函数确实是一个凸函数,得证。同理可得,规范化后的对数损失函数也是一个凸函数,这里不再赘述。这样,利用梯度下降法求解对数损失函数的最小值,一定可以收敛到全局最优点。 - -### 多分类问题 - -顾名思义,多分类问题中预测的结果可以有多个(多于两个)取值,比如给定一个图片集,将每张图片划分为行人,车辆,道路,树木四个类别。为了求解多分类问题,可以简单地套用二分类问题的方法。 - -具体说来,假设多分类问题中一共有$k(k \ge 3)$个类别,因此预测值$y$有$k$个不同的取值。套用二分类问题的方法是,对于每一个类别$c$,定义输出为$c$时$y = 1$,输出不为$c$时$y = 0$,这样就把问题转化为了一个二分类问题。可见,对于任一个类别,都需要训练一个二分类器,最终一共需要训练$k$个二分类器,即 - -$$ -h_\theta^{(i)}(x) = P(y = i | x)\ (i = 1, 2, 3, ..., k) -$$ - -对模型进行预测时,分别使用$k$个二分类器对模型进行预测,可以得到$k$个概率,分别表示在当前输入的条件下,输出为某个类别的可能性。最后,简单选择可能性最大的一个类别,作为多分类问题的输出,即 - -$$ -max_i h_\theta^{(i)}(x) -$$ - -## 过拟合 - -在此前的 [线性回归总结](../linear_regression/linear_regression.md) 中,已经对过拟合问题进行了一些探讨,这里主要是结合实例,直观地说明过拟合问题及其解决方案。 - -为了解决过拟合,仍然可以对此前的对数损失函数添加**正则化项**,正则化后的损失函数$J(\theta)$ - -$$ -J(\theta) = -\frac{1}{m}[\Sigma_{i = 1}^my^{(i)}log(h_\theta(x^{(i)})) + (1-y^{(i)})log(1 - h_\theta(x^{(i)}))] + \frac{\lambda}{2m}\Sigma_{j = 1}^n\theta_j^2 -$$ - -下面以一个具体实例,直观地展示过拟合现象以及正则化对过拟合的影响。这个实例是一个二类分类问题,输入$x$是一个二维向量,即具有两个特征$x_1, x_2$。首先对原始数据进行可视化,如下图所示: - -![initial_data](images/initial_data.png) - -其中用圆圈标识$y = 0$的实例,用十字标识$y = 1$的实例,容易看出,原始数据并不服从线性分布,因此考虑添加高次项,利用多项式回归对该模型进行拟合。这里是添加了$x_1, x_2$的所有二次项与三次项,再加上$x_0$,处理后的输入具有28个特征。 - -之后,我手动将这些数据划分成了训练集和测试集,其中测试集占所有数据的20%。然后分别使用未正则化的损失函数与正则化的损失函数($\lambda = 1$),利用梯度下降法对模型进行训练。结果如下: - - - - -
- -图中黑色的圆圈或者十字,是表示训练集的实例;而红色边界的圆圈或者十字,表示的是测试集实例。可以看到,未经过正则化的损失函数,训练出来的模型决策边界相对复杂,具有许多高次项,同时对于训练集有更高的预测准确率(87.21%);而正则化后的损失函数,利用梯度下降法训练出的决策边界则要简单很多,比较规则,对训练集的准确率也要低一些(80.23%)。 - -尽管如此,未正则化的模型,对于测试集数据的预测正确率则要低了不少(81.25%),而正则化的模型,则对测试集有更高的预测准确率(84.375%)。这说明没有正则化虽然可以获得更高的训练集预测准确率,却难以将训练得到的模型**泛化**到其他输入数据当中,因此发生了过拟合。而引入正则化则可以使训练的模型具有更好的**泛化能力**。 diff --git a/ml/ml_scipts.md b/ml/ml_scipts.md deleted file mode 100644 index eb9ecbb..0000000 --- a/ml/ml_scipts.md +++ /dev/null @@ -1,54 +0,0 @@ -Handscripts when studing Machine Learning -========================================= - -> 什么是机器学习? - -注意三个点,即E, T, P。 - -> 监督学习与无监督学习之间的区别? - -监督学习是指对于输入的数据,它所对应的输出是已知的。监督学习可以分为两类,即回归问题与分类问题,它们的区别在于输出是否是连续的。具体的例子有房价预测问题(回归问题),判断肿瘤是否是良性(分类问题)。 - -无监督学习的输入数据之间没有任何区别,每个输入数据都是等价的,并没有事先表明它的状态或者分类信息(比如房价或者恶性肿瘤),而是由机器来分辨不同数据的属性。典型的例子有`聚类问题`(clustering)以及鸡尾酒宴算法。 - -> 关于深度学习算法的一些思考。 - -人工神经元算法的设计乃是`线性内核`与`非线性激活`的叠加。根据`线性内核`的不同,可以分为`DNN`,`CNN`,`RNN`,它们分别适用于不同的场景。但是这种建模方法显然是不准确的,片面的,因为实际中的神经元对于各种场合的问题都可以很好的适用。这样,应该存在一种更好的方式来模拟神经元。 - -人工神经网络的精髓都在于对大脑中的神经元进行模拟。但是我在想神经元并非一定是解决问题的最高效的方法,虽然神经元经过了几十亿年的进化与自然选择,但它未必是解决现实问题的最优解,可能只是一个局部最优而已,alphaGo的例子就说明了这一点——人类数千年形成的围棋算法实际上只是局部最优解。 - -另一方面,让计算机模拟人脑也未必就是最好的方法。因此我在想,有没有可能跳出现有神经元的桎梏,开创出一个更优化的算法,这样说不定还可以反过来对人类的神经元进行改造。 - -> 梯度下降法存在的问题。 - -首先是学习率(learning rate)的选择。如果$\alpha$太小,则需要多次迭代才能找到局部最优解,需要较长的学习时间;而如果$\alpha$太大,则可能直越过最低点,导致无法收敛,甚至发散。 - -因此$\alpha$的选择,最好是选择可以使代价函数收敛的最大的$\alpha$,为了找到这样一个$\alpha$,可以作出代价函数-迭代次数的关系图。从理论上来将,如果$\alpha$取得足够小,则每一次迭代代价函数都会减小。因此,如果代价函数呈现出其他的趋势,则往往说明是$\alpha$取得太大了。 - -需要指出的是,$\alpha$过大时,也可能会出现收敛速度慢的现象。 - -此外,显而易见的是,梯度下降法只能找到局部最优解,而非全局最优解。实际上,梯度下降法找到的解取决于初始位置的选择。然而,对于线性回归(linear regression)问题,则不存在这个问题,因为线性回归问题的代价函数是一个凸函数(convex function),即它只有一个极值点,该极值点就是它的全局最优解,因此使用梯度下降算法总是可以得到唯一的最优解。 - -> 梯度下降法的深入讨论。 - -对于多元的线性回归问题,如果不同的特征取值范围差别很大,比如$0 < x_1 < 1$,$0 < x_2 < 1000$,两者的取值范围查了1000倍。在使用梯度下降法时,参数$\theta_2$的变化量也将是$\theta_1$的1000倍,这将导致损失函数等高线图呈现扁平的椭圆状,梯度下降路径相对取值范围更大的特征对应的参数来回波动,导致收敛速度缓慢。但是关于这个的数学推导我仍然存在问题。 - -为了解决上面的问题,需要对取值相差比较大的特征进行特征缩放(feature scaling),使它们的取值都在1的附近。这样得到的等高线图就接近于圆形,收敛速度就要快多了。 - -此外还有一个方法是使这些特征的平均值都在0附近,相对于对特征进行了规范化。 - -> 在线性回归问题中,是通过平方损失函数(square error function)作为损失函数,来对参数进行优化的。老师说这样得到的是一条可能性最大的直线,是否可以用最大似然估计来解释这一点呢? - -后来的总结中给出了数学推导。 - -> 多元线性回归(multivariate linear regression)的梯度下降法与规范方程法(normal equation)的一些问题。 - -不明白规范方程法正确性的证明,以及通过多元极值求得的结果与规范方程法是否一致? - -用平方损失函数,利用多元极值求得的$\theta$与求解矩阵方程$Y = X\theta$所得到的结果一致,即$\theta = (X^X)^{-1}X^TY$,不得不让人思考两者是否有什么内在的联系? - -规范方程法伪逆(pseudo-inversion)的数学推导;为什么求逆操作的时间复杂度是`O(n^3)`,其中`n`表示特征的数量。 - -为什么梯度下降法的时间复杂度是`O(kn^2)`,k是什么含义。 - -> 为什么是平方损失函数? diff --git a/ml/neural_network/images/neuron_model.jpg b/ml/neural_network/images/neuron_model.jpg deleted file mode 100644 index 53069d7..0000000 Binary files a/ml/neural_network/images/neuron_model.jpg and /dev/null differ diff --git a/ml/neural_network/images/nn_model.jpg b/ml/neural_network/images/nn_model.jpg deleted file mode 100644 index ada84de..0000000 Binary files a/ml/neural_network/images/nn_model.jpg and /dev/null differ diff --git a/ml/neural_network/images/nn_multiclass.jpg b/ml/neural_network/images/nn_multiclass.jpg deleted file mode 100644 index 3b08427..0000000 Binary files a/ml/neural_network/images/nn_multiclass.jpg and /dev/null differ diff --git a/ml/neural_network/neural_network.md b/ml/neural_network/neural_network.md deleted file mode 100644 index d3d9d7f..0000000 --- a/ml/neural_network/neural_network.md +++ /dev/null @@ -1,176 +0,0 @@ -神经网络算法总结 -============== - -## 目录 -+ 摘要 -+ 前向传播算法 - - 模拟大脑 - - 模型表示 - - 为什么使用神经网络 - - 多分类问题 -+ 后向传播算法 - - 损失函数 - - 梯度的推导 - - 后向传播 - - 实际应用 - + 梯度检查 - + 向量化 - + 随机初始化 -+ 总结 - -## 摘要 - -这篇文章介绍**神经网络算法**的基本原理以及实现方法。对原理的阐述仍然从三个方面来进行,即模型,策略与算法。这里的模型对应了**前向传播算法**(`forward propagation`,策略与算法则对应了**后向传播算法**(`backward propagation`),分别在文章的两段进行说明。最后,会将这两部分串联起来,给出实现神经网络的完整步骤。 - -## 前向传播算法 - -### 模拟大脑 - -让机器具有智能一直是计算机科学家的梦想,然而任何传统的算法都没能做到这一点。一种想法是,让机器模拟人类大脑的行为,从而使机器可以作出和人类相似的决策。 - -人类认知世界的过程可以作出以下抽象:我们的感觉器官,如眼睛、耳朵,获得了来自外界的光学信号与空气振动信号,通过大脑皮层相应位置的处理,转化为了我们可以辨识的物体以及语音的含义。大脑皮层的基本组成单位是神经元细胞,视觉皮层或者听觉皮层都是由许多个神经元细胞相互连接组成的。神经元细胞有两个重要的结构,即树突(`dentrite`)与轴突(`axon`),树突的功能是接受来自其他神经元传递来的信号,是输入单元;轴突则传递信号给其他的神经元,是输出单元。正是我们的感觉器官收到的信号引起的刺激,使得信号在不同神经元之间层层传递,才使得我们能够理解看到和听到的事物。 - -神经元之间传递信号的模式并不是先天决定或是一层不变的,比如刚出生的婴儿显然不能理解大人对它呢喃的话语,不同文化的人们看到同一事物也会产生不同的反映。因此,可以通过后天的训练与学习来改变神经元之间传递信号的模式,实际上,这也是人类学习和认识世界的机理所在。 - -同时,相关研究也指出,不同功能皮层的神经元之间也不存在差异。实验人员将视觉皮层损坏的小白鼠的视觉神经接到了听觉皮层,经过一段时间后,小白鼠就可以用听觉皮层来看到事物了,类似的实验在其他功能皮层也得到了成功。这说明,不同功能皮层的神经元本来是没有差异的,是后天的训练使得它们具有了自己特殊的信号传递方式。 - -可以看到,一个个简单的神经元,竟然使人类拥有了认识复杂世界的能力。造物主似乎偏爱这样的模式,即『复杂源于简单』,DNA 和蛋白质都是相关的例子。神经网络算法就是采用这样思路,即建立若干神经元,通过这些神经元的相互连接,来模拟人类大脑的行为。 - -### 模型表示 - -> 单个神经元模型 - -每一个神经元都是一个**逻辑回归单元**(`logistic regression unit`),它接受多个输入信号,产生一个输出信号,如下图所示: - -![neuron_model](images/neuron_model.jpg) - -不妨设 - -$$ -x = \left[ -\begin{matrix} -x_0\\\\ -x_1\\\\ -x_2\\\\ -x_3 -\end{matrix} -\right],\ -\theta = \left[ -\begin{matrix} -\theta_0\\\\ -\theta_1\\\\ -\theta_2\\\\ -\theta_3 -\end{matrix} -\right] -$$ - -这里仍然有$x_0 = 1$,是手动添加的偏置单元。这样,$\hat{y}$满足逻辑回归的假设函数 - -$$ -\hat{y} = h_\theta(x) = sigmoid(\theta^T x) -$$ - -> 神经网络模型 - -神经网络是由多个层次的神经元组成的,每个层次含有数量可以是不相同的神经元,一个简单的神经网络结构如下图所示: - -![nn_model](images/nn_model.jpg) - -可以看到,任何一个神经网络,都包含一个**输入层**,一个**输出层**,以及若干数量不确定的**隐含层**(`hidden layer`)。输入层一般用$x$或者$a^{(1)}$来表示,其中$x_j$或者$a_j^{(1)}$表示第$j$个特征的值;第一个隐含层用$a^{(2)}$来表示,$a_j^{(2)}$表示第一个隐含层的第$j$个神经元的值,后面的隐含层以此类推;用$L$表示神经网络含有的层次数量,这样输出层就可以用$y$或者$a^{(L)}$来表示了。 - -由于不同层次含有的神经元数量未必相同,通常使用$S_l$来表示第$l$层所包含的神经元数量,上图中,有$S_1 = 3, S_2 = 3, S_3 = 1$。 - -根据上面的神经元模型,相邻层次的神经元之间具有一定的信号传递关系,该传递关系实际上是对上一层次的全部神经元(加上偏置单元)的加权和做逻辑函数`sigmoid function`,各个神经元的权重可以理解为相邻两层神经元之间的练线上的数值,权重越大说明该神经元对于下一个层次的神经元的影响越大。相邻层次间神经元信号传递的权重可以构成一个矩阵,用$\Theta$表示,其中第$l$层和第$l + 1$层的权重矩阵可以表示为$\Theta^{(l)}$,它的每一个行向量都对应了第$l + 1$层的一个神经元的全部权重信息,因此该行向量的维度为$S_l + 1$,而$\Theta^{(l)}$一共具有$S_{l + 1}$行,一般地有 - -$$ -\begin{aligned} -a_1^{(2)} = sigmoid(\Theta_{10}^{(1)}x_0 + \Theta_{11}^{(1)}x_1 +\Theta_{12}^{(1)}x_2 + \Theta_{13}^{(1)}x_3)\\\\ -a_2^{(2)} = sigmoid(\Theta_{20}^{(1)}x_0 + \Theta_{21}^{(1)}x_1 +\Theta_{22}^{(1)}x_2 + \Theta_{23}^{(1)}x_3)\\\\ -a_3^{(2)} = sigmoid(\Theta_{30}^{(1)}x_0 + \Theta_{31}^{(1)}x_1 +\Theta_{32}^{(1)}x_2 + \Theta_{33}^{(1)}x_3) -\end{aligned} -$$ - -这就是相邻层次间神经元信号传递的模型。 - -> 前向传播算法 - -实际上,通过上面的公式已经可以窥见前向传播算法的大概了,将其写成矩阵形式,有 - -$$ -a^{(2)} = \left[ -\begin{matrix} -a_1^{(2)}\\\\ -a_2^{(2)}\\\\ -a_3^{(2)} -\end{matrix} -\right] = sigmod(\Theta^{(1)} a^{(1)}) -$$ - -通过给$a^{(2)}$添加偏置项$a_0^{(2)} = 1$,可以写出输出$y$的表达式 - -$$ -y = a^{(3)} = sigmoid(\Theta_{10}^{(2)}a_0^{(2)} + \Theta_{11}^{(2)}a_1^{(2)} +\Theta_{12}^{(2)}a_2^{(2)} + \Theta_{13}^{(2)}a_3^{(2)}) -$$ - -同理,也可以把它写作矩阵形式 - -$$ -y = sigmoid(\Theta^{(2)}a^{(2)}) -$$ - -实际上,对于任意复杂结构神经网络,都可以定义以下前向传播算法 - -$$ -\begin{aligned} -&a^{(1)} = x\\\\ -&z^{(2)} = \Theta^{(1)}a^{(1)}, a^{(2)} = sigmoid(z^{(2)})\\\\ -&z^{(3)} = \Theta^{(2)}a^{(2)}, a^{(3)} = sigmoid(z^{(3)})\\\\ -&\cdots\\\\ -&z^{(L)} = \Theta^{(L - 1)}a^{(L - 1)}, h_\Theta(x) = a^{(L)} = sigmoid(z^{L}) -\end{aligned} -$$ - -顾名思义,为了计算神经网络的输出,需要首先计算$a^{(2)}, a^{(3)}, \cdots$,最终才能计算出$h_\Theta(x)$,因此称为前向传播算法。训练神经网络的过程,其实就是训练这里的权重矩阵$\Theta$,这将在后面具体阐述。 - -> 为什么要使用神经网络? - -神经网络是用于解决分类问题,而在此前的 [逻辑回归总结](../logistic_regression/logistic_regression.md) 中已经指出,借助**多项式回归**,逻辑回归模型理论上可以表示任意复杂的函数。既然如此,那为什么还要引入神经网络模型呢? - -设想一种具体的应用场景,现在需要对一张`20x20`像素的图片进行分类,模型采用逻辑回归模型。图片分类问题显然不是一个线性划分问题,因此需要使用多项式回归,考虑使用所有的低于四次(含)的项,容易看出其实这并不是一个相当复杂的模型,但是仅考虑四次项就已经有$C_{400}^4$,即大约有10亿项。需要指出的是,这里使用的图片还只是`20x20`像素的,对于实际中常见分辨率的图片,如`480*360`,将具有更高维的特征向量,这是现有的计算能力不能满足的。 - -而使用神经网络模型则不然,因为不需要手工指定特征向量的形式,只将初始的400个像素点作为输入即可。这是因为,神经网络的多个隐藏层,足以形成足够复杂的模型,而无需手动引入多项式。 - -实际上,神经网络的一个主要优势在于,**神经网络可以自行学习特征**,而不需要像线性回归或者逻辑回归那样手工给定特征向量。对于上面图中的简单神经网络,如果只看隐藏层以及输出层,容易发现这就是一个逻辑回归模型,而此前的层次的功能,则可以理解为对特征的学习,隐藏层的值就是学到的特征向量。由于`sigmoid`函数是一个非线性函数,再加上多个隐藏层,神经网络模型足以学习到相当复杂的特征,而无需像逻辑回归那样手工指定高次多项式。 - -> 多分类问题。 - -如逻辑回归一样,神经网络也可以被推广到解决多分类问题,两者的方法如出一辙。对于逻辑回归而言,为了解决$n(n \ge 3)$分类问题,只需要训练$n$个二类分类问题的分类器即可;而对于神经网络模型,则只需要将输出层的神经元增加到$n$个,由于前面已经指出,神经网络的最后两个层次本质上就是逻辑回归模型,因此该方法本质上也是训练了$n$个二类分类器,与逻辑回归一致。多分类的神经网络结构如下所示: - -![nn_multiclass](images/nn_multiclass.jpg) - -但是两者在实现的细节上也有一些差异。对于$n$类分类问题,逻辑回归的输出的取值往往是$\{1, 2, \cdots, n\}$,而神经网络则要沿用向量表示法,例如当$n = 3$时,神经网络的输出应该表示为 - -$$ -y \in \{\left[ -\begin{matrix} -1\\\\ -0\\\\ -0 -\end{matrix} -\right],\ -\left[ -\begin{matrix} -0\\\\ -1\\\\ -0 -\end{matrix} -\right],\ -\left[ -\begin{matrix} -0\\\\ -0\\\\ -1 -\end{matrix} -\right]\} -$$ diff --git a/thu_dsa/chp11/string.md b/thu_dsa/chp11/string.md deleted file mode 100644 index 6f218a1..0000000 --- a/thu_dsa/chp11/string.md +++ /dev/null @@ -1,13 +0,0 @@ -Conclusions on String -==================== - -+ `串`的特点:串长要远远大于字母表的大小 - - -## KMP -> 为什么要取最长自匹配? -快速右移 + 避免回退。(怎么理解) - -## BM -kmp是利用经验的高手,那么bm就是吸取教训的高手 -适用于大字符集 diff --git a/thu_os/answers.md b/thu_os/answers.md deleted file mode 100644 index 3569820..0000000 --- a/thu_os/answers.md +++ /dev/null @@ -1,5 +0,0 @@ -操作系统错题答案 -============== - -1. C -
分页方式不会有外碎片 diff --git a/thu_os/mistakes.md b/thu_os/mistakes.md deleted file mode 100644 index 015232a..0000000 --- a/thu_os/mistakes.md +++ /dev/null @@ -1,7 +0,0 @@ -操作系统错题 -=========== - -1. 在使能分页机制的情况下,更合适的外碎片整理方法是() - + 紧凑(compaction) - + 分区对换(swapping in/out) - + 都不是 diff --git a/words.md b/words.md deleted file mode 100644 index f02e0b3..0000000 --- a/words.md +++ /dev/null @@ -1,2261 +0,0 @@ -Some Words -========= - -## Words List -+ euclidean -> (adj)relating to geometry as developed by Euclid - - - euclidean distance - - euclidean geometry - -+ zip through -> (v)to go somewhere or do something very quickly - - - If you zip through a page in less than an hour, you are probably going too fast. - - A lot of questions zip through his mind. - -+ internalize -> (v)if you internalize a particular belief, attitude, behaviour etc, it becomes part of your character - - - Over time, she internalized her parents' attitudes. - - You should ponder and internalize each definition. - -+ beige -> (adj)a pale brown colour - - - The walls are beige. - - Definitions are in beige boxes and theorems are in blue boxes. - -+ bestow -> (v)to give someone something of great value or importance - - - honours bestowed on him by the Queen - - Bestowing proper credit on all the contributors would be a difficult task that I have not undertaken. - -+ in lieu of -> (adv)in place of - - - extra time off in lieu of payment - -+ preliminary -> (adj)happening before something that is more important, often in order to prepare for it
- (n)something that is said or done first, to introduce or prepare for something else
- (n)one of the games in the first part of the competition, when it is decided who will go to the main competition - - - a preliminary draft - - After the usual preliminaries, the chairman made his announcement. - - Four teams will be eliminated in the preliminaries. - -+ arithmetic -> (n)the science of numbers involving adding, multipying, etc. - - - teaching the usual rules of reading, writing and arithmetic. - -+ derive -> (v)to get something, especially an advantage or a pleasant feeling, from something
- (v)to develop from something else
- (v)to get a chemical substance from another substance - - - Many students derived enormous satisfaction from the course. - - patterns of behaviour that derive from basic beliefs - - The enzyme is derived from human blood. - -+ enzyme -> (n)a chemical substance that is produced in a plant or animal, and helps chemical changes to take place in the plant or animal. - -+ commutativity -> (n)In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. - -+ cumbersome -> (adj)A cumbersome system or process is very complicated and inefficient.
- (adj)large and heavy and therefore difficult to carry, wear, or handle - - - Although the machine looks cumbersome, it is actually easy to use. - - and old and cumbersome computer system - -+ analogue -> (n)something having the property of being analogous to something else. - -+ pentagon -> (n) a flat shape with five sides and five angles - -+ hexagon -> (n) a shape with six sides - -+ equilateral -> (adj)A shape or a figure that is equilateral has sides that are all the same length.(equi + lateral) - - - an equilateral triangle - -+ lateral -> (adj)relating to the sides of something, or movement to the side - - - He estimated that the lateral movement of the bridge to be between four and six inches. - -+ temperate -> (adj)Temperate is used to describe a climate or a place which is never extremely hot or extremely cold. - - - The Nile Valley keeps a temperate climate throughout the year. - -+ in rude health ->(adv)to be extremely healthy - - - To that extent at least, American democracy remains in rude health. - -+ desolate -> (adj)A desolate place is empty of people and lacking comfort.
- (adj)A desolate person feels sad, lonely and without hope. - - - a desolate landscape of flat green fields - - He was desolate without her. - -+ whence -> from where - - - No one ordered him back whence he came. - -+ asylum -> (n)protection given to someone by a government because they escaped from fighting or political trouble in their own country
-> (n)a mental hospital - - - He has been granted (political) asylum in France. - - He applied for asylum in 1987 after fleeing the police back home. - -+ lunatic -> (n) someone who behaves in a crazy or very stupid way -- often used humorously
-> (n) a very offensive word for someone who is mentally ill
-> (adj)insane and believed to be affected by the phases of moon
- - - The hotel is run by a lunatic! - - a dangerous lunatic - - lunatic behaviour; lunatic asylum = asylum - -+ legislature -> (n)an institution that has the power to make or pass laws - - - The proposals before the legislature include the creation of two special courts to deal exclusively with violent crimes. - -+ truce -> (n)an agreement between enemies to stop fighting or arguing for a short time, or the period for which this is arranged, ceasefire. - - - They agreed to call a truce. - - There was an uneasy truce between Alex and Dave over dinner. - -+ animate -> (adj)living
- (v)to give life or energy to something - - - Natural philosophy involved the study of all aspects of the material world, animate and inanimate. - - Laughter animated his face for a moment. - -+ wretched -> (adj)someone who is wretched is very unhappy or ill, and you feel sorry for them
- (adj)Someone who feels wretched feels very unhappy
- (adj)You use wretched to describe someone or something that you dislike or feel angry with
- (adj)extremly bad or unpleasant; miseralbe - - - the poor, wretched girl - - I feel really wretched and confused. - - Wretched woman, he thought, why the hell cannot she wait? - - I was shocked to see their wretched living conditions. -+ formidable -> (adj)very powerful or impressive, and often frightening - - - The new range of computers have formidable processing power. - - The building is grey, formidalbe, not at all picturesque. - -+ picturesque -> (adj)a picturesque place is pretty and interesting in an old-fashioned way.
- (adj)picturesque languages uses unusual, interesting, or sometimes rude words to describe something
- (n)picturesque things - - - a quiet fishing village with a picturesque harbour - - a picturesque account of his trip to New York - - lovers of the picturesque - -+ thence -> (adj)from there or following that - - - We went to Trieste, and thence by train to Prague. - - the conversion of sunlight into heat and thence into electricity - -+ posterity -> (n)all the people in the future who will be alive - - - a priceless work of art that must be kept for posterity - -+ diligent -> (adj)someone who is diligent works hard and is careful and thorough - - - a diligent student - -+ skilful -> (adj) = skillful(American) - -+ offspring -> (n)a person's children or an animal's young - - - Eleanor was now less anxious about her offspring than she had once been. - - a young mother trying to control her offspring - -+ approximate -> (adj)close but not exact
- (v)to be close to a particular number
- (v)to be similar to but not exactly the same as something - - - This figure approximates to a quarter of the UK's annual consumption. - - Your story only approximates to the real facts. - -+ antecedent -> (n)An antecedent of something happened or existed before it and was similar to it in some ways. - - - We shall first look briefly at the historical antecedents of this theory. - -+ frugal -> (adj)careful to buy only what is necessary; (oppo)extravagant
-> frugality(n) - - - He led a remarkably frugal existence. - -+ henceforth -> (adv)from this time on - - - never to permit the child henceforth to enter his former home - -+ sanitary -> (adj)relating to the ways that dirt, infection, and waste are removed, so that places are clean and healthy for people to live in
- (adj)clean and not causing any danger to people's health - - - Diseases were spread through poor sanitary conditions. - - Often, the camps were not very sanitary. - -+ hereditary -> (adj)A hereditary characteristic or illness is passed on to a child from its parents before it is born
- (adj)A title or position in society that is hereditary is one that is passed on as a right from parent to child - - - a hereditary disease - - The position of the head of the state is hereditary. - -+ serf -> (n)In former times, serfs were a class of people who had to work on a particular person's land and could not leave without that person's permission - -+ squalor -> (n)the condition of being dirty and unpleasant because of a lack of care or money - - - He was out of work and living in squalor. - -+ aristocracy -> (n)a class of people in some countries who have a high social rank and special titles. - - - a member of the aristocracy - -+ rabble -> (n)a noisy crowd of people who seem likely to cause trouble - - - a rabble of angry youths - -+ destitute -> (adj)having no money, no food, no home, etc
- be destitute of sth: to be completely without something - - - The floods left many people destitute. - - a man who is destitute of mercy - -+ sedition -> (n)speech, writing, or actions intended to encourage people to disobey a government - - - Government officials charged him with sedition. - -+ ordinance -> (n)a law, usually of a city or town, that forbids or restricts an activity - - - a city ordinance that says park must be closed at 11 p.m. - - a wise ordinance of Nature - -+ wax -> (v)to become larger, more powerful, etc - - - The moon is waxing. - -+ wane -> (v)If something wanes, it becomes gradually weaker or less, often so that it eventually disappears. - - - My enthusiasm for the project was waning. - - Interest in the show has waxed and waned. - -+ perish -> (v)to die, especially in a terrible or sudden way. - - - Hundreds perished when the ship went down. - - Most of the butterflies perish in the first frosts of autumn. - -+ annals -> (n, pl)in the annals of sth, means in the whole history of sth - - - one of the most unusual cases in the annals of crime - - He has become a legend in the annals of military history. - -+ mentor -> (n)an experienced person who advises and helps a less experienced person
- (v)to give someone help and advice over a period of time, especially help and advice related to their job - - - Leon Sullivan was my mentor and my friend. - - He had mentored scores of younger doctors. - -## 8th, Sep - -+ genome -> (n)all the genes in one type of living thing - - - the human genome - -+ kin -> (n)your relatives - -+ kinship -> (n)a family relationship
- (n)a strong connection between people - - - The ties of kinship may have helped the young man find his way in life. - - She evidently felt a sense of kinship with the woman. - -+ befriend -> (v)to behave in a friendly way towards someone, especially someone who is younger or needs help - - - They befriended me when I first arrived in London as a student. - - The film's about an elderly woman and a young nurse who befriends her. - -+ ancestry -> (n)the members of your family who lived a long time ago - - - Her mother is of German ancestry. - - Helen's family can trace their ancestry back to the 1700s. - -+ ethnic -> (adj)relating to a particular race, nation, or tribe and their customs and traditions.
- (adj)cooking, fashion etc from countries that are far away, which seems very different and unusual. - - - The school teaches pupils from different ethnic groups. - - ethnic music, a magnificent range of ethnic fabrics - -+ see -> (v)make sure that something is done - - - See that you take care of her. - -+ disrupt -> (v)to prevent something from continuing in its usual way by causing problems - - - Climate change could disrupt the agricultural economy. - - Anti-war protestors disrupted the debate. - -+ disruptive -> (adj)causing problems and preventing something from continuing in its usual way. - - - way to handle disruptive pupils - - Alcohol can produce violence, disruptive behaviour. - -+ pick pace -> (v)pick up speed - - - Pick up the pace, guys - we are late. - -## 9th, Sep - -+ abdicate -> (v)to give up the position of begin king or queen
- (v)to refuse to be responsible for something, when you should be or were before - - - The king was forced to abdicate the throne. - - The government has largely abdicated its responsibility in dealing with housing needs. - -+ the writing is on the wall -> mean that there are clear signs that something will fail or no longer exist - - - The writing is on the wall for the local football club: bankruptcy seems certain. - -+ monarch -> (n)a king or queen - - - He was an absolute monarch.(arbitrary king) - -+ monarchy -> (n)a system in which a country is ruled by a king or queen.
- (n)a country that has a monarch - - - a serious debate about the future of the monarchy - - Britain is a constitutional monarchy - -+ polarize -> (v)to divide into clear separate groups with opposite beliefs, ideas, or opinions, or to make people do this - - - The issue has polarized the country. - - As the car rental industry polarizes, business will go to the bigger companies. - -+ transcendence -> (n)the quality of being able to go beyond normal limits or boundaries - - - The Arab-American Society promotes the transcendence of racial and religious differences. - -+ infest -> (v)if insects, rats etc infest a place, there are a lot of them and they usually cause damage
- (v)if things or people you do not want infest a place, there are too many of them - - - The prison is infested with rats. - - Crime and drugs are infesting the inner cities. - - an area infested with holiday homes - -+ downside -> (n)The downside of a situation is the aspect of it which is less positive, pleasant, or useful than its other aspects. - - - The downside of this approach is a lack of clear leadership. - -## 10th, Sep -+ symbolic -> (adj)Something that is symbolic of a person or thing is regarded or used as a symbol of them. - - - Yellow clothes are worn as symbolic of spring. - - Today's fighting is symbolic of the chaos which the country is facing. - -+ embody -> (v)to be a very good example of an idea or quality, represent. - - - She embodies everything I admire in a teacher. - -+ strive -> (v)to make a great effort to achieve something - - - We must continue to strive for greater efficiency. - - He strives hard to keep himself very fit. - -+ intrusive -> (adj)affecting someone's private life or interrupting them in an unwanted and annoying way. - - - They found the television cameras too intrusive. - -+ embodiment -> (n)someone or sth that represents or is very typical of an idea of quality. - - - He is the embodiment of evil. - - A baby is the embodiment of vulnerability. - -+ sweeping -> (adj)affecting many things, or making an important difference to something.
- (adj)sweeping statement: a statement etc that is too general and that does not consider all the facts. - - - They want to make sweeping changes to education policies. - - sweeping generalizations about women drivers. - -+ reckless -> (adj)not caring or worrying about the possible bad or dangerous results of your action. - - - He was accused of causing death by reckless driving. - - a reckless disregard for safety - - He ran into the burning house with reckless abandon(= without caring about the danger). - -+ discern -> (v)to notice or understand something by thinking about it carefully
-> (v)to be able to see something, but not clearly - - - Officials are keen to discern how much public support there was. - - We could just discern a town in the distance. - -+ lame -> (adj)unable to walk properly because your leg or foot is injured or weak.
-> (n)people who are lame
-> (adj)a lame excuse or explanation is weak and difficult to believe. - - - She gave some lame excuse about missing the bus. - - "Lovely house!"I said lamely. - -+ scrutiny -> (n)careful and thorough examination of someone or something. - - - His private life came under media scrutiny. - - Careful scrutiny of the company's accounts revealed a whole series of errors. - -+ dearth -> (n)a situation in which there are very few of something that people want or need. - - - a dearth of job opportunties. - - Construction had slowed because of a dearth of labourers. - -+ collective -> (adj)shared or made by every member of a group or society.
-> (n)A collective is a business or farm which is run, and often owned, by a group of people. - - - a collective decision made by all board members. - - our collective responsiblity for the environment - -+ morale -> (n)the level of confidence and positive feelings that people have, especially people who work together, who belong to the same team etc. - - - The media feels pressure to keep the morale of the country up in war time. - - low staff morale - - A win is always good for morale. - -+ moral -> (n)principles and beliefs concerning right and wrong behaviour.
-> (adj)relating to beliefs about what is right or wrong. - - - If we accept that certain babies should be allowed to die, we place doctors in a moral dilemma. - - the corruption of public morals - -+ ethical -> (adj)relating to principles of what is right and wrong, moral.
-> (adj)morally good or correct - - - The use of animals in scientific tests raised difficult ethical questions. - - I don't think it's echical for you to accept a job you know you can't do. - -+ doctrine -> (n)a set of beliefs that form an important part of a religion or system of ideas.
-> (n)a formal statement by a government about its future plans - - - traditional doctrine of divine power - - the Marxist doctrine about perpetual revolution - - the announcement of the Truman Doctrine - -## 11th, Sep - -+ cockroach -> (n)(also roach)a large black or brown insect that lives in dirty houses, especially if they are warm and there is food to eat. - -+ idiom -> (n)a group of words that has a special meaning that is different from the ordinary meaning of each separate words. For example, 'under the weather' is an idiom meaning 'ill'. - - - familiar idioms and metaphors, such as 'turning over a new leaf' - -+ ethnicity -> (n)the state or fact of belonging to a particular ethnic group. - - - He said his ethnicity had not been important to him. - -+ discourse -> (n)a serious speech or piece of writing on a particular subject
-> (n)serious conversation or discussion between people - - - a discourse on art - - Candidates should engage in serious political discourse. - - "I do not know."And thus ended their discourse. - -+ assimilation -> (n)the process of understanding and using new ideas.
-> (n)the process of becoming an accepted part of a country or group. - - - The assimilation of ethnic Germans in the US was accelerated by the two world wars. - - Poor assimilation of vitamins and nutrients can cause health problems. - -+ mouthpiece -> (n)the part of a musical instrument, telephone etc that you put in your mouth or next to your mouth.
-> (n)a person, newspaper etc that expresses the opinions of a government or a political organization. - - - He shouted into the mouthpiece. - - This newspaper is just a Republican mouthpiece. - -+ literacy -> (n)the state of being able to read and write - - - a new adult literacy campaign - - Many adults have problems with literacy and numeracy. - -+ ascribe -> (v)to claim that something is caused by a particular person, situation etc.
-> (v)to claim that something has been written, said, made etc by a particular person - - - The report ascribes the rise in childhood asthma to the increase in pollution. - - a quotation that's often been ascribed to Marilyn Monroe - -## 13th, Sep - -+ upfront -> (adj)behaving or talking in an honest way so that people know what you really think, honest.
-> (adj)paid before any work has been done or before goods are supplied.
-> (adv)in the beginning - - - Mo's very upfront with him about their relationship. - - an upfront fee of 500 dollars - - Every consumer should know upfront what it is going to cost them. - -+ eligible -> (adj)some who is eligible for sth is able or allowed to do it, for example, because they are the right age.
-> (adj)an eligible man or woman is not yet married and is thought by many people to be a suitable partner. - - - Almost half the population are eligible to vote in today's election. - - Students on a part-time course are not eligible for a loan. - - Stephen was regarded as an eligible bachelor. - -+ treasury -> (n)a government department that controls the money that the country collects and spends.
-> (n)a place in a castle, church, palace etc where money or valuable objects are kept. - - - a senior official at the Treasury - -+ exchequer -> (n)the British government department that is responsible for collecting taxes and paying out public money, the Treasury. - - - The industry claims it contributes to the Irish exchequer because it generates employment and wealth. - -+ chancellor -> (n)a person in a position of the highest or high rank, especially in a government or university - - - A former politician has been appointed Chancellor of the university. - - Helmut Kohl, the former German Chancellor - -## 14th, Sep - -+ roundabout -> (n)a raised circular area where three or more roads join together and which cars must drive around.
-> (adj)a roundabout way of getting somewhere is longer and more complicated than necessary.
-> (adj)a roundabout way of saying something is not clear, direct or simple. - - - Turn left at the first roundabout. - - It was a roundabout way of telling us to leave. - - The bus took a very long and roundabout route. - -+ workout -> (n)a period of physical exercise, especially as training for a sport. - - - a daily workout in the gym - - Give your upper body a workout by using handweights. - -+ genre -> (n)a particular type of art, writing, music etc, which has certain features that all examples of this type share - - - a new genre of film-making - - his love of films and novels in the horror genre - -+ fluctuate -> (v)if a price or amount fluctuates, it keeps changing and becoming higher and lower. - - - Insect populations fluctuate wildly from year to year. - - Prices were volatile, fluctuating between 20 and 40. - -+ bar -> (n)all lawyers considered as a group, or the profession of law. - - - sit for the bar: take part in the bar examination - - she was admitted to the bar in her early thirties. - - Less than a quarter of graduates from the law school pass the bar exam on the first try. - -+ guild -> (n)an organization of people who do the same job or have the same interests. - - - the Women's Guild - - the Writers' Guild of America - -+ hindrance -> (n)something or someone that makes it difficult for you to do something.
-> (n)the act of making it difficult for someone to do something - - - The higher rates have been a hindrance to economic recovery. - - The floods have been a major hindrance to relief efforts. - - Visitors are allowed to wander without hindrance. - -+ lucrative -> (adj)a job or activity that is lucrative lets you earn a lot of money, profitable. - - - He inherited a lucrative business from his father. - - Thousands of ex-army officers have found lucrative jobs in private security firms. - -+ upstart -> (n)someone who behaves as if they were more important than they really are and who shows a lack of respect towards people who are more experienced and or older. - - - Many prefer a familiar authority figure to a yound upstart. - -+ status quo -> (n)the present situation - - - Certain people always want to maintain the status quo. - -+ lone -> (adj)used to talk about the only person or thing in a place, or the only person or thing that does something. - - - the lone survivor of the shipwreck - - A lone figure was standing at the bus stop. - -+ outgrow -> (v)to grow too big for something
-> (v)to no longer do or enjoy something that you used to, because you have grown older and changed - - - They outgrew their clothes so quickly. - - Most children eventually outgrow a tendency towards travel sickness. - -+ liberal -> (adj)willing to understand and respect other people's ideas, opinions, and feelings.
-> (adj)allowing people or organizations a lot of political or economic freedom
-> (adj)giving, using, or taking a lot of something, or existing in large quantities - - - I had quite liberal parents. - - a liberal democracy with a multiparty political system - - As always he is liberal with jokes. - - Chemical products were used liberally over agricultural land. - -+ critical -> (adj)if you are critical, you criticize someone or something. - - - Many economists are highly critical of the government's economic policies. - - Many parents are strongly critical of the school. - -+ contemptuous -> (adj)showing that you think someone or something deserves no respect. - - - a contemptuous glance - - He's openly contemptuous of all the major political parties. - -+ set back -> (v)if something sets you back or sets back a project or plan, it causes a delay. - - - It has set us back in so many aspects that I'm not sure how long it will take for us to catch up. - -+ citizenry -> (n)all the citizens in a particular town, country, or state. - - - The country's citizenry is(are) more politically aware than in the past. - -+ propaganda -> (n)information which is false or which emphasizes just one part of a situation, used by a government or political group to make people agree with them. - - - the government propaganda machine - - the spreading of political propaganda - -## 18th, Sep - -+ sift -> (v)to put flour, sugar etc through a sieve or similar container in order to remove large pieces.
-> (v)to examine information, documents etc carefully in order to find something out or decide what is important and what is not. - - - Sift the flour and baking powder into a medium-sized mixing bowl. - - Police are sifting through the evidence. - - It' hard to sift out the truth from the lies in this case. - -+ ornate -> (adj)covered with a lot of decoration - - - an ornate gold mirror - - an ornate iron staircase - -+ tentative -> (adj)not definite or certain, and may be changed later.
-> (adj)done without confidence, hesitant - - - I passed on my tentative conclusions to the police. - - The government is taking tentative steps towards tackling the country's economic problems. - - Albi knocked tentatively and entered. - -+ semaphore -> (n)a system of sending messages using two flags, which you hold in different positions to represent letters and numbers - -## 19th, Sep - -+ glitch -> (n)a small fault in a machine or piece of equipment, that stops it working - - - a software glitch - - Manufacturing glitches have limited the factory's output. - -## 23rd, Sep - -+ akin -> (adj)very similar to something - - - Something akin to panic overwhelmed him. - - Listening to his life story is akin to reading a good adventure novel. - -+ hijack -> (v)to use violence or threats to take control of a plane, vehicle, or ship.
-> (v)to take control of something and use it for your own purposes - - - The airliner was hijacked by a group of terrorists. - - Some people think the party has been hijacked by right-wing extremists. - -+ satire -> (n) a way of criticizing something such as a group of people or a system, in which you deliberately make them seem funny so that people will see their faults
-> (n)a piece of writing, film, play etc that uses this type of criticism. - - - the characteristic use of satire in Jonson's work - - a satire on the American political process - -+ get cracking -> (v)to start doing something quickly - - - I'd better get cracking on the food for tonight. - -+ utopia -> (n)an imaginary perfect world where everyone is happy - - - When plans to rebuild the neighborhood were first announced, I’d been hoping for a utopia. - -+ asteroid -> (n)one of the many planets that move around the Sun, especially between Mars and Jupiter - -+ envisage -> (v)to think that something is likely to happen in the future - - - The scheme cost a lot more than we had originally envisaged. - - It's hard to envisage how it might happen. - -+ fad -> (n)something that people like to do for a short time, or that is fashionable for a short time. - - - He doesn't believe environmental concern is a passing fad. - -+ rosy -> (adj)seeming to offer hope of success or hapiness
-> (adj)pink - - - The job prospects for those graduating in engineering are far less rosy now than they used to be. - - Letters to relatives in Europe painted a rosy picture of life in the United States. - - the kid's rosy cheeks - -+ enforce -> (v)the make people obey a rule or law
-> (v)to make something happen or force someone to do something - - - Parking restrictions will be strictly enforced. - - Governments make laws and the police enforce them. - - It is unlikely that a record company would enforce its views on an established artists. - -+ statute -> (n)a law passed by a council, parliament etc and formally written down
-> (n)a formal rule of an institution or organization - - - Protection for the consumer is laid down by statute. - - College statutes forbid drinking on campus. - -+ check -> (v)to stop something bad from getting worse or continuing to happen - - - The police are failing to take adequate measures to check the growth in crime. - -+ to the letter -> (adv)if you obey instructions or rules to the letter, you do exactly what you have been told to do, giving great attention to every detail. - - - I followed the instructions to the letter and it still went wrong. - -+ envision -> (v)to imagine something that you think might happen in the future, especially something that you think will be good, envisage. - - - I envisioned a future of educational excellence. - - The company envisions adding at least five stores next year. - -+ fashion -> (v)to make or shape something, using your hands or only a few tools - - - He fashioned a box from a few old pieces of wood. - -+ flourish -> (v)to develop well and be successful
-> (v)to grow well and be very healthy - - - The economy is booming and small businesses are flourishing. - - Most plants will flourish in the rich deep soils here. - -+ eradicate -> (v)to completely get rid of something such as a disease or a social problem. - - - an attempt to eradicate inflation - - a significant contribution towards the eradication of corruption - -+ radical -> (adj)a radical change or difference is very big and important
-> (adj)believing or expressing the belief that there should be great or extreme social or political change. - - - He was known as a radical reformer. - - They are proposing radical changes to the way the company is run. - - There are radical differences between the two organizations. - -+ paraphrase -> (v)to express in a shorted, clearer, or different way what someone has said or written. - - - To paraphrase President Bush, we must restore confidence in our economic sector. - -+ enact -> (v)to put something into action, especially to make something law
-> (v)to perform a story or play by acting - - - The authorities have failed so far to enact a law allowing unrestricted emigration. - - a project that is to be enacted next year - - She often enacted the stories told to her by her father. - -## 24th, Sep - -+ demographics -> (n)the number and characteristics of people who live in a particular area or form a particular group, especially in relation to their age, how much money they have and what they spend it on - - - The demographics of the country have changed dramatically in recent years. - - demographic change - -+ stem -> (v)to stop something from happening, spreading, or developing.
-> (V)to stop the flow of liquid - - - The measures are meant to stem the tide of illegal immigration. - - an attempt to stem the decline in profits - - A tight bandage should stem the bleeding. - -+ up in arms -> angry or upset - - - The union is up in arms over the reduction in health benefits. - -## 30th, Sep - -+ legitimacy -> (n)the quality of being legal - - - The lawyers expressed serious doubts about the legitimacy of military actions. - -+ impartial -> (adj)not involved in a particular situation, and therefore able to give a fair opinion or piece of advice - - - A trial must be fair and impartial. - - The state must ensure the independence and impartiality of the justice system. - -+ code of conduct -> (n)a set of rules about how to behave and do business with other people - - - Doctors say a new code of conduct is urgently needed to protect the doctor-patient relationship. - -+ judiciary -> (n)the part of a country's government that is responsible for its legal system, including all the judges in the country's courts - - - The judiciary must think hard before jailing nonviolent offenders. - -+ ideological -> (adj)based on strong beliefs or ideas, especially political or economic ideas - - - The party is split by ideological differences. - - There are some fairly profound ideological disagreements within the movement. - -+ dismiss -> (v)to refuse to consider someone's idea, opinion, etc, because you think it is not serious, true, or important. - - - The government has dismissed criticisms that the country's health policy is a mess. - - He just laughed and dismissed my proposal as unrealistic. - -+ address -> (v)if you address a problem, you start trying to solve it. - - - Our products address the needs of real users. - - The government has to address the rise in violent crimes. - -+ accountable -> (adj)responsible for the effects of your actions and willing to explain or be criticised for them - - - The hospital should be held accountable for the quality of care it gives. - - Managers must be accountable to their decisions. - - The government should be accountable to all the people of the country. - -## 2nd, October - -+ contend -> (v)to compete against someone in order to gain something
-> (v)to argue or state that something is true - - - Inevitably, fights break out between the members of the contending groups. - - the two main groups contending for power - - Some astronomers contend that the universe may be younger than previously thought. - -+ perceptive -> (adj)someone who is perceptive notices things quickly and understands situations, people's feelings etc well - - - highly perceptive comments - - He was one of the most perceptive U.S. political commentators. - -+ spot-on -> (adj)exactly right - - - Judith is always spot-on with her advice. - -+ virtuous -> (adj)behaving in a very honest and moral way.(oppo) wicked - - - Sue considered herself very virtuous because she neither drank nor smoked. - - steer yound men in virtuous directions - -+ abide -> (v)used to say that someone dislikes someone or something very much
-> (v)abide by: to accept and obey a decision, agreement etc, even though you may not agree with it - - - I can't abide that man -- he's so self-satisfied. - - You have to abide by the referee's decision. - -## 3rd, October - -+ reaffirm -> (v)to formally state an opinion, belief, or intention agian, especially when someone has questioned you or expressed a doubt - - - The party reaffirmed its commitment to nuclear disarmament. - - The government has reaffirmed that education is a top priority. - -+ disarmament -> (n)the act of reducing the number of weapons, especially nuclear weapons, that a country has. - - - The goal would be to increase political stability in the region and accelerate the pace of nuclear disarmament. - -+ dishonour -> (v)to refuse to keep an agreement or promise - - - The government has been accused of dishonouring its pledge to upgrade London's underground network. - -+ pledge -> (n)s serious promise or agreement, especially one made publicly or officially.
-> (v)to make a formal, usually public promise that you will do something, promise. - - - the government's pledge to make no deals with terrorists - - Eisenhower fulfilled his election pledge to end the war. - - The new governor pledged to reduce crime. - - He pledged his cooperation. - -+ evade -> (v)to avoid talking about something, especially because you are trying to hide something.
-> (v)to not do or deal with something that you should do
-> (v)to avoid paying money that you ought to pay, for example tax. - - - I could tell he was trying to evade the issue. - - The minister evaded the question. - - You can't go on evading your responsibilities in this way. - - Employers will always try to find ways to evade tax. - -+ patchwork -> (n)a type of sewing in which many coloured squares of cloth are stitched together to make one large piece
-> (n)something that is made up of a lot of different things - - - beds covered in patchwork quilts - - a patchwork of woods and fields, typical of the English countryside - - The area was a patchwork of local industries. - -+ defy -> (v)to refuse to obey a rule or law, or refuse to do what someone in authority tells you to do, disobey
-> (v)defy description/analysis/belief etc: to be almost impossible to describe or understand - - - This was my first (and last) time that I dared to defy my mother. - - The beauty of the scene defies description - -+ stake -> at stake: if something is at stake, it is being risked and might be lost or damaged if you are not successful.
-> (n)the stakes involved in in a contest or a risky action are the things that can be gained or lost
-> (v)if you stake something such as your money or your reputation on the result of something, you risk your money or reputation on it.
-> (n)if you have a stake in a business, you have invested money in it.
-> have a stake in sth: if you have a stake in something, you will get advantages if it's successful, and you feel you have an important connection with it. - - - The tension was naturally high for a game with so much at stake. - - The game was usually played for high stakes between two large groups. - - He has staked his political future on an election victory. - - He holds a 51% stake in the firm. - - Young people don't feel they have a stake in the country's future. - -+ laureate -> (n)someone who has been given an important prize or honour, especially the nobel prize - - - Nigeria's Nobel laureate, Wole Soyinka. - -+ hardline -> (adj)firm and uncompromising - - - a hardline politician - -+ rally -> (n)a large public meeting, especially one that is held outdoors to support a political idea, protest etc.
-> (v)to come together, or to bring people together, to support an idea, a political party etc.
-> (v)to return to a better condition - - - 5,000 people held an anti-nuclear rally - - We decided to hold a rally to put pressure on the government. - - The general rallied his forces to defend the town. - - The president has called on the people to rally behind the government. - - The nurse said my mother had rallied after a poor night. - - The team played badly in the first half of the game but rallied in the second. - -+ clamp -> (v)to hold two things together using a clamp
-> (v)to put or hold something in a position so that it cannot move
-> (v)to put limits on what someone is allowed to do
-> (v)clamp down: to take firm action to stop a particular type of crime - - - She clamped her hands over her ears. - - The President clamped sanctions on the island after the bomb attack. - - The police are clamping down on drink-driving offenders. - -+ teamster -> (n)someone whose job is to drive a truck - -+ egregious ->(adj)extremely bad in a way that is very noticeable. - - - the most egregious abuses of human rights - - It was an egregious error for a statesman to show such ignorance. - -+ ignorant -> (adj)not knowing facts or information that you ought to know - - - Political historians are often rather ignorant of economics. - - an ignorant and uneducated man - -+ resort -> (n)last/final resort: what you will do if everything else fails
-> (v)resort to: to do something bad, extreme, or difficult because you cannot think of any other way to deal with a problem - - - Drug treatment should only be used as a last resort. - - Nuclear weapons should be used only as a last resort. - - Officials fear that extremists may resort to violence. - - His punishing work schedule had made him resort to drugs. - -+ constituent -> (n)one of the substances or things that combine to form something
-> (n)someone who votes in a particular area
-> (adj)being one of the parts of something - - - Oxygen is a constituent of air. - - He told his constituents that he would continue to represent them to the best of his ability. - - Let's look at the constituent parts of this sentence. - - the EU and its constituent members - -+ superfluous -> (adj)more than is needed or wanted, unnecessary - - - a modern building with no superfluous decoration - -## 4th, October - -+ sanction -> (n)official orders or laws stopping trade, communication etc with another country, as a way of forcing its leaders to make political changes
-> (n)official permission, approval, or acceptance - - - a resolution to impose sanctions on DPRK - - the threat of trade sanctions - - a newspaper run by citizens without the sanction of the government - -## 11th, October - -+ tone -> (n)how firm and strong your muscles or skin are - - - A regular brisk walk will improve muscle tone. - -+ brisk -> (adj)quick and full of energy
-> (adj)quick, pratical, and showing that you want to get things done quickly
-> (adj)trade or business that is brisk is very busy, with a lot of products being sold
-> (adj)weather that is brisk is cold and clear - - - They set off at a brisk pace. - - Her tone of voice is brisk. - - The public bar was already doing a brisk trade. - - a typically brisk winter's day on the south coast - -+ conceivable ->(adj)able to be believed or imagined - - - It is conceivable that you may get full compensation, but it's not likely. - - We were discussing the problems from every conceivable angle. - - Conceivably, interest rates could rise very high indeed. - -+ subconscious ->(adj)subconscious feelings, desires, etc are hidden in your mind and affect your behavious, but you do not know that you have them - - - a subconscious fear of failure - - Subconciously, he blames himself for the accident. - -+ acclaim -> (v)to praise someone or something publicly.
-> (n)public praise for something or someone - - - His work was acclaimed by art critics. - - Their recordings have won great acclaim. - -+ hooray -> shouted when you are very glad about something - -+ sober -> (adj)not drunk
-> (adj)serious, and thinking or making you think carefully about things.
-> (adj)plain and not at all brightly coloured - - - He's a nice guy when he's sober. - - a sober, hard-working yound man - - It was a room filled with sober faces. - - He dressed in sober grey suits. - -+ hitherto -> (adv)up to this time - - - a species of fish hitherto unknown in the west - -+ damn sb with faint praise -> (v)to praise someone so slightly that it suggests you do not really adimre them - - - He was so jealous of his colleague's talent that he tried to damn him with faint praise. - -+ inferior -> (adj)not good, or as good as someone or something else, opp superior
-> (adj)lower in rank - - - I felt inferior among all those academics. - - wine of inferior quality - - Their performance was inferior to that of other teams. - - an inferior court of law - - He refused to accept a job of inferior status. - -+ philharmonic -> used in the names of musical groups, especially orchestras - - - He will conduct the Berlin Philharmonic in the final concert of the season. - - the Vienna Philharmonic Orchestra. - -+ vibrant -> (adj)full of activity or energy in a way that is exciting and attractive, lively
-> (adj)a vibrant colour is bright and strong - - - Tom felt himself being drawn towards her vibrant personality. - - a painting full of vibrant reds and blues - -## 12th, October - -+ cloak ->(v)to deliberately hide facts, feelings etc so that people do not see or understand them -- used especially in news report
->(v)to cover something, for example with darkness or snow
->(n) - - - He has always kept his love affairs cloaked in secrecy. - - hills cloaked in mist - - prejustice hiding under the cloak of religion - - The political party is used as a cloak for terrorist activities. - -+ pronouncement -> (n)an official public statement - - - the Pope's last pronouncement on birth control - - the President's lastest pronouncement about the protection of minorities - -+ turbulent -> (adj)a turbulent situation or period of time is one in which there are a lot of sudden changes
-> (adj)turbulent air or water moves around a lot - - - the turbulent times of the French Revolution - - He has had a turbulent political career. - - the dark turbulent waters of the river - -+ abound -> (v)to exist in very large numbers - - - Stories abound about when he was in charge. - - The forests abound with deer, birds, and squirrels. - -+ deputy -> (n)someone who is directly below another person in rank, and who is officially in charge when that person is not there - - - the deputy Secretary of State - -+ turnover -> (n)the amount of business done during a particular period, profit
-> (n)the rate at which people leave an organization and are replaced by others - - - The company had a turnover of $3.8 million. - - Short-term contracts increase staff turnover. - - Low pay accounts for the high turnover. - -+ spur -> (n)a fact or event that makes you try harder to do something
-> (n)a sharp pointed object on the heel of a rider’s boot which is used to encourage a horse to go faster
-> (v)to encourage someone or make them want to do something
-> (v)to make an improvement or change happen faster, encourage - - - We would often decide what to play on the spur of the moment. - - a belief in competition as a spur to efficiency - - The band were spurred on by the success of their last two singles. - - Lower taxes would spur investment and help economic growth. - -+ leverage -> (n)the action or advantage of using a lever
-> (n)influence that you can use to make people do what you want
-> (v)to use borrowed money to buy an investment or company
-> (v)to use something you already have in order to achieve something new or better - - - If the United Nations had more troops in the area, it would have greater leverage. - - We can gain a market advantage by leveraging our network of partners. - - He might feel that leveraging the company at a time when he sees tremendous growth opportunities would be a mistake. - -+ infancy -> (n)the period of a child's life before they can walk or talk
-> (n)the time when something is just starting to be developed - - - In the past, many children died in infancy. - - Computing science was still in its infancy. - -+ harassment -> (n)when someone behaves in an unpleasant or threatening way towards you - - - African Americans have been complaining about police harassment for years. - - Political paries are banned, and harassment of dissidents is commonplace. - -+ allegation -> (n)a statement that someone has done something wrong or illegal, but that has not been proved. - - - Mr Singh has strongly denied the allegations of sexual harassment. - - A committee will investigate the allegations of racial discrimination. - -+ stakeholder -> (n)someone who has invested money into something, or who has some important connection with it, and therefore is affected by its success or failure - - - Citizens should be stakeholders in the society they live in. - -+ boycott -> (v)to refuse to buy something, use something, or take part in something as a way of protesting - - - We boycott all products tested on animals. - -+ alleviate -> (n)to make something less painful or difficult to deal with - - - measures to alleviate poverty - - a new medicine to alleviate the symptoms of flu - -+ kitten -> (n)a yound cat - -+ madonna -> (n)Mary, the mother of Jesus, in the Christian religion - - - a picture of the Modonna - -+ newsstand -> (n)a place on a street where newspapers and magazines are sold - - - Eight new national newspapers have appeared on the newsstands since 1981. - -+ procreate -> (n)to produce children or bady animals, reproduce - - - Most yound women feel a biological need to procreate. - - Early marriage and procreation are no longer discouraged there. - -+ glamorous -> (adj)attractive, exciting, and related to wealth and success - - - She led an exciting and glamorous life. - - the most glamorous neighbourhood in the city - -+ retrospect -> (n)thinking back to a time in the past, especially with the advantage of knowing more now than you did then - - - In retrospect, I wonder if we should have done more. - -+ soothe -> (v)to make someone feel calmer and less anxious, upset or angry
-> (v)to make a pain become less severe, or slowly disappear
-> (adj)soothing - - - Lucy soothed the baby by rocking it in her arms. - - She made a cup of tea to soothe her nerves. - - Massage can gently soothe away your aches and pains. - -## 18th, October - -+ econometrics -> (n)the application of mathematical and statistical techniques to economic problems and theories - -+ mischievous -> (adj)someone who is mischievous likes to have fun, especially by playing tricks on people or doing things to annoy or embarrass them. - - - Their sons are noisy and mischievous. - - Gabby looked at him with a mischievous grin. - - There was a mischievous gleam in her eyes. - -+ allege -> (v)to say that something is true or that someone has done something wrong, although it has not been proved. - - - It was alleged that the policeman had accepted bribes. - - The prosecution alleged that the man had been responsible for an act of terrorism. - - The water is alleged to be polluted with mercury. - -+ prosecution -> (n)when a charge is made against someone for a crime, or when someone is judged for a crime in a court of law
-> (n)the lawyers who try to prove in a court of law that someone is guilty of a crime
-> (n)when you do something that is your job - - - a criminal prosecution - - The evidence is not sufficient to bring a prosecution against him. - - Walters could face prosecution for his role in the robbery. - - the chief witness for the prosecution - - the prosecution of her duties - -+ pin down -> (v)if you try to pin something down, you try to discover exactly what, where, or when it is. - - - It has taken until now to pin down it exact location. - - The fire department is trying to pin down the cause of Wednesday's fire. - -+ duly -> (adv)in the proper or expected way
-> (adv)at the proper time or as expected - - - Here are your travel documents, all duly signed. - - The Queen duly appeared on the balcony to wave to the crowds. - -+ shop floor -> (n)the area in a factory where the ordinary workers do their work
-> (n)the ordinary workers in a factory, not the managers - - - The chairwoman started her working life on the shop floor. - - negotiations between the shop floor and management - -+ be subject to -> to have or experience a particular thing, especially something unpleasant
-> only able to happen if something else happens - - - Cars are subject to high domestic tax. - - In recent years, she has been subject to attacks of depression. - - We plan to go on Wednesday, subject to your approval. - - Moving all the books should not take long, subject to there being enough helpers. - -## 19th, October - -+ inexorable -> (adj)an inexorable process cannot be stopped, unstoppable - - - the inexorable decline of Britain's manufacturing industry - - the seemingly inexorable rise in crime - -+ save -> (adv)except - - - She answered all the questions save one. - - Little is known about his early life, save that he had a brother. - -+ headlong -> (adj, adv)with great speed or without thinking
-> (adj)moving forward with your head first - - - In the headlong rush to buy houses, many people got into debt. - - a headlong plunge into the lake - -+ prose -> (n)language in its usual form, as opposed to poetry - - - I've always preferred reading prose to poetry. - -+ stylist -> (n)a person whose job is to arrage, shape, or design something, such as a person's hair
-> (n)a writer who has a good writing style and chooses the words they use very carefully - - - She gave up a career as a fashion stylist to rear two daughters. - - She's no stylist, but she writes very exciting stories. - -+ scale back -> (v)to reduce something in size, amount, or production - - - Sid will have to scale back his plans. - - Some firms may scale back their expansion plans in the region as a result of the downturn. - -## 20th, October - -+ conditioning -> (n)the process by which people or animals are trained to behave in a particular way when particular things happen. - - - Social conditioning makes crying more difficult for men. - - Conditioning starts as soon as boys are given guns to play with and girls are given dolls. - -+ wistful -> (adj)thinking sadly about something you would like to have but cannot have, especially something that you used to have in the past - - - a wistful smile - - "That's the house where I was born," she said wistfully. - - A child stood looking with wistful eyes at the toys in the shop window. - -+ inconclusive -> (adj)not giving or having a result or decision - - - The medical tests were inconclusive, and will need to be repeated. - - The evidence is inconclusive, and no arrest is warranted. - -+ spontaneous -> (adj)something that is spontaneous has not been planned or organized, but happens by itself, or because you suddenly feel you want to do it. - - - The crowd gave a spontaneous cheer. - - My spontaneous reaction was to run away. - -+ better still -> used to say that a particular choice would be more satisfactory - - - Why don't you give her a call or, better still, go and see her? - -+ herd -> (n)a group of animals of one kind that live and feed together
-> (n)people generally, especially when thought of as being easily influenced by others - - - a herd of cattle - - herds of elephants - - You have to be an individual; it's no use running with the herd. - -+ hawk -> (n)a large bird that hunts and eats small birds and animals.
-> (v)to try to sell goods, usually by going from place to place and trying to persuade people to buy them. - - - On every street corner there were traders hawking their wares. - -+ deride -> (v)to make remarks or jokes that show you think someone or something is silly or useless, mock - - - You shouldn't deride their efforts. - - The party was derided as totally lacking in ideas. - -+ preoccupation -> (n)a state when someone thinks or worries about something a lot, with the result that they do not pay attention to other things.
-> (n)something that you give all your attention to - - - the current preoccupation with sex and scandal - - The management's preoccupation with costs and profits resulted in a drop in quality and customer service. - - Their main preoccupation was how to feed their families. - -+ superstitious -> (adj)based on or believing in superstitions(belief based on old ideas about luck and magic rather than science and reason) - - - the old superstition that walking under a ladder is unlucky - - A wave of superstitious fear spread among the townpeople. - - Jean was extremely superstitious and believed the colour green brought bad luck. - -+ sermon -> (n)a talk given as part of a Christian church service, usually on a religious or moral subject. - - - Today's sermon was on the importance of compassion. - - deliver a sermon - -## 27th, October - -+ independently of -> - - - The person appointed here shall act independently of the Board. - -+ rare bird -> (n)an unusual person - - - He's that rare bird, a bartender who doesn't drink alcohol. - -+ bartender -> (n)someone who makes and serves drinks in a bar - -+ sb/sth in question -> the person or thing that is being discussed - - - I stayed at home on the night in question. - -+ disproportionate -> (adj)too large or small in comparison to something else, or not deserving its importance or influence - - - There are disproportionate number of girls in the class. - - The country's great influence in the world is disproportionate to its relatively small size. - -+ put sth down to sth -> (v)to think that a problem or situation is caused by a particular thing - - - I put the children's bad behaviour down to the fact that they were tired. - - She did seem rather unhappy, but I just put it down to the pressure she was under at work. - -+ affirm -> (v)to state something as true
-> (v)to publicly show your support for an opinion or idea - - - She affirmed her intention to apply for the post. - - The government has affirmed its commitment to equal rights. - -## 28th, October - -+ susceptible -> (adj)easily influenced or harmed by something
-> (adj)used to describe someone who is easily emotionally influenced - - - These plants are particularly susceptible to frost. - - She isn't very susceptible to flattery. - - They persuade susceptible teenagers to part with their money. - -+ psychiatrist -> (n)a doctor who treats people suffering from mental illness. - -+ affiliation -> (n)a connection with a political party or religion, or with a larger organization. - - - Their lack of affiliation to any particular bank allows them to give objective financial advice. - - The kidnappers had no affiliation with any military group. - -+ substantial -> (adj)large in size, value or importance - - - The findings show a substantial difference between the opinions of men and women. - - She inherited a substantial fortune from her grandmother. - -+ bipedal -> (adj)walking on two legs, or relating to this movement - - - He was facinated by the gaint, bipedal ape. - - Some dinosaurs were capable of bipedal walking. - -+ bedrock -> (n)the solid rock in the ground which supports all the soil above it.
-> (n)the bedrock of something is the principles, ideas, or facts on which it is based. - - - Mutual trust is the bedrock of a relationship. - - We believe in religious freedom as a bedrock principle of our democracy. - -+ railroad -> (v)to force something to happen or force someone to do something, especially quickly or unfairly. - - - We were railroaded into signing the agreement. - - He more or less railroaded the rest of the Europe into recognising the new "independent" states. - -+ substantiate -> (v)to show something to be true, or to support a claim with facts. - - - We have evidence to substantiate the allegations against him. - - Reports that children had been hurt have not been substantiated. - -+ chauvinistic -> (adj)believing or showing an unreasonable belief that your own country or race is the best or most important.
-> (adj)believing that or behaving as if women are naturally less important, intelligent, or able than men. - - - It is a deeply chauvinistic community where the few women who have jobs are ridiculed. - -+ prod -> (v)to push something or someone with your finger or with a pointed object.
-> (v)to encourage someone to take action, especially when they are being slow or unwilling. - - - I prodded her in the back to get her attention. - - She prodded the cake with her fork to see if it was cooked. - - He gets things done, but only after I've prodded him into doing them. - -## 4th, November - -+ plea -> (n)an urgent and emotional request
-> (n)the answer that a person gives in court when they have been accused of committing a crime. - - - They made a desperate plea for help. - - The president has made an emotional plea for the killing to stop. - - The prisoner entered a plea of not guilty. - -+ sovereign -> (n)a king or queen
-> (adj)having the highest power or being completely independent. - - - Sovereign power is said to lie with the people in some countries, and with a ruler in others. - - We must respect the rights of sovereign states to conduct their own affairs. - -+ viable -> (adj)able to work as intended or able to succeed. - - - In order to make the company viable, it will unfortunately be necessary to reduce staffing levels. - - She's going to have to cut costs to maintain the viability of her business. - -+ regime -> (n)a particular government or a system or method of government.
-> (n)a particular way of operating or organising a business, etc. - - - the collapse of the Fascist regime at the end of the war - - The old, corrupt, totalitarian regime was overthrown. - - The regime in this office is hard work and more hard work. - -+ tribute -> (n)something that you say, or give that shows your respect and admiration for someone, especially on a formal occasion. - - - Tributes have been pouring in from all over the world for the famous actor who died yesterday. - -+ totalitarian -> (adj)of or being a political system in which those in power have complete control and do not allow people freedom to oppose them. - - - a totalitarian regime - -+ egalitarian -> (adj)believing that all people are equally important and should have the same rights and opportunities in life. - - - I still believe in the notion of an egalitarian society - -+ temper -> (v)to make something less strong, extreme, etc. - - - My enthusiasm for the venture was tempered by my knowledge of the hard work that would be involved. - - I learned to temper my criticism. - -## 8th, November - -+ quirk -> (n)an unusual habit or part of someone's personality, or something that is strange and unexpected - - - You have to get used to other people's quirks and foibles. - - By some strange quirk, we ended up on the same train. - -+ foible -> (n)a small fault or foolish habit - - - We all have our little foibles. - -+ astrological -> (adj)involving or relating to astrology(the belief that the positions of the sun, moon, planets, and stars affect people's lives and character) - - - He likes to talk about his astrological sign, which is Leo. - -+ confer -> (v)to exchange ideas on a particular subject, ofter in order to reach a decision on what action to take.
-> (v)to give an official title, honour or advantage to someone - - - I need some time to confer with my lawyer. - - An honorary doctorate was conferred on him by Columbia University. - - Certain astrological signs confer superior soccer skills. - -+ doctorate -> (n)the highest degree from a university - - - She has a doctorate in physics from Yale. - -+ stamina -> (n)the physical or mental strength to do something that might be difficult and will take a long time. - - - The triathlon is a great test of stamina. - - You have to have a lot of stamina to be top-class dancer. - -+ mania -> (n)a very strong interest in something that fills a person's mind or uses up all their time. - - - The article describes the religious mania that is sweeping the US. - - Van Gogh suffered from acute persecution mania. - -+ persecution -> (n)unfair or cruel treatment over a long period of time because of race, religion or political beliefs. - - - refugees escaping from political persecution - - the persecution of minorities - -+ swamp -> (n)an area of very wet, soft land
-> (v)to cover a place or thing with a large amount of water
-> (v)if something swamps a person, system or place, more of it arrives than can be easily dealt with. - - - an alligator-infested swamp - - High tides have swamped the coast. - - The boat was swamped by an enormous wave. - - Foreign cars have swamped the UK market. - - I'm swamped with work at the moment. - - Don't let feelings of depression swamp you. - -+ entail -> (v)to make something necessary, or to involve something. - - - Such a large investment inevitably entails some risk. - - Repairing the roof will entail spending a lot of money. - -+ hysteria -> (n)extreme fear, excitement, anger etc. that cannot be controlled - - - One woman, close to hysteria, grabbed my arm. - -+ overlook -> (v)to provide a view of, especially from above
-> (v)to fail to notice or consider something or someone - - - Our hotel room overlooked the harbour. - - I think there is one key fact that you have overlooked. - -+ nurture -> (v)to feed or care for a child, or to help someone or something develop by encouraging that person or thing.
-> (n)the way in which children are treated as they are growing, especially as compared with the characteristics they are born with. - - - Which do you believe has the strongest influence on how children develop -- nature or nurture. - - As a record company director, his job is to nurture yound talent. - - a carefully nurtured garden - -+ take to -> (v)to start doing something often - - - She was so depressed that she took to drink. - - He's taken to staying out very late. - -+ vaunt -> (v)to speak proudly about or praise something, especially in a way that is more than acceptable or reasonable. - - - The local people like to vaunt the glories of their faded past. - - His much vaunted new plan has serious weaknesses. - -## 11th, November - -+ shelter -> (n)(a building designed to give) protection from bad weather, danger or attack - - - They opened a shelter to provide temporary housing for the city's homeless. - - a shelter for homeless women - -+ comprehensive -> (adj)complete and including everything that is necessary - - - He has writen a fully comprehensive guide to Rome. - - We offer you a comprehensive training in all aspects of the business. - -+ coordination -> (n)the act of making all the people involved in a plan or activity work together in an organized way. - - - the lack of coordination between the civilian and military authorities - -+ package deal -> (n)a set of arrangements that must be accepted together and not separately.
-> (n)the offer of a low price if several things are bought together. - - - The package deal under discussion will require every country to be flexible on its immigration laws. - - Many small hotels offer accommodation and breakfast as a package deal. - -## 18, November - -+ justify -> (v)to show that something is reasonable, right or true. - - - Her fears of a low voter turnout were justified when fewer than half of the people voted. - - She said that there was no justification for such a rude behaviour. - -+ turnout -> (n)the number of people who are present at an event, especially the number who go to vote at an election. - - - Good weather on polling day should ensure a good turnout. - -+ dim -> (adj)not very clever - - - He's a nice guy, but a little dim. - -+ corporate -> (adj)relating to a business, especially a large business.
-> Corporate America: used to refer to large companies that have a lot of influence in the economy of the US. - - - Analysts are expecting share prices to fall in the first quarter of next year because of poor corporate earnings. - -+ put sb on notice -> (v)to warn someone offically that something is going to happen, or could possibly happen. - - - The facility was put on notice that its funding was in danger if improvements were not carried out. - -+ compliance -> (n)the act of obeying an order, rule or request. - - - It is the job of the inspectors to enforce compliance with the regulations. - - We've been working hard to remain in compliance with all air pollution control laws. - -+ feeble -> (adj)weak and without energy, strength or power. - - - He was a feeble, helpless old man. - - Opposition to the plan was rather feeble. - - a feeble joke/excuse - -+ pink slip -> (n)a document given to an employee to tell them that they do not have a job any longer. - - - Eastern Airlines shut down operations at midnight, and 18,000 employees were given their pink slips. - -+ outlive -> (v)to live or exist longer than someone or something. - - - He outlived all of his brothers. - -+ deductible -> (adj)a deductible expense is a cost that you can subtract from the earnings on which you have to pay income tax.
-> (n)a part of the cost of an accident, injury etc that you agree to pay yourself when you buy insurance. - - - Expenses like office phone bills are tax-deductible. - - Customers can lower insurance premiums by taking higher deductibles. - -+ odds -> (n)the probability that a particular thing will or will not happen. - - - If you drive a car all your life, the odds are that you'll have an accident at some point. - -+ attendant -> (adj)coming with a stated thing or resulting from it. - - - debt and its attendant problems - - There are too many risks attendant on such a large investment of money. - -+ eightfold -> (adj, adv)eight times as big or as much - - - an eightfold increase - - Drug-related crimes jumped eightfold. - -+ elude -> (v)if something that you want eludes you, you do not succeed in achieving it.
-> (v)if a piece of information eludes you, you cannot remember it.
-> (v)if you elude someone or something, you avoid them or escape from them. - - - Sleep eluded her. - - They had minor breakthroughs but real success eluded them. - - I know who you mean but her name eludes me. - - He eluded the police for 13 years. - -+ encompass -> (v)to include different types of things. - - - The festival is to encompass everything from music, theatre and ballet to literature, cinema and the visual arts. - -+ chronological -> (adj)following the order in which a series of events happened - - - Give me the dates in chronological order. - - I have arranged these stories in chronological order. - - The exhibition is organized chronologically. - -+ toil -> (v)to work hard
-> (n)hard work, especially work that makes you feel physically tired. - - - People who toiled in dim factories were too exhausted to enjoy their family life. - - Well, after a day's toil in the office I like to relax a little. - -## 24th, November - -+ deteriorate -> (v)to become worse - - - She was taken to hospital last week when her condition suddenly deteriorated. - - There are fears that the situation might deteriorate into full-scale war. - - concern about the rapid deterioration in relations between the two countries - -+ divisive -> (adj)tending to cause disagreements that seperate people into opposing groups. - - - The Vietnam War was an extremely divisive issue in the US. - -+ seethe -> (v)to feel very angry but to be unable or unwilling to express it clearly. - - - The rest of the class positively seethed with indignation when Julia won the award. - - She took it calmly at first but under the surface she was seething. - -+ uniformity -> (n)the quality or fact of being the same, or of not changing or being different in any way. - - - We do not seek to impose uniformity on every single decision that is made. - - He argues that we need statewide uniformity. - -+ deference -> (n)respect or politeness. - - - He treats her with such a deference. - - She covered her head in deference to Muslim custom. - -+ in a row -> (adv)one after another without a break - - - She's been voted Best Actress three years in a row. - -+ worthy -> (adj)deserving respect, admiration or support. - - - Every year she makes a large donation to a worthy cause. - -+ sandal -> (n)a light shoe, especially worn in warm weather, consisting of a bottom part held onto the foot by straps. - - - a pair of open-toed sandals - -+ fishery -> (n)an area of water where fish are caught so that they can be sold. - - - an offshore fishery - -+ daffodil -> (n)a yellow, bell-shaped flower with a long stem that is commonly seen in the spring. - -+ massacre -> (n)an act of killing a lot of people
-> (n)a bad defeat, especially in a sport.
-> (v) - - - He ordered the massacre of 2,000 women and children. - - The manager resigned after the team's 7-2 massacre in the final. - - Hundreds of cilivians were massacred in the raid. - - English was massacred 5-0 by France in the semi-final. - -+ anchor -> (n)a person who reports the news and manages reports by others on a television or radio program. - - - The mayor grants frequent interviews to local news anchors. - -+ phony -> (adj)represented as real but actually false; intended to deceice.
-> (n)a person who falsely pretends to be something. - - - They were accused of submitting phony claims to insurers, including Medicare. - - I thins he's a phony. - -+ arthritis -> (n)a serious condition in which a person's joints become painful, swollen and stiff. - - - In later life She was crippled with arthritis. - -+ auditor -> (n)someone whose job is to carry out an official examination of the accounts of a business and to produce a report. - - - The committee will appoint an independent auditor to examine the annual accounts. - -+ compulsive -> (adj)doing something wrong or harmful a lot and unable to stop doing it.(not compulsory)
-> (adj)If a film, play, sports event, book, etc. is compulsive, it is so interesting or exciting that you do not want to stop watching or reading it. - - - a compulsive liar/thief/eater - - He was a compulsive gambler and often heavily in debt. - - compulsive behaviour - - Her new series is compulsive viewing. - -+ suit -> (n)a problem taken to a law court by ordinary person or an organization rather than the police in order to obtain a legal decision, lawsuit - - - Two of the directors filed a suit against their former employer. - - They brought a lawsuit against the company - -+ sue -> (v)to take legal action against a person or organization, especially by making a legal claim for money because of some harm that they have caused you. - - - Mr. Warren sued for libel over the remarks. - - The company could be sued for damages. - -+ patronize -> (v)to speak or behave towards someone in a way that seems friendly, but that shows that they think they are superior to you in some way.
-> (v)to be a regular customer of a shop or restaurant, etc. - - - Don't you patronize me. - - The restaurant was patronized by many artists and writers during the 1920s. - - We'd like to thank all of our customers for their patronage in the past. - -+ windfall -> (n)an amount of money that you win or receive from someome unexpectedly. - - - the man who received a $250,000 windfall after a banking error. - -+ wager -> (v)to risk money by guessing the result of something.
-> (n)an amount of money that you risk in the hope of winning more. - - - She put a cash wager of $25 on the race. - - I wager you $5 that they'll get there first. - - I'd wager that she's interested in you. - -+ pepper -> (v)to direct something suddenly and repeated at someone, as if attacking the person. - - - He was wounded in both legs and severely peppered with sharpnel. - - The mayor was peppered with questions from reporters about the municipal corruption scandal. - -+ shrapnel -> (n)some pieces of metal that fly through the air when a bomb or simialr weapon explodes and are intended to injure people. - - - Twelve people were hit by shrapnel in the attack. - -+ municipal -> (adj)of or belonging to a town or city - - - They are responsible for municipal leisure facilities. - - next month's municipal elections - -+ libel -> (n)a piece of writing that contains bad and false things about a person.
-> (v)to write and publish something that contains bad and false things about a person. - - - She threatened to sue the magazines for libel. - - She claims the newspaper libelled her in editorials and news articles. - - With a few exceptions, no writer consciously sits down with the aim of libelling anyone. - -+ pathological -> (adj)(of a person)unable to control part of their behaviour; unreasonable - - - a pathological liar - - He experiences chronic, almost pathological jealousy. - -## 30th, December - -+ spam -> (n)unwanted email, ususually advertisements
-> (v)to send someone advertisements by email that they do not want. - - - Some Internet service providers block spam to subscribers. - - He spammed the message to 30,000 addresses in a week. - -+ tumor -> (n)a mass of cell in the body that grow faster than usual and can cause illness. - - - a malignant/benign tumor - -+ malignant -> (adj)a malignant disease or growth is cancer or is related to cancer, and is likely to be harmful.
-> (adj)having a strong wish to do harm - - - The process by which malignant cancer cells multiply isn't fully understood. - - He developed a malignant hatred for the land of his birth. - -+ benign -> (adj)pleasant or kind; not harmful or severe
-> (adj)a benign growth is not cancer and is not likely to be harmful - - - a benign tumor - - They are normally a more benign audience. - - I just smiled benignly and stood back. - -+ inventory -> (n)a detailed list of all the things in a place.
-> (n)the amount of goods a store or business has for sale at a particular time, or their value. - - - About half of the shop's inventory was damaged in the tornado. - - Before starting, he made an inventory of everything that was to stay. - -+ tornado -> (n)a strong, dangerous wind that forms itself into an upside-down spinning cone and is able to destroy buildings as it moves across the ground. - -## 31st, December - -+ delegate -> (n)a person chosen or elected by a group to speak, vote, etc. for them, especially at a meeting.
-> (v)to give a particular job, duty, right, etc. to someone else so that they do it for you.
-> (v)to choose or elect someone to speak, vote, etc. for a group, especially at a meeting. - - - Each union elects several delegates to the annual conference. - - He talks of travelling less, and delegating more authority to his deputies. - - A group of teachers were delegated to represent their colleagues at the union conference. - -## 1st, January - -+ sloppy -> (adj)very wet or liquid, often in a way that is unpleasant
-> (adj)not taking care or making an effort - - - She covered his face with sloppy kisses. - - Spelling mistakes always look sloppy in formal letters. - - He has little patience for sloppy work from colleagues. - -+ substitute -> (v)to use something or someone instead of another thing or person
-> (v)substitute for sth: to perform the same job as another thing or to take its place.
-> (n)a thing or person that is used instead of another thing or person.
-> (n)in sports, a player who is used for part of a game instead of another player. - - - You can substitute oil for butter in the recipe. - - I use that recipe but substitute wheat-free flour for regular flour. - - Of course, no book or course of study can substitute for experience. - - Gas-fired power station will substitute for less efficient coal-fired equipment. - - Vitamins should not be used as a substitute for a healthy diet. - - The manager brought on another substitute in the final minutes of the game. - -+ sophomore -> (n)a student studying in the second year of a course at a US college or high school. - -+ frontage -> (n)the front part of a building that faces a road or river, or land near a load or river. - - - These apartments all have a delightful dockside frontage. - - The restaurant has a river frontage. - -## 7th, January - -+ proprietary -> (adj)relating to owning something, or relating to or like an owner.
-> (adj)Propritary goods are made and sent out by a particular company whose name is on the product. - - - I just assumed he owned the place - he had a proprietary air about him. - - some proprietary brands of dog food - - This is proprietary software, and you have no right to copy it without a license from the owner. - -+ recap -> (v)to repeat the main points of something that has been discussed earlier at the end of a meeting or talk.
-> (n)also a noun - - - Let me just recap very briefly what I said earlier. - - To recap, our main aim is to increase sales by 15 percenet this year. - - Here's a recap of the week's most hotly debated new items. - -+ defer -> (v)to delay something until a later time.
-> (v)defer to: to allow someone or something to make decisions for you or tell you what to do because of your respect for them or because of their higher rank, authority, knowledge, etc. - - - My bank has agreed to defer the payments of my loans. - - Can we defer making a decision until next week. - - I have to defer to my boss on important decisions. - - I defer to your judgement. - -+ outlet -> (n)a shop that is one of many owned by a particular company and that sells the goods that the company has produced. - - - the largest retail outlet in the city - - At the factory outlet you'll find discounted items at up to 75% off regular prices. - -+ intercept -> (v)to stop and catch something or someone before before that thing or person is able to reach a particular place.
(n)the point at which a line intercepts a coordinate axis. - - - Law enforcement agents intercepted a shipment of drugs from Central America. - - The ball was intercepted by Grady. - -+ franchise -> (n)a right to sell a company's products in a particular area using the company's name. - - - Each store is owned by an individual who pays a fee for franchise. - - He lost the franchise after failing to meet the specified standards. - -## 19th, January - -+ wade -> (v)to walk through water or other liquid with some effort, because it is deep enough to come quite high up your legs, or thick. - - - The river was full but we managed to wade across.- - - They had to wade through a swamp. - -+ proposition -> (n)an offer or suggestion, usually in business.
-> (n)an idea or opinion
-> (n)a statement or problem that must be solved or proved to be true or not true.
-> (n)a question or statement about an issue of public policy that appears on a voting paper so that people can vote for it or against it. - - - He wrote to me last week regarding a business proposition that he thought might interest me. - - They were debating the proposition that "All people are created equal". - - Under Proposition 71, the state will commit $3 billion to studying stem cells over the next 10 years. - -+ moisture -> (n)a liquid such as water in the form of very small drops, either in the air, in a substance, or on a surface. - - - These plants need a rich soil that remains moisture. - - It was a clear day with little moisture in the air. - - Plants absorb carbon dioxide from the air and moisture from the soil. - -## 21st, February - -+ ostensible -> (adj)appearing or claiming to be one thing when it is really something else. - - - Their ostensible goal was to clean up government corruption, but their real aim was to unseat the government. - -+ supersede -> (v)to relace something, especially something older or more old-fashioned. - - - Most of the old roads have been superseded by the great interstate highways. - - Hand tools are relics of the past that have now been superseded by the machine. - -+ disentangle -> (v)to separate things that have become joined or confused. - - - It's difficult to disentangle hard fact from myth, or truth from lies. - - I tried to disentangle the wires under my desk. - -## 23rd, February - -+ commensurate -> (adj)in a correct and suitable amount compared to something else. - - - a salary that is commensurate with skills and experience - - The agency's workload has increased without any commensurate increase in staff. - -## 29th, February - -+ efficacy -> (n)the ability, especially of a medicine or a method of achieving something, to produce the intended result, effectiveness. - - - They recently ran a series of tests to meausre the efficacy of the drug. - -+ cosmetically -+ rationale -+ ameliorate -+ level off -+ delve -+ coronary diff --git a/thu_os/belady.md b/操作系统/belady.md similarity index 100% rename from thu_os/belady.md rename to 操作系统/belady.md diff --git a/thu_os/chp1.md b/操作系统/chp1.md similarity index 100% rename from thu_os/chp1.md rename to 操作系统/chp1.md diff --git a/thu_os/chp11.md b/操作系统/chp11.md similarity index 100% rename from thu_os/chp11.md rename to 操作系统/chp11.md diff --git a/thu_os/chp15.md b/操作系统/chp15.md similarity index 100% rename from thu_os/chp15.md rename to 操作系统/chp15.md diff --git a/thu_os/chp17.md b/操作系统/chp17.md similarity index 100% rename from thu_os/chp17.md rename to 操作系统/chp17.md diff --git a/thu_os/chp18.md b/操作系统/chp18.md similarity index 100% rename from thu_os/chp18.md rename to 操作系统/chp18.md diff --git a/thu_os/chp20.md b/操作系统/chp20.md similarity index 100% rename from thu_os/chp20.md rename to 操作系统/chp20.md diff --git a/thu_os/chp5&6.md b/操作系统/chp5&6.md similarity index 100% rename from thu_os/chp5&6.md rename to 操作系统/chp5&6.md diff --git a/thu_os/chp8.md b/操作系统/chp8.md similarity index 100% rename from thu_os/chp8.md rename to 操作系统/chp8.md diff --git a/thu_os/chp9.md b/操作系统/chp9.md similarity index 100% rename from thu_os/chp9.md rename to 操作系统/chp9.md diff --git a/thu_os/eisenberg.md b/操作系统/eisenberg.md similarity index 100% rename from thu_os/eisenberg.md rename to 操作系统/eisenberg.md diff --git a/thu_os/images/FCFS.png b/操作系统/images/FCFS.png similarity index 100% rename from thu_os/images/FCFS.png rename to 操作系统/images/FCFS.png diff --git a/thu_os/images/FSS.png b/操作系统/images/FSS.png similarity index 100% rename from thu_os/images/FSS.png rename to 操作系统/images/FSS.png diff --git a/thu_os/images/MLFQ.png b/操作系统/images/MLFQ.png similarity index 100% rename from thu_os/images/MLFQ.png rename to 操作系统/images/MLFQ.png diff --git a/thu_os/images/PFF.png b/操作系统/images/PFF.png similarity index 100% rename from thu_os/images/PFF.png rename to 操作系统/images/PFF.png diff --git a/thu_os/images/RR_waiting_time.png b/操作系统/images/RR_waiting_time.png similarity index 100% rename from thu_os/images/RR_waiting_time.png rename to 操作系统/images/RR_waiting_time.png diff --git a/thu_os/images/SPN.png b/操作系统/images/SPN.png similarity index 100% rename from thu_os/images/SPN.png rename to 操作系统/images/SPN.png diff --git a/thu_os/images/alloc_pid.png b/操作系统/images/alloc_pid.png similarity index 100% rename from thu_os/images/alloc_pid.png rename to 操作系统/images/alloc_pid.png diff --git a/thu_os/images/clock.png b/操作系统/images/clock.png similarity index 100% rename from thu_os/images/clock.png rename to 操作系统/images/clock.png diff --git a/thu_os/images/cpu_use.png b/操作系统/images/cpu_use.png similarity index 100% rename from thu_os/images/cpu_use.png rename to 操作系统/images/cpu_use.png diff --git a/thu_os/images/deadlock_circular_wait.png b/操作系统/images/deadlock_circular_wait.png similarity index 100% rename from thu_os/images/deadlock_circular_wait.png rename to 操作系统/images/deadlock_circular_wait.png diff --git a/thu_os/images/estimate_time.png b/操作系统/images/estimate_time.png similarity index 100% rename from thu_os/images/estimate_time.png rename to 操作系统/images/estimate_time.png diff --git a/thu_os/images/exchange.png b/操作系统/images/exchange.png similarity index 100% rename from thu_os/images/exchange.png rename to 操作系统/images/exchange.png diff --git a/thu_os/images/get_pte.png b/操作系统/images/get_pte.png similarity index 100% rename from thu_os/images/get_pte.png rename to 操作系统/images/get_pte.png diff --git a/thu_os/images/hansen.png b/操作系统/images/hansen.png similarity index 100% rename from thu_os/images/hansen.png rename to 操作系统/images/hansen.png diff --git a/thu_os/images/hoare.png b/操作系统/images/hoare.png similarity index 100% rename from thu_os/images/hoare.png rename to 操作系统/images/hoare.png diff --git a/thu_os/images/improved_clock.png b/操作系统/images/improved_clock.png similarity index 100% rename from thu_os/images/improved_clock.png rename to 操作系统/images/improved_clock.png diff --git a/thu_os/images/kern_thread.png b/操作系统/images/kern_thread.png similarity index 100% rename from thu_os/images/kern_thread.png rename to 操作系统/images/kern_thread.png diff --git a/thu_os/images/lightweight_proc.png b/操作系统/images/lightweight_proc.png similarity index 100% rename from thu_os/images/lightweight_proc.png rename to 操作系统/images/lightweight_proc.png diff --git a/thu_os/images/load_control.png b/操作系统/images/load_control.png similarity index 100% rename from thu_os/images/load_control.png rename to 操作系统/images/load_control.png diff --git a/thu_os/images/monitor.png b/操作系统/images/monitor.png similarity index 100% rename from thu_os/images/monitor.png rename to 操作系统/images/monitor.png diff --git a/thu_os/images/mp3_version1.png b/操作系统/images/mp3_version1.png similarity index 100% rename from thu_os/images/mp3_version1.png rename to 操作系统/images/mp3_version1.png diff --git a/thu_os/images/mp3_version2.png b/操作系统/images/mp3_version2.png similarity index 100% rename from thu_os/images/mp3_version2.png rename to 操作系统/images/mp3_version2.png diff --git a/thu_os/images/nproc_mutex.png b/操作系统/images/nproc_mutex.png similarity index 100% rename from thu_os/images/nproc_mutex.png rename to 操作系统/images/nproc_mutex.png diff --git a/thu_os/images/organize_PCB.png b/操作系统/images/organize_PCB.png similarity index 100% rename from thu_os/images/organize_PCB.png rename to 操作系统/images/organize_PCB.png diff --git a/thu_os/images/overlay.png b/操作系统/images/overlay.png similarity index 100% rename from thu_os/images/overlay.png rename to 操作系统/images/overlay.png diff --git a/thu_os/images/page_fault.png b/操作系统/images/page_fault.png similarity index 100% rename from thu_os/images/page_fault.png rename to 操作系统/images/page_fault.png diff --git a/thu_os/images/page_fault_error_code.png b/操作系统/images/page_fault_error_code.png similarity index 100% rename from thu_os/images/page_fault_error_code.png rename to 操作系统/images/page_fault_error_code.png diff --git a/thu_os/images/page_remove_pte.png b/操作系统/images/page_remove_pte.png similarity index 100% rename from thu_os/images/page_remove_pte.png rename to 操作系统/images/page_remove_pte.png diff --git a/thu_os/images/proc_state.png b/操作系统/images/proc_state.png similarity index 100% rename from thu_os/images/proc_state.png rename to 操作系统/images/proc_state.png diff --git a/thu_os/images/process_running.png b/操作系统/images/process_running.png similarity index 100% rename from thu_os/images/process_running.png rename to 操作系统/images/process_running.png diff --git a/thu_os/images/process_switch.png b/操作系统/images/process_switch.png similarity index 100% rename from thu_os/images/process_switch.png rename to 操作系统/images/process_switch.png diff --git a/thu_os/images/status_queue.png b/操作系统/images/status_queue.png similarity index 100% rename from thu_os/images/status_queue.png rename to 操作系统/images/status_queue.png diff --git a/thu_os/images/suspend_proc.png b/操作系统/images/suspend_proc.png similarity index 100% rename from thu_os/images/suspend_proc.png rename to 操作系统/images/suspend_proc.png diff --git a/thu_os/images/swap_entry.png b/操作系统/images/swap_entry.png similarity index 100% rename from thu_os/images/swap_entry.png rename to 操作系统/images/swap_entry.png diff --git a/thu_os/images/synchroniaztion_methods.png b/操作系统/images/synchroniaztion_methods.png similarity index 100% rename from thu_os/images/synchroniaztion_methods.png rename to 操作系统/images/synchroniaztion_methods.png diff --git a/thu_os/images/user_thread.png b/操作系统/images/user_thread.png similarity index 100% rename from thu_os/images/user_thread.png rename to 操作系统/images/user_thread.png diff --git a/thu_os/images/virt_pte.png b/操作系统/images/virt_pte.png similarity index 100% rename from thu_os/images/virt_pte.png rename to 操作系统/images/virt_pte.png diff --git a/thu_os/images/vm2pm.png b/操作系统/images/vm2pm.png similarity index 100% rename from thu_os/images/vm2pm.png rename to 操作系统/images/vm2pm.png diff --git a/thu_os/images/working_set.png b/操作系统/images/working_set.png similarity index 100% rename from thu_os/images/working_set.png rename to 操作系统/images/working_set.png diff --git a/thu_os/lab1_report.md b/操作系统/lab1_report.md similarity index 100% rename from thu_os/lab1_report.md rename to 操作系统/lab1_report.md diff --git a/thu_os/lab2_report.md b/操作系统/lab2_report.md similarity index 100% rename from thu_os/lab2_report.md rename to 操作系统/lab2_report.md diff --git a/thu_os/lab3_report.md b/操作系统/lab3_report.md similarity index 100% rename from thu_os/lab3_report.md rename to 操作系统/lab3_report.md diff --git a/thu_os/lab4_report.md b/操作系统/lab4_report.md similarity index 100% rename from thu_os/lab4_report.md rename to 操作系统/lab4_report.md diff --git a/thu_os/lab5_report.md b/操作系统/lab5_report.md similarity index 100% rename from thu_os/lab5_report.md rename to 操作系统/lab5_report.md diff --git a/thu_os/lab6_report.md b/操作系统/lab6_report.md similarity index 100% rename from thu_os/lab6_report.md rename to 操作系统/lab6_report.md diff --git a/thu_os/lab7_report.md b/操作系统/lab7_report.md similarity index 100% rename from thu_os/lab7_report.md rename to 操作系统/lab7_report.md diff --git a/thu_os/lab8_report.md b/操作系统/lab8_report.md similarity index 100% rename from thu_os/lab8_report.md rename to 操作系统/lab8_report.md diff --git a/thu_os/philosopher.md b/操作系统/philosopher.md similarity index 100% rename from thu_os/philosopher.md rename to 操作系统/philosopher.md diff --git a/thu_dsa/chp1/chp1.md b/数据结构/chp1/README.md similarity index 100% rename from thu_dsa/chp1/chp1.md rename to 数据结构/chp1/README.md diff --git a/thu_dsa/chp1/exercises.md b/数据结构/chp1/exercises.md similarity index 100% rename from thu_dsa/chp1/exercises.md rename to 数据结构/chp1/exercises.md diff --git a/thu_dsa/chp1/fib/fib.cpp b/数据结构/chp1/fib/fib.cpp similarity index 100% rename from thu_dsa/chp1/fib/fib.cpp rename to 数据结构/chp1/fib/fib.cpp diff --git a/thu_dsa/chp1/lcs/Makefile b/数据结构/chp1/lcs/Makefile similarity index 100% rename from thu_dsa/chp1/lcs/Makefile rename to 数据结构/chp1/lcs/Makefile diff --git a/thu_dsa/chp1/lcs/lcs.cpp b/数据结构/chp1/lcs/lcs.cpp similarity index 100% rename from thu_dsa/chp1/lcs/lcs.cpp rename to 数据结构/chp1/lcs/lcs.cpp diff --git a/thu_dsa/chp1/lcs/lcs.h b/数据结构/chp1/lcs/lcs.h similarity index 100% rename from thu_dsa/chp1/lcs/lcs.h rename to 数据结构/chp1/lcs/lcs.h diff --git a/thu_dsa/chp1/lcs/test_lcs.cpp b/数据结构/chp1/lcs/test_lcs.cpp similarity index 100% rename from thu_dsa/chp1/lcs/test_lcs.cpp rename to 数据结构/chp1/lcs/test_lcs.cpp diff --git a/thu_dsa/chp1/master_theorem.md b/数据结构/chp1/master_theorem.md similarity index 100% rename from thu_dsa/chp1/master_theorem.md rename to 数据结构/chp1/master_theorem.md diff --git a/thu_dsa/chp1/max2/Makefile b/数据结构/chp1/max2/Makefile similarity index 100% rename from thu_dsa/chp1/max2/Makefile rename to 数据结构/chp1/max2/Makefile diff --git a/thu_dsa/chp1/max2/data.txt b/数据结构/chp1/max2/data.txt similarity index 100% rename from thu_dsa/chp1/max2/data.txt rename to 数据结构/chp1/max2/data.txt diff --git a/thu_dsa/chp1/max2/generateTestData.py b/数据结构/chp1/max2/generateTestData.py similarity index 100% rename from thu_dsa/chp1/max2/generateTestData.py rename to 数据结构/chp1/max2/generateTestData.py diff --git a/thu_dsa/chp1/max2/max2.cpp b/数据结构/chp1/max2/max2.cpp similarity index 100% rename from thu_dsa/chp1/max2/max2.cpp rename to 数据结构/chp1/max2/max2.cpp diff --git a/thu_dsa/chp1/max2/max2.h b/数据结构/chp1/max2/max2.h similarity index 100% rename from thu_dsa/chp1/max2/max2.h rename to 数据结构/chp1/max2/max2.h diff --git a/thu_dsa/chp1/max2/max2test.cpp b/数据结构/chp1/max2/max2test.cpp similarity index 100% rename from thu_dsa/chp1/max2/max2test.cpp rename to 数据结构/chp1/max2/max2test.cpp diff --git a/thu_dsa/chp10/CompleteBinaryHeap.h b/数据结构/chp10/CompleteBinaryHeap.h similarity index 100% rename from thu_dsa/chp10/CompleteBinaryHeap.h rename to 数据结构/chp10/CompleteBinaryHeap.h diff --git a/thu_dsa/chp10/LeftistHeap.h b/数据结构/chp10/LeftistHeap.h similarity index 100% rename from thu_dsa/chp10/LeftistHeap.h rename to 数据结构/chp10/LeftistHeap.h diff --git a/thu_dsa/chp10/PriorityQueue.h b/数据结构/chp10/PriorityQueue.h similarity index 100% rename from thu_dsa/chp10/PriorityQueue.h rename to 数据结构/chp10/PriorityQueue.h diff --git a/thu_dsa/chp10/heap.md b/数据结构/chp10/README.md similarity index 94% rename from thu_dsa/chp10/heap.md rename to 数据结构/chp10/README.md index 63fdf78..f905ac0 100644 --- a/thu_dsa/chp10/heap.md +++ b/数据结构/chp10/README.md @@ -1,9 +1,9 @@ -完全二叉堆知识总结 -================ +知识总结:完全二叉堆与左式堆 +========================= ## 知识脉络 -本章的主要内容是完全二叉堆和左式堆,它们都是用于[优先级搜索]这一特定的应用场景的,即每次只是找到其中的最值元素。完全二叉堆的关键在于`堆序性`和`结构性`——得益于`结构性`,完全二叉堆可以使用`向量`来存储,在相当地程度上简化了它的实现;左式堆主要用于`堆的合并`,该操作是通过递归地将堆A的右子堆与堆B合并,再将合并后的堆重新连接在堆A的右孩子上面来实现,其性能正比于两堆的最右侧通路长度之和。左式堆的关键在于`堆序性`和`左倾性`,即任意节点左孩子的`空节点路径长度`(npl, null path length)都不小于右孩子,基于此左式堆的最右侧路径即是全局最短路径,从而保证了`堆的合并`高效地进行。由于需要频繁地提取和接入堆的右子堆,左式堆显然不能基于`向量`实现,而是需要使用`二叉树`作为底层结构。 +完全二叉堆和左式堆都是用于`优先级搜索`这一特定的应用场景的,即每次只是找到其中的最值元素。完全二叉堆的关键在于`堆序性`和`结构性`——得益于`结构性`,完全二叉堆可以使用`向量`来存储,在相当地程度上简化了它的实现;左式堆主要用于`堆的合并`,该操作是通过递归地将堆A的右子堆与堆B合并,再将合并后的堆重新连接在堆A的右孩子上面来实现,其性能正比于两堆的最右侧通路长度之和。左式堆的关键在于`堆序性`和`左倾性`,即任意节点左孩子的`空节点路径长度`(npl, null path length)都不小于右孩子,基于此左式堆的最右侧路径即是全局最短路径,从而保证了`堆的合并`高效地进行。由于需要频繁地提取和接入堆的右子堆,左式堆显然不能基于`向量`实现,而是需要使用`二叉树`作为底层结构。 堆在前面讨论过的算法中有不少应用,比如第六章的`优先级搜索`(PFS, Priority First Search)中,每一步迭代都需要找到所有节点中优先级最高的节点,使用一个堆结构可以将该算法的时间复杂度提升至`O((n + e)logn)`,需要指出的是这里的堆结构还应该支持节点的优先级修改(提升)操作,这可以简单地通过`上滤`(percolate up)来实现。可以注意到,在稠密图的情况下,引入了堆结构以后该算法的性能反而下降了,为了解决这个问题,可以引入一个`多叉堆`(d-heap),其`上滤`和`下滤`操作的时间复杂度分别为`O(log_d^n)`以及`O(dlog_d^n)`,如果令`d = e/n + 2`,此时的整体时间性能为`O(ndlog_d^n + elog_d^n)`,则无论在稀疏图还是稠密图,都可以自适应得到最优的性能。 @@ -51,7 +51,7 @@ public: `结构性`不言而喻,完全二叉堆在结构上是一棵完全二叉树。如下图所示: -![cb_tree](cb_tree.png) +![cb_tree](images/cb_tree.png) 这意味着,可以简明地利用向量`Vector`来作为它的底层结构,因为此时父子结点之间的链接关系,可以直接通过它们的秩`Rank`来体现,而不需要像一般的树结构那样显式地给出链接。具体说来,假设根节点从`0`开始编号,则对于任意秩为`i`的节点,它的父节点的秩一定为`(i - 1) / 2`,它的左孩子的秩一定为`2i + 1`,右孩子的秩一定为`2i + 2`(如果左右孩子存在的话)。因此,可以给出下面的宏定义: @@ -89,7 +89,7 @@ V* CBHeap::getMax(){ 如果新插入的元素和它的父节点之间的确不满足`堆序性`,即新插入的元素大于它的父亲节点,为了恢复`堆序性`,只需要将这两个元素交换即可。这样一次操作之后,虽然原有的堆序性得到满足,但是新插入的元素又有可能与它的新的父亲节点之间不满足`堆序性`。为此,我们只需要不断地重复交换操作,直到这种冲突不再发生。插入的过程如下图所示: -![percolate_up](percolate_up.png) +![percolate_up](images/percolate_up.png) 可以看到,在这种插入策略下,新插入的节点是由下而上不断攀升的,因此这种插入又被称为`上滤插入`。为了实现`上滤插入`,可以首先将`上滤`操作进行抽象,形成一个内部函数,在`insert`中调用该函数即可。具体的代码如下: @@ -125,7 +125,7 @@ void CBHeap::insert(entry e){ 为了修复树根处的`堆序性`,可以将`last_elem`与它的两个孩子节点进行比较,并且将其中的最大者(堪为父者)作为新的树根,此时树根处的`堆序性`将得到修复。但是和插入节点的情形一样,此时`堆序性`冲突有可能传递到`last_elem`的新位置处,此时只需要简明地重复上面的操作,直到这种`堆序性`冲突不再发生。删除最大元素的过程如下图: -![percolate_down](percolate_down.png) +![percolate_down](images/percolate_down.png) 可以看到,为了调整堆的`堆序性`,被放置到树根的末元素是由上至下不断下降的,因此这种删除策略也被称为`下滤删除`。我们同样可以将下滤操作进行抽象,然后在`delMax`函数中调用下滤操作来实现`delMax`函数。具体的代码如下: diff --git a/thu_dsa/chp10/cb_tree.png b/数据结构/chp10/images/cb_tree.png similarity index 100% rename from thu_dsa/chp10/cb_tree.png rename to 数据结构/chp10/images/cb_tree.png diff --git a/thu_dsa/chp10/heap_merge.png b/数据结构/chp10/images/heap_merge.png similarity index 100% rename from thu_dsa/chp10/heap_merge.png rename to 数据结构/chp10/images/heap_merge.png diff --git a/thu_dsa/chp10/leftist_heap.png b/数据结构/chp10/images/leftist_heap.png similarity index 100% rename from thu_dsa/chp10/leftist_heap.png rename to 数据结构/chp10/images/leftist_heap.png diff --git a/thu_dsa/chp10/leftist_heap_structure.png b/数据结构/chp10/images/leftist_heap_structure.png similarity index 100% rename from thu_dsa/chp10/leftist_heap_structure.png rename to 数据结构/chp10/images/leftist_heap_structure.png diff --git a/thu_dsa/chp10/percolate_down.png b/数据结构/chp10/images/percolate_down.png similarity index 100% rename from thu_dsa/chp10/percolate_down.png rename to 数据结构/chp10/images/percolate_down.png diff --git a/thu_dsa/chp10/percolate_up.png b/数据结构/chp10/images/percolate_up.png similarity index 100% rename from thu_dsa/chp10/percolate_up.png rename to 数据结构/chp10/images/percolate_up.png diff --git a/thu_dsa/chp10/leftistHeap.md b/数据结构/chp10/leftistHeap.md similarity index 98% rename from thu_dsa/chp10/leftistHeap.md rename to 数据结构/chp10/leftistHeap.md index 7666afb..a7b04ea 100644 --- a/thu_dsa/chp10/leftistHeap.md +++ b/数据结构/chp10/leftistHeap.md @@ -5,7 +5,7 @@ 堆合并是指,对于任意两个堆,将其合并为一个更大的堆,如下图所示: -![heap_merge](heap_merge.png) +![heap_merge](images/heap_merge.png) 为了解决这个问题,我们可以首先进行一些初步的尝试。 @@ -59,7 +59,7 @@ npl(root) = npl(rc(root)) + 1 = npl(rc(rc(root))) + 2 = ... = right path length 因此,根节点的外部路径长度`npl`,就等于最右侧路径长度。左式堆的一个实例如下图所示: -![leftist_heap](leftist_heap.png) +![leftist_heap](images/leftist_heap.png) 而让我们再回顾一下`npl`的定义,即任一节点到外部节点路径的最短距离,因此根节点到外部节点的最短路径,就是左式堆的最右侧路径。设最右侧路径的长度为`d`,左式堆中一定包含了以`d`为高度的一棵满子树,因此左式堆的全部节点数量(包括外部节点)`n`一定满足 @@ -75,7 +75,7 @@ $$ 左式堆的该性质如下图所示: -![leftist_heap_structure](leftist_heap_structure.png) +![leftist_heap_structure](images/leftist_heap_structure.png) 在这样的条件下,我们上面给出的合并算法,其时间复杂度就只有`O(logm + logn)`了,这无疑是对性能的一次极大改进。但是需要注意的是,左式堆只是保证最右侧路径的长度最小,并非左子堆的规模和高度一定会大于右子堆。 diff --git a/thu_dsa/chp10/testHeap.cpp b/数据结构/chp10/testHeap.cpp similarity index 100% rename from thu_dsa/chp10/testHeap.cpp rename to 数据结构/chp10/testHeap.cpp diff --git a/thu_dsa/chp10/testLeftHeap.cpp b/数据结构/chp10/testLeftHeap.cpp similarity index 100% rename from thu_dsa/chp10/testLeftHeap.cpp rename to 数据结构/chp10/testLeftHeap.cpp diff --git a/thu_dsa/chp11/kmp.md b/数据结构/chp11/README.md similarity index 94% rename from thu_dsa/chp11/kmp.md rename to 数据结构/chp11/README.md index 7b9a969..c35d69c 100644 --- a/thu_dsa/chp11/kmp.md +++ b/数据结构/chp11/README.md @@ -1,9 +1,17 @@ -kmp conclusion -============== +知识总结:串匹配算法 +================== ## 知识脉络 -这里主要讨论串匹配的多个有效算法。各个算法的历史与逻辑在对应的`md`文件里面已经说得非常清楚了,这里主要针对考题来给出总结,即给定一个模式串`P`,要求写出它的`next`数组,改进的`next`数组,`bc`表,`ss`表和`gs`表。 +这里主要讨论串匹配的多个有效算法。各个算法的历史与逻辑在对应的 `md` 文件里面已经说得非常清楚了: + ++ [kmp 算法](kmp.md) ++ [bm-gs 算法](bm.md) ++ [karp-rabin 算法](karp_rabin.md) + +--- + +这里主要针对考题来给出总结,即给定一个模式串`P`,要求写出它的`next`数组,改进的`next`数组,`bc`表,`ss`表和`gs`表。 对于`next`数组,一种方案是把`makeNext`函数手动运行一遍,不过我觉得太慢了,不太喜欢。比较好的办法是,对于当前字符`P[j]`,`next[j]`即是它的前缀中最长的自匹配长度。不过需要注意的是,这里自匹配的前缀和后缀一定要是`P[0, j)`的真前缀和真后缀,否则`next[j] = j`也没有意义啊。此外还有一种验证的方法,即`next[j+1]`至多比`next[j]`大`1`。 @@ -23,17 +31,17 @@ kmp conclusion 为了查找模式串`P`在文本串`T`中出现的位置,最简单的思路就是对于每一个可能的对齐位置,依次去比对两个串中的字符是否相等,如果完全匹配,则返回匹配成功;否则就在下一个对其位置开始新的匹配。该策略执行的流程如下图所示: -![brute_force](brute_force.png) +![brute_force](images/brute_force.png) 容易看出,这种策略在最坏情况下,在每个对齐位置都需要进行`m`次比对,其中`m`为模式串的长度,设文本串的长度为`n`,则最坏情况下的时间复杂度为`O(mn)`。一种最坏情况的实例如下图所示: -![brute_force_worst](brute_force_worst.png) +![brute_force_worst](images/brute_force_worst.png) ## kmp 对上面的蛮力策略进行分析,可以发现其时间性能较差是因为在该策略中做了大量无意义的比对。比如上图中的这种情况,在每一个对齐位置都首先进行`m - 1`次成功的比对,其中每个字符都是`'0'`,然后失败于最后的一次比对。每次移动到新的对齐位置后,此前比对过的字符,将再次参与比对。如下图所示: -![brute_force_flaw](brute_force_flaw.png) +![brute_force_flaw](images/brute_force_flaw.png) 正是这些重复的比对,拉低了该策略的时间性能。因此,就应该从避免这些重复工作的角度,来对该算法进行改进。 @@ -115,7 +123,7 @@ int* makeNext(char* str){ 上面的`kmp`算法还存在一些缺陷,例如下面的这种情况: -![kmp_improvement](kmp_improvement.png) +![kmp_improvement](images/kmp_improvement.png) 可以注意到,在文本串的`1`这个位置,统共需要进行四次比对,然而后面的三次比对都是无意义的,因为模式串的四个字符都是`0`,而在第一次失败的比对中我们就可以知道,文本串的该字符并不是`0`,因此聪明的做法应该是直接跳过这三个不可能会成功的比对。 diff --git a/thu_dsa/chp11/bm.md b/数据结构/chp11/bm.md similarity index 96% rename from thu_dsa/chp11/bm.md rename to 数据结构/chp11/bm.md index 0c98084..dfacf99 100644 --- a/thu_dsa/chp11/bm.md +++ b/数据结构/chp11/bm.md @@ -9,7 +9,7 @@ 在一般的串匹配问题中,模式串的规模较小而文本串的规模往往极大,比如在一篇论文中查找某个英文单词的情形,在这种情况下,文本串中往往具有大量模式串中不会出现的字符。设想如果在任意一个对齐位置,模式串与文本串是从后往前比对,此时一旦在文本串中出现模式串中不存在的字符,就可以将整个模式串移出这个字符,从而跳过大量无意义的对齐位置。如下图所示: -![compare_backwards](compare_backwards.png) +![compare_backwards](images/compare_backwards.png) 如我们前面指出的那样,这种情况在头几次比对中出现的概率是极高的,因此相对于自左向右的比对,将比对的方向变成自右向左,的确可以极大地提高串匹配的效率。 @@ -17,7 +17,7 @@ 坏字符(bad character, bc)策略是一种非常简明的策略。它的基本思想是,在自后向前的某次匹配中,一旦在某个位置`k`比对失败,则将模式串整体右移,并且重新从右至左开始新一轮的比对,将比对失败的字符称为坏字符。而要使下一个对齐位置能够匹配,则至少要在坏字符处能够匹配才行,如下图所示: -![bad_character](bad_character.png) +![bad_character](images/bad_character.png) 因此,坏字符策略实现的关键,就在于找到模式串中,位于坏字符`Y`左侧,并且与字符`X`能够匹配的下一个字符。应该指出的是,这样的字符能够有多个,也就对应了多个移动距离,所有这些移动距离都是值得对齐的。因此,为了不错过其中的任意一个字符,应该取移动距离最小的那一个,使之与文本串中的`X`对齐。 @@ -27,11 +27,11 @@ 一是`bc[X]`的确存在,并且`bc[X] < j`,此时直接将`bc[X]`处的字符平移到与文本串中的`X`对齐,即可开始新一轮的比对,此时模式串移动的距离应该是`j - bc[X]`;另一种情况是`X`没有出现在模式串的字符集中,此时应该将模式串整体移动字符过`X`,即移动的距离的`j + 1`,为了与上面的情况统一,可以令`bc[*] = -1`,其中`*`表示所有没有出现在模式串中的字符,另一种理解方法是认为在模式串的左侧`P[-1]`存在一个通配符可以与`X`匹配。这两种情况如下图所示: -![bc_case12](bc_case12.png) +![bc_case12](images/bc_case12.png) 最后一种情况是`bc[X]`存在,但是`bc[X] > j`,即`P[bc[X]]`出现在`P[j]`的右侧,此时显然是不可以将`P[bc[X]]`与文本串中的`X`对齐的,因为此时将造成模式串的左移。实际上,这种对齐早在之前的比对中就已经被排除掉了。这种情况下,不妨简单地让模式串右移一个单位,然后从右至左开始新一轮的比对。该情况如下图所示: -![bc_case3](bc_case3.png) +![bc_case3](images/bc_case3.png) 这样,可以形成下面基于`bm-bc`策略的串匹配代码: @@ -72,11 +72,11 @@ void makeBC(char* const pattern, int* bc){ 使用`bc`策略时,最好可以达到`O(n/m)`的时间复杂度,对应了每次都在最右一个字符匹配失败,然后整体右移`m`个单位的情况,如下图所示: -![bc_bestcase](bc_bestcase.png) +![bc_bestcase](images/bc_bestcase.png) 这固然是一个非常好的结论,但是`bc`策略在最坏情况下却会达到`O(mn)`的时间复杂度,与蛮力策略相当,该情况如下图所示: -![bc_worstcase](bc_worstcase.png) +![bc_worstcase](images/bc_worstcase.png) 可以看到,在这种情况下,每次都需要进行`m - 1`次比对,才能在最左侧一次比对中失败,而该次失败只能让模式串右移一个单位。这种情况正与蛮力策略的最好情况相一致。一般地,单次匹配成功的概率越大,即字符集越小,就越接近于这种最坏的情况;单次匹配成功的概率越小,即字符集越大,就越接近于最好的情况。 @@ -88,11 +88,11 @@ void makeBC(char* const pattern, int* bc){ 设某次比对失败于模式串的位置`j`,因此`P[j+1, m)`与文本串中的对应字符依次相等。一般地,如果这`m - j -1`个字符在模式串中左侧的另一位置再次出现,则显然是一个值得的对齐位置,如下图所示: -![gs_case1](gs_case1.png) +![gs_case1](images/gs_case1.png) 但是如果这`m - j - 1`字符没有在模式串中重复出现,是否就不存在值得的对齐位置了呢?答案是否定的,因为此时的情形就类似`kmp`的情形了啊,一般地,如果模式串存在一个前缀,与子串`P[j+1, m)`的后缀相互匹配,那么这也是一个值得的对齐位置,如下图所示: -![gs_case2](gs_case2.png) +![gs_case2](images/gs_case2.png) 和`bc`算法和`kmp`算法一样,如果这样的对齐位置有多个,应该取出其中移动距离最短的一个,从而不会错过其他的对齐位置。并且仿照`bc`策略和`kmp`算法的思想,可以预先构造一个`gs`表,其中`gs[i]`表示在第`i`个位置比对失败后,按照`好后缀策略`应该采取的位移量。这样,就可以通过`bc`表和`gs`表把两个策略结合起来,具体说来,由于两个策略都是给出可能匹配的必要条件,因此值得对齐的位置一定同时满足这两个必要条件,在一次匹配失败后,可以同时查询`gs[i]`和`bc[i]`,并且选择它们给出的移动距离的最大值,来作为最终的移动距离,具体的代码如下: @@ -127,7 +127,7 @@ int match(char* text, char* pattern){ 为了构造`gs`表,首先引入`ss`表的概念——`ss[i]`是表示在`P[0, i]`的所有后缀中,与`P`的某一后缀匹配的最长长度,即最长匹配后缀(maximum matched suffix)的长度。如下图所示: -![ss_definition](ss_definition.png) +![ss_definition](images/ss_definition.png) 如果可以成功地构造出`ss`表的话,就可以用`ss`表快捷地构造出`gs`表,因为`ss`表中包含了`gs`表中的全部信息。 @@ -143,7 +143,7 @@ ss[j] == j + 1; 如下图所示: -![ss2gs_case1](ss2gs_case1.png) +![ss2gs_case1](images/ss2gs_case1.png) 此时,对于模式串中所有的秩为`i`的字符,如果有`i < m - j - 1`,则`MS[j]`都是在该处匹配失败的一个候选对齐位置,对应了上面好后缀的第二种情形,此时它们的移动距离距离都是`m - j - 1`,即`m - j -1`必然是`gs[i]`的一个候选。需要指出的是,这种情形并不适用于`i >= m - j - 1`的情形,因为首位两个子串完全匹配,在该位置对齐后的下一次匹配必然会失败。 @@ -155,7 +155,7 @@ ss[j] < j + 1; 如下图所示: -![ss2gs_case2](ss2gs_case2.png) +![ss2gs_case2](images/ss2gs_case2.png) 在这种情况下,`MS[j]`只能作为在位置`m - ss[j] - 1`处比对失败的候选对齐位置。这是因为,假如`i > m - ss[j] - 1`,这里的情形与上面讨论的一致,两个子串完全匹配,在该位置对齐后的下一次匹配必然会失败;而假如`i < m - ss[j] - 1`,由于`ss[j]`的最值性,`MS[j]`的前一个字符必然与`P[m - ss[j] - 2]`不相等,因此这并不是一个有意义的对齐位置。综上,此时`m - j - 1`是`gs[m - ss[j] - 1]`的一个候选。 @@ -199,7 +199,7 @@ int* buildGS(char* P){ 第一种情形如下图所示: -![buildss_case1](buildss_case1.png) +![buildss_case1](images/buildss_case1.png) 在构造`ss`表的过程中,动态地保存和更新之前的极大匹配后缀,分别用`lo`和`hi`来表示它的范围,即`P(lo, hi]`。此时`j`位于`lo`和`hi`之间,因此就可以找到这样一个位置`m - hi + j - 1`,以这两个位置为后缀的子串,至少拥有`j - lo`个完全匹配的字符。第一种情形是 @@ -221,7 +221,7 @@ ss[m - hi + j - 1] > j - lo 则对应了这里的第二种情形,如下图所示: -![buildss_case2](buildss_case2.png) +![buildss_case2](images/buildss_case2.png) 在这种情况下,根据已有的信息,只能知道`P(lo, j]`与`P(m - hi + lo - 1, m - hi + j - 1]`是相互匹配的,因此`P(lo, j]`与`P(m + lo - j - 1, m - 1]`是相互匹配的,故`ss[j]`至少为`j - lo`。此时,`MS[j]`有可能继续向左侧扩展,因此需要依次对接下来的字符进行比对。此时将更新`hi = j`,并在一次比对成功后更新`lo`的值,即`--lo`,直到这样的比对失败,即可确定当前的`ss[j]`。 diff --git a/thu_dsa/chp11/bad_character.png b/数据结构/chp11/images/bad_character.png similarity index 100% rename from thu_dsa/chp11/bad_character.png rename to 数据结构/chp11/images/bad_character.png diff --git a/thu_dsa/chp11/bc_bestcase.png b/数据结构/chp11/images/bc_bestcase.png similarity index 100% rename from thu_dsa/chp11/bc_bestcase.png rename to 数据结构/chp11/images/bc_bestcase.png diff --git a/thu_dsa/chp11/bc_case12.png b/数据结构/chp11/images/bc_case12.png similarity index 100% rename from thu_dsa/chp11/bc_case12.png rename to 数据结构/chp11/images/bc_case12.png diff --git a/thu_dsa/chp11/bc_case3.png b/数据结构/chp11/images/bc_case3.png similarity index 100% rename from thu_dsa/chp11/bc_case3.png rename to 数据结构/chp11/images/bc_case3.png diff --git a/thu_dsa/chp11/bc_worstcase.png b/数据结构/chp11/images/bc_worstcase.png similarity index 100% rename from thu_dsa/chp11/bc_worstcase.png rename to 数据结构/chp11/images/bc_worstcase.png diff --git a/thu_dsa/chp11/brute_force.png b/数据结构/chp11/images/brute_force.png similarity index 100% rename from thu_dsa/chp11/brute_force.png rename to 数据结构/chp11/images/brute_force.png diff --git a/thu_dsa/chp11/brute_force_flaw.png b/数据结构/chp11/images/brute_force_flaw.png similarity index 100% rename from thu_dsa/chp11/brute_force_flaw.png rename to 数据结构/chp11/images/brute_force_flaw.png diff --git a/thu_dsa/chp11/brute_force_worst.png b/数据结构/chp11/images/brute_force_worst.png similarity index 100% rename from thu_dsa/chp11/brute_force_worst.png rename to 数据结构/chp11/images/brute_force_worst.png diff --git a/thu_dsa/chp11/buildss_case1.png b/数据结构/chp11/images/buildss_case1.png similarity index 100% rename from thu_dsa/chp11/buildss_case1.png rename to 数据结构/chp11/images/buildss_case1.png diff --git a/thu_dsa/chp11/buildss_case2.png b/数据结构/chp11/images/buildss_case2.png similarity index 100% rename from thu_dsa/chp11/buildss_case2.png rename to 数据结构/chp11/images/buildss_case2.png diff --git a/thu_dsa/chp11/compare_backwards.png b/数据结构/chp11/images/compare_backwards.png similarity index 100% rename from thu_dsa/chp11/compare_backwards.png rename to 数据结构/chp11/images/compare_backwards.png diff --git a/thu_dsa/chp11/gs_case1.png b/数据结构/chp11/images/gs_case1.png similarity index 100% rename from thu_dsa/chp11/gs_case1.png rename to 数据结构/chp11/images/gs_case1.png diff --git a/thu_dsa/chp11/gs_case2.png b/数据结构/chp11/images/gs_case2.png similarity index 100% rename from thu_dsa/chp11/gs_case2.png rename to 数据结构/chp11/images/gs_case2.png diff --git a/thu_dsa/chp11/kmp_improvement.png b/数据结构/chp11/images/kmp_improvement.png similarity index 100% rename from thu_dsa/chp11/kmp_improvement.png rename to 数据结构/chp11/images/kmp_improvement.png diff --git a/thu_dsa/chp11/ss2gs_case1.png b/数据结构/chp11/images/ss2gs_case1.png similarity index 100% rename from thu_dsa/chp11/ss2gs_case1.png rename to 数据结构/chp11/images/ss2gs_case1.png diff --git a/thu_dsa/chp11/ss2gs_case2.png b/数据结构/chp11/images/ss2gs_case2.png similarity index 100% rename from thu_dsa/chp11/ss2gs_case2.png rename to 数据结构/chp11/images/ss2gs_case2.png diff --git a/thu_dsa/chp11/ss_definition.png b/数据结构/chp11/images/ss_definition.png similarity index 100% rename from thu_dsa/chp11/ss_definition.png rename to 数据结构/chp11/images/ss_definition.png diff --git a/thu_dsa/chp11/update_fingerprint.png b/数据结构/chp11/images/update_fingerprint.png similarity index 100% rename from thu_dsa/chp11/update_fingerprint.png rename to 数据结构/chp11/images/update_fingerprint.png diff --git a/thu_dsa/chp11/karp_rabin.md b/数据结构/chp11/karp_rabin.md similarity index 99% rename from thu_dsa/chp11/karp_rabin.md rename to 数据结构/chp11/karp_rabin.md index 453a4e4..d24c6c8 100644 --- a/thu_dsa/chp11/karp_rabin.md +++ b/数据结构/chp11/karp_rabin.md @@ -37,7 +37,7 @@ hash(fingerprint) = fingerprint % M; 但是对于文本串则不然,诚然,对于任意一个长度为`m`的串,计算其指纹也必须需要`O(m)`的时间开销,但是在文本串中,可以注意到,相邻串的指纹是具有一定的联系的,如下图所示: -![update_fingerprint](update_fingerprint.png) +![update_fingerprint](images/update_fingerprint.png) 具体说来,相邻串只有最前一个字符和最后一个字符是不相同的,利用模余的运算法则,就可以根据前一个串的指纹,在`O(1)`时间内计算出下一个串的指纹。设`a, b`分别是两个正整数,且有`a > b > 0`,具体利用到的运算法则是, diff --git a/thu_dsa/chp11/kmp.cpp b/数据结构/chp11/kmp.cpp similarity index 100% rename from thu_dsa/chp11/kmp.cpp rename to 数据结构/chp11/kmp.cpp diff --git a/thu_dsa/chp11/kmp.h b/数据结构/chp11/kmp.h similarity index 100% rename from thu_dsa/chp11/kmp.h rename to 数据结构/chp11/kmp.h diff --git a/thu_dsa/chp11/test_kmp.cpp b/数据结构/chp11/test_kmp.cpp similarity index 100% rename from thu_dsa/chp11/test_kmp.cpp rename to 数据结构/chp11/test_kmp.cpp diff --git a/thu_dsa/chp12/sort.md b/数据结构/chp12/README.md similarity index 97% rename from thu_dsa/chp12/sort.md rename to 数据结构/chp12/README.md index 50288f7..9d92bea 100644 --- a/thu_dsa/chp12/sort.md +++ b/数据结构/chp12/README.md @@ -1,8 +1,13 @@ -Conclusions on Sorting Algorithm -================================ +知识总结:排序算法 +================ ## 知识脉络总结 ++ [快速排序](quicksort.md) ++ [希尔排序](shellsort.md) + +--- + `快速排序`的思想是简单而深刻的,即`分而治之`的策略,需要注意它和`归并排序`在思想上的异同。`快速排序`的关键在于`partition`算法,本章给出了`partition`算法的三种实现,需要领会这三种不同的实现各自的特点是什么,是为了解决什么问题而出现的。需要指出的是,尽管在一般情况下`快速排序`运行的平均时间最短(这也是它被称作`快速排序`的原因),可是在最坏情况下`快速排序`仍然需要`O(n^2)`的时间,可以通过改造`轴点`的选择算法,在一定程度上减小最坏情况出现的概率。在平均情况下,`快速排序`仅需要`O(nlogn)`的时间复杂度,要会证明这个结论。 `希尔排序`的正确性和有穷性不言而喻,其时间复杂度主要取决于`增量序列`的选择,例如`希尔序列`就是一个比较糟糕的序列,需要能够领会为什么`希尔序列`的性能较差。为了分析`希尔排序`的时间复杂度,首先需要理解`邮资问题`和`g-有序`的序列经过`h-排序`后仍然保持`g-有序`的证明,在此基础上,即可针对不同的`增量序列`给出复杂度的证明。 diff --git a/thu_dsa/chp12/quicksort.cpp b/数据结构/chp12/quicksort.cpp similarity index 100% rename from thu_dsa/chp12/quicksort.cpp rename to 数据结构/chp12/quicksort.cpp diff --git a/thu_dsa/chp12/quicksort.md b/数据结构/chp12/quicksort.md similarity index 100% rename from thu_dsa/chp12/quicksort.md rename to 数据结构/chp12/quicksort.md diff --git a/thu_dsa/chp12/shellsort.cpp b/数据结构/chp12/shellsort.cpp similarity index 100% rename from thu_dsa/chp12/shellsort.cpp rename to 数据结构/chp12/shellsort.cpp diff --git a/thu_dsa/chp12/shellsort.md b/数据结构/chp12/shellsort.md similarity index 100% rename from thu_dsa/chp12/shellsort.md rename to 数据结构/chp12/shellsort.md diff --git a/thu_dsa/chp2/Fib.cpp b/数据结构/chp2/Fib.cpp similarity index 100% rename from thu_dsa/chp2/Fib.cpp rename to 数据结构/chp2/Fib.cpp diff --git a/thu_dsa/chp2/chp2.md b/数据结构/chp2/README.md similarity index 91% rename from thu_dsa/chp2/chp2.md rename to 数据结构/chp2/README.md index 6e5cd6b..0952409 100644 --- a/thu_dsa/chp2/chp2.md +++ b/数据结构/chp2/README.md @@ -1,5 +1,5 @@ -Conclusion on Chapter Two: Vector -================================= +知识总结:向量 +============= ## 知识脉络 @@ -56,7 +56,7 @@ Vector myVector; > 有哪些扩容策略? -最容易想到的肯定是容量递增策略。他的基本思想是每次扩容都增加一个固定的大小`INCREMENT`,代码实现如下: +最容易想到的肯定是容量递增策略。他的基本思想是每次扩容都增加一个固定的大小 `INCREMENT` ,代码实现如下: ``` void Vector::expand(){ @@ -90,7 +90,7 @@ void Vector::expand(){ 如果可以忽略重新分配内存的时间的话,每次扩容的主要时间开销是将数据从原来的区域,拷贝到新分配的区域。显然,对于两种算法而已,这个操作的时间复杂度是相同的,都是$O(n)$。所以,直接比较两个算法每次扩容的时间性能,是没有意义的。 -所以这里需要引入`分摊复杂度`的概念。所谓分摊复杂度,是指对数据结构连续地实施足够多次的操作后,所需总体成本分摊到单次操作的时间复杂度。它与平均复杂度是有本质区别的。平均复杂度是指假定各种输入情况满足一定的概率分布,进而估算出的加权复杂度均值,这里所做的概率分布可以是不符合真实情况的,也不必考察各种情况出现的次序,因此在一定程度上不符合真实情况,比如说对于扩容策略地分析,我可以假定每次插入都有`50%`的概率扩容,但是后面可以看到,这是不正确的。而分摊复杂度的操作序列确实是可以发生的,它更关注整体的性能,是因此更符合与真实的情况,比平均复杂度更为可信。 +所以这里需要引入`分摊复杂度`的概念。所谓分摊复杂度,是指对数据结构连续地实施足够多次的操作后,所需总体成本分摊到单次操作的时间复杂度。它与平均复杂度是有本质区别的。平均复杂度是指假定各种输入情况满足一定的概率分布,进而估算出的加权复杂度均值,这里所做的概率分布可以是不符合真实情况的,也不必考察各种情况出现的次序,因此在一定程度上不符合真实情况,比如说对于扩容策略地分析,我可以假定每次插入都有 `50%` 的概率扩容,但是后面可以看到,这是不正确的。而分摊复杂度的操作序列确实是可以发生的,它更关注整体的性能,是因此更符合与真实的情况,比平均复杂度更为可信。 下面,我们分别考察两种扩容策略的分摊复杂度。 @@ -120,7 +120,7 @@ $$ 首先还是考虑无序向量的唯一化。其实这就比较简单,因为你也不能做什么优化。对于当前的第k个元素,总是保证前k-1个元素是唯一化的,然后在前k-1个元素中遍历以查找当前元素。如果查找成功,则删除当前元素(后继元素全部向前移位),若查找失败,则唯一化序列长度加1,处理第k+1个元素。 -上面的算法,显然具有$O(n^2)$的时间复杂度。当然可以借助`set`或者排序来实现$O(nlogn)$的时间复杂度,但不是我们这里讨论的范围。 +上面的算法,显然具有$O(n^2)$的时间复杂度。当然可以借助 `set` 或者排序来实现$O(nlogn)$的时间复杂度,但不是我们这里讨论的范围。 那么,对于有序向量,其和无序向量相比有什么不同呢? @@ -162,16 +162,16 @@ int Vector::unique(){ } ``` -所以就这么几行代码,就实现了$O(n)$的时间复杂度和$O(1)$的空间复杂度。所以说,代码不在于量的多少,而在于思考与逻辑。这个算法里面,跟我的思路比起来,根本就没有用到删除操作。而是用`i`指示当前唯一化序列的最后一个元素,用`j`指示当前全局遍历的元素。 +所以就这么几行代码,就实现了$O(n)$的时间复杂度和$O(1)$的空间复杂度。所以说,代码不在于量的多少,而在于思考与逻辑。这个算法里面,跟我的思路比起来,根本就没有用到删除操作。而是用`i`指示当前唯一化序列的最后一个元素,用 `j` 指示当前全局遍历的元素。 -+ 如果`j`指示的是一个重复出现的元素,则直接将它忽略掉。 -+ 而如果`j`指示元素是一个新的唯一化元素,就把它添加到i的后面。 ++ 如果 `j` 指示的是一个重复出现的元素,则直接将它忽略掉。 ++ 而如果 `j` 指示元素是一个新的唯一化元素,就把它添加到i的后面。 这样,只需要$O(n)$的时间复杂度就完成有序向量的唯一化操作。可见,我的问题在于没有跳出删除操作的限制,从而看不到优化的途径。 ## 有序向量的查找操作 -> `search`函数的接口 +> `search` 函数的接口 有必要定义一下查找函数的接口是怎么样的,因为可能存在多种情况。例如如果Vector中存在若干个相同的元素,是应该返回哪个元素的位置?又比如如果没有查找到目标元素,是应该返回一个错误码,还是返回距离目标元素最近的元素,诸如此类,都需要预先在ADT中进行定义。 @@ -181,7 +181,7 @@ int Vector::unique(){ + 如果没有找到目标元素,则返回小于目标元素的最后一个元素 -上面两条可以总结为,返回不大于目标元素的最后一个元素,也就是`upper_bound`。 +上面两条可以总结为,返回不大于目标元素的最后一个元素,也就是 `upper_bound`。 这样的接口会使其他操作变得简单。比如插入操作,考虑一种情况,如果在Vector中没有找到目标元素e,则插入目标元素。在上面的接口定义下,可以轻易地找到目标元素应该插入的位置,从而简化插入操作。 @@ -217,7 +217,7 @@ int Vector::search(T e, int lo, int hi){ 上面说二分查找右边的代价高于左边的代价,作为一种优化的思路,可以想到应该尽量查找进入左边,从而减少高代价操作的次数,这个思想和哈夫曼树是一样的。斐波拉契查找就是这样一种算法。 -斐波拉契查找的基本思想是不使用平均二分来划分左右两个子问题,而是使用黄金分割比来划分左右两个子区域,其中左边的区域会大于右边的区域,从而减少右边高代价操作的次数。这里要利用到一个`Fib`类,利用它可以在$O(logn)$的时间内创建最大值小于n的斐波拉契序列。其代码如下: +斐波拉契查找的基本思想是不使用平均二分来划分左右两个子问题,而是使用黄金分割比来划分左右两个子区域,其中左边的区域会大于右边的区域,从而减少右边高代价操作的次数。这里要利用到一个 `Fib` 类,利用它可以在$O(logn)$的时间内创建最大值小于n的斐波拉契序列。其代码如下: ``` int Vector::search(T e, int lo, int hi){ @@ -252,9 +252,9 @@ int Vector::search(T e, int lo, int hi){ } ``` -由于查找区间是[lo,hi),因此我把相同的情况归入到右边的情况,否则`hi = mid`会直接将相同的元素排除在外。 +由于查找区间是[lo,hi),因此我把相同的情况归入到右边的情况,否则 `hi = mid` 会直接将相同的元素排除在外。 -以及由于右边的情况是`lo = mid`,如果循环条件是`while(lo < hi)`的话,则有可能出现死循环,所以需要改成`while(lo < hi - 1)`。 +以及由于右边的情况是 `lo = mid` ,如果循环条件是 `while(lo < hi)` 的话,则有可能出现死循环,所以需要改成 `while(lo < hi - 1)`。 这个版本的二分查找虽然在比较相同的情况下也还需要继续查找,但是整体效率更加稳定,平均情况下的时间复杂度更好。 @@ -275,8 +275,8 @@ int Vector::search(T e, int lo, int hi){ 可以看到,同样实现地非常简练,但是其内部逻辑其实非常完备和复杂。 -+ 这里左边的操作又变成了`lo = mid + 1`,也就是说直接忽略相等的情况。这样,退出循环时`lo`总是指向大于目标元素的第一个元素。因此返回值应该是lo的前面一个元素(`--lo`) -+ 由于左边操作变成了`lo = mid + 1`,循环条件又回到了`while(lo < hi)`,因为此时每一步操作都会缩小问题的规模,不会出现死循环了。 ++ 这里左边的操作又变成了 `lo = mid + 1`,也就是说直接忽略相等的情况。这样,退出循环时 `lo` 总是指向大于目标元素的第一个元素。因此返回值应该是lo的前面一个元素(`--lo`) ++ 由于左边操作变成了 `lo = mid + 1`,循环条件又回到了 `while(lo < hi)`,因为此时每一步操作都会缩小问题的规模,不会出现死循环了。 > 插值查找 @@ -333,7 +333,7 @@ bool Vector::bubble(int lo, int hi){ } ``` -这里用到一个整体有序标志`sorted`来判断Vector是否已经全局有序,以防止额外的操作。 +这里用到一个整体有序标志 `sorted` 来判断Vector是否已经全局有序,以防止额外的操作。 > 对上面版本的冒泡排序进行优化 @@ -341,7 +341,7 @@ bool Vector::bubble(int lo, int hi){ 但是实际上,它根本不需要这么高的时间复杂度。因为要是可以检测到该序列的局部有序性,只给前面的k个元素排序的话,就只需要$O(k^2) = O(n)$的时间代价就可以完成。 -因此,我们需要知道每一次`bubble`最后进行的交换操作。而这并不难做到,只需要将`sorted`标志位改成最后一次交换的位置就可以了。根据这个位置信息,就可以知道当前序列的局部有序程度,从而对上面的算法进行优化。代码如下: +因此,我们需要知道每一次 `bubble` 最后进行的交换操作。而这并不难做到,只需要将 `sorted` 标志位改成最后一次交换的位置就可以了。根据这个位置信息,就可以知道当前序列的局部有序程度,从而对上面的算法进行优化。代码如下: ``` void Vector::bubble_sort(int lo, int hi){ diff --git a/thu_dsa/chp2/Vector.h b/数据结构/chp2/Vector.h similarity index 100% rename from thu_dsa/chp2/Vector.h rename to 数据结构/chp2/Vector.h diff --git a/thu_dsa/chp2/bin_fib_search_len.md b/数据结构/chp2/bin_fib_search_len.md similarity index 100% rename from thu_dsa/chp2/bin_fib_search_len.md rename to 数据结构/chp2/bin_fib_search_len.md diff --git a/thu_dsa/chp2/exercises.md b/数据结构/chp2/exercises.md similarity index 100% rename from thu_dsa/chp2/exercises.md rename to 数据结构/chp2/exercises.md diff --git a/thu_dsa/chp2/testFib.cpp b/数据结构/chp2/testFib.cpp similarity index 100% rename from thu_dsa/chp2/testFib.cpp rename to 数据结构/chp2/testFib.cpp diff --git a/thu_dsa/chp2/testVector.cpp b/数据结构/chp2/testVector.cpp similarity index 100% rename from thu_dsa/chp2/testVector.cpp rename to 数据结构/chp2/testVector.cpp diff --git a/thu_dsa/chp3/List.h b/数据结构/chp3/List.h similarity index 100% rename from thu_dsa/chp3/List.h rename to 数据结构/chp3/List.h diff --git a/thu_dsa/chp3/ListNode.h b/数据结构/chp3/ListNode.h similarity index 100% rename from thu_dsa/chp3/ListNode.h rename to 数据结构/chp3/ListNode.h diff --git a/thu_dsa/chp3/chp3.md b/数据结构/chp3/README.md similarity index 99% rename from thu_dsa/chp3/chp3.md rename to 数据结构/chp3/README.md index 24d72d6..bec2705 100644 --- a/thu_dsa/chp3/chp3.md +++ b/数据结构/chp3/README.md @@ -1,5 +1,5 @@ -Conclusion on Chapter Three: List -================================= +知识总结:列表 +============= ## 知识脉络 diff --git a/thu_dsa/chp3/test_list.cpp b/数据结构/chp3/test_list.cpp similarity index 100% rename from thu_dsa/chp3/test_list.cpp rename to 数据结构/chp3/test_list.cpp diff --git a/thu_dsa/chp4/Queue.h b/数据结构/chp4/Queue.h similarity index 100% rename from thu_dsa/chp4/Queue.h rename to 数据结构/chp4/Queue.h diff --git a/thu_dsa/chp4/chp4.md b/数据结构/chp4/README.md similarity index 93% rename from thu_dsa/chp4/chp4.md rename to 数据结构/chp4/README.md index d31ee30..82db8f8 100644 --- a/thu_dsa/chp4/chp4.md +++ b/数据结构/chp4/README.md @@ -1,9 +1,9 @@ -Conclusion on Chapter Four: Stack & Queue -========================================= +知识总结:栈与队列 +================= ## 知识脉络梳理 -本章主要是讨论了栈(和队列)。借助前面的`Vector`或者`List`,都可以比较轻易地实现两种数据结构,只不过在一些细节上需要在意。栈和队列的基本操作都只需要`O(1)`的时间,它们更多是作为算法设计的基本出发点,来构造更为复杂的算法。因此本章主要是讨论两种数据结构的应用。 +本章主要是讨论了栈和队列。借助前面的`Vector`或者`List`,都可以比较轻易地实现两种数据结构,只不过在一些细节上需要在意。栈和队列的基本操作都只需要`O(1)`的时间,它们更多是作为算法设计的基本出发点,来构造更为复杂的算法。因此本章主要是讨论两种数据结构的应用。 为了用栈或者队列构造其他算法,关键在于把握它们的`LIFO`(栈)或者`FIFO`(队列)的特性。利用栈的特性,可以实现`逆序输出`,`递归嵌套`,`延迟缓冲`以及`试探回溯`的算法。而队列在模拟现实生活中的排队现象中比较有用武之地。 @@ -111,13 +111,13 @@ bool paren(const char exp[]){ 根据上面的描述可以看出,栈混洗只需要满足唯一一个条件:出栈时栈不得为空。只要满足该条件的一个序列都是一个合法的栈混洗。这个和上面描述的括号匹配问题是不是感觉有一点相似啊?其实它们之间本质就是一样的,一个入栈操作可以看做是一个左括号,一个出栈操作可以看做是一个右括号,合法的栈混洗要求入栈操作总是大于等于出栈操作,匹配的括号表达式要求左括号数量总是大于等于右括号。 -可以通过递推表达式或者排列组合的方法来得到栈混洗的数量。关于这个可以参考[栈混洗的数量](https://www.cnblogs.com/jiayouwyhit/p/3222973.html)。从结论上来说,所有的操作数量有$C_{2n}^{n}$个,其中合法的操作数有$C_{2n} - C_{2n}^{n-1}$个。 +可以通过递推表达式或者排列组合的方法来得到栈混洗的数量。关于这个可以参考 [栈混洗的数量](https://www.cnblogs.com/jiayouwyhit/p/3222973.html)。从结论上来说,所有的操作数量有$C_{2n}^{n}$个,其中合法的操作数有$C_{2n} - C_{2n}^{n-1}$个。 > 合法栈混洗的快速判断。 经常会有这种问题,给你一个出栈序列,需要你判断它是否是合法的。慢慢模拟吧来的太慢了,没有耐心。所以需要开发出一些快速的方法。 -一种方法是,出栈序列中,元素i之后比其小的元素必须是降序排列的。这是比较容易理解的,因为在出栈序列中,排列在元素i之后且比i小的元素,在元素i出栈时必然已经入栈了,并且在元素i出栈时仍然滞留在栈中,他们之后的出栈次序当然是递减的。具体可以看[这篇](https://blog.csdn.net/Xiao__Tian__/article/details/51111632)。 +一种方法是,出栈序列中,元素i之后比其小的元素必须是降序排列的。这是比较容易理解的,因为在出栈序列中,排列在元素i之后且比i小的元素,在元素i出栈时必然已经入栈了,并且在元素i出栈时仍然滞留在栈中,他们之后的出栈次序当然是递减的。具体可以看 [这篇文章](https://blog.csdn.net/Xiao__Tian__/article/details/51111632)。 另一种方法是我邓公的方法。合法栈混洗的充要条件是它的出栈序列不含`312`模式。这个条件的必要性是显然的,因为`312`是三个元素栈混洗中唯一不合法的那个。但是充分性的证明好像有点复杂,至今不会...... diff --git a/thu_dsa/chp4/Stack.h b/数据结构/chp4/Stack.h similarity index 100% rename from thu_dsa/chp4/Stack.h rename to 数据结构/chp4/Stack.h diff --git a/thu_dsa/chp4/notice.md b/数据结构/chp4/notice.md similarity index 100% rename from thu_dsa/chp4/notice.md rename to 数据结构/chp4/notice.md diff --git a/thu_dsa/chp4/stackApp.cpp b/数据结构/chp4/stackApp.cpp similarity index 100% rename from thu_dsa/chp4/stackApp.cpp rename to 数据结构/chp4/stackApp.cpp diff --git a/thu_dsa/chp4/stackApp.h b/数据结构/chp4/stackApp.h similarity index 100% rename from thu_dsa/chp4/stackApp.h rename to 数据结构/chp4/stackApp.h diff --git a/thu_dsa/chp4/testQueue.cpp b/数据结构/chp4/testQueue.cpp similarity index 100% rename from thu_dsa/chp4/testQueue.cpp rename to 数据结构/chp4/testQueue.cpp diff --git a/thu_dsa/chp4/testStack.cpp b/数据结构/chp4/testStack.cpp similarity index 100% rename from thu_dsa/chp4/testStack.cpp rename to 数据结构/chp4/testStack.cpp diff --git a/thu_dsa/chp4/testStackApp.cpp b/数据结构/chp4/testStackApp.cpp similarity index 100% rename from thu_dsa/chp4/testStackApp.cpp rename to 数据结构/chp4/testStackApp.cpp diff --git a/thu_dsa/chp5/chp5.md b/数据结构/chp5/README.md similarity index 99% rename from thu_dsa/chp5/chp5.md rename to 数据结构/chp5/README.md index 435d30b..fd5408c 100644 --- a/thu_dsa/chp5/chp5.md +++ b/数据结构/chp5/README.md @@ -1,5 +1,5 @@ -Conclusion on Chapter Five: Binary Tree -======================================= +知识总结:二叉树 +=============== ## 知识脉络 diff --git a/thu_dsa/chp5/binNode.h b/数据结构/chp5/binNode.h similarity index 100% rename from thu_dsa/chp5/binNode.h rename to 数据结构/chp5/binNode.h diff --git a/thu_dsa/chp5/binTree.h b/数据结构/chp5/binTree.h similarity index 100% rename from thu_dsa/chp5/binTree.h rename to 数据结构/chp5/binTree.h diff --git a/thu_dsa/chp5/images/inOrder.png b/数据结构/chp5/images/inOrder.png similarity index 100% rename from thu_dsa/chp5/images/inOrder.png rename to 数据结构/chp5/images/inOrder.png diff --git a/thu_dsa/chp5/images/postOrder.png b/数据结构/chp5/images/postOrder.png similarity index 100% rename from thu_dsa/chp5/images/postOrder.png rename to 数据结构/chp5/images/postOrder.png diff --git a/thu_dsa/chp5/images/preOrder.png b/数据结构/chp5/images/preOrder.png similarity index 100% rename from thu_dsa/chp5/images/preOrder.png rename to 数据结构/chp5/images/preOrder.png diff --git a/thu_dsa/chp5/testBinTree.cpp b/数据结构/chp5/testBinTree.cpp similarity index 100% rename from thu_dsa/chp5/testBinTree.cpp rename to 数据结构/chp5/testBinTree.cpp diff --git a/thu_dsa/chp6/Edge.h b/数据结构/chp6/Edge.h similarity index 100% rename from thu_dsa/chp6/Edge.h rename to 数据结构/chp6/Edge.h diff --git a/thu_dsa/chp6/Graph.h b/数据结构/chp6/Graph.h similarity index 100% rename from thu_dsa/chp6/Graph.h rename to 数据结构/chp6/Graph.h diff --git a/thu_dsa/chp6/GraphMatrix.cpp b/数据结构/chp6/GraphMatrix.cpp similarity index 100% rename from thu_dsa/chp6/GraphMatrix.cpp rename to 数据结构/chp6/GraphMatrix.cpp diff --git a/thu_dsa/chp6/GraphMatrix.h b/数据结构/chp6/GraphMatrix.h similarity index 100% rename from thu_dsa/chp6/GraphMatrix.h rename to 数据结构/chp6/GraphMatrix.h diff --git a/thu_dsa/chp6/Graph.md b/数据结构/chp6/README.md similarity index 99% rename from thu_dsa/chp6/Graph.md rename to 数据结构/chp6/README.md index 2881c3c..20b0c0f 100644 --- a/thu_dsa/chp6/Graph.md +++ b/数据结构/chp6/README.md @@ -1,5 +1,5 @@ -Conclusion on Chapter Six: Graph -================================ +知识总结:图 +============ ## 知识脉络 @@ -343,5 +343,3 @@ void Graph::dijkstraPU(int x){ } } ``` - - diff --git a/thu_dsa/chp6/Vertex.h b/数据结构/chp6/Vertex.h similarity index 100% rename from thu_dsa/chp6/Vertex.h rename to 数据结构/chp6/Vertex.h diff --git a/thu_dsa/chp6/testGraph.cpp b/数据结构/chp6/testGraph.cpp similarity index 100% rename from thu_dsa/chp6/testGraph.cpp rename to 数据结构/chp6/testGraph.cpp diff --git a/thu_dsa/chp6/testGraphMatrix.cpp b/数据结构/chp6/testGraphMatrix.cpp similarity index 100% rename from thu_dsa/chp6/testGraphMatrix.cpp rename to 数据结构/chp6/testGraphMatrix.cpp diff --git a/thu_dsa/chp7/AVL.h b/数据结构/chp7/AVL.h similarity index 100% rename from thu_dsa/chp7/AVL.h rename to 数据结构/chp7/AVL.h diff --git a/thu_dsa/chp7/AVL.md b/数据结构/chp7/AVL.md similarity index 99% rename from thu_dsa/chp7/AVL.md rename to 数据结构/chp7/AVL.md index d5f20bd..48677da 100644 --- a/thu_dsa/chp7/AVL.md +++ b/数据结构/chp7/AVL.md @@ -1,4 +1,4 @@ -Conclusion on AVL +知识总结:AVL 树 ================= ## 平衡树 diff --git a/thu_dsa/chp7/BST.h b/数据结构/chp7/BST.h similarity index 100% rename from thu_dsa/chp7/BST.h rename to 数据结构/chp7/BST.h diff --git a/thu_dsa/chp7/BST.md b/数据结构/chp7/README.md similarity index 96% rename from thu_dsa/chp7/BST.md rename to 数据结构/chp7/README.md index d0bda5b..3d74b10 100644 --- a/thu_dsa/chp7/BST.md +++ b/数据结构/chp7/README.md @@ -1,8 +1,18 @@ -Conclusion on BST +知识总结:二叉搜索树 ================= ## BST与BBST知识脉络 +本章和下一章讨论了几个常见的二叉搜索树,包括 AVL 树,伸展树,B 树,红黑树以及 k-d 树。这篇知识总结首先对知识脉络进行了梳理,之后详细介绍了一般的二叉搜索树。对于某个特定的二叉搜索树,其知识总结如下: + ++ [AVL 树](AVL.md) ++ [伸展树](../chp8/SplayTree.md) ++ [B 树](../chp8/B-tree.md) ++ [红黑树](../chp8/RB-tree.md) ++ [关于几个平衡搜索树的深入探讨](关于几个平衡搜索树的深入探讨.md) + +--- + 理解二叉搜索树的关键在于它的定义,即对于任一节点,其左子树的所有节点均不大于它,右子树的所有节点均不小于它,即`局部有序性`;通过二叉搜索树的`局部有序性`,可以证明得到它的`全局单调性`,因此可以仿照有序向量,在二叉搜索树上实现高效的查找算法。对二叉搜索树的动态操作,即插入与删除,关键也在于维护它的`局部有序性`。因此对于插入操作,首先需要调用一次`search`接口定位插入的位置;而对于删除操作,如果被删除节点没有左子树或者右子树,则可以用它的另一棵子树来替代它,如果同时具有左右子树,则需要用它的直接后继来替代它,随后删除一定没有左子树的直接后继。 容易注意到,二叉搜索树的搜索操作非常类似`二分查找`,但是它存在的问题是每次比较的节点未必就是中间节点,即它的`平衡性`问题——在最坏的情况下,二叉搜索树退化为单链表,其搜索操作的时间复杂度也退化为`O(n)`。为了保持二叉搜索树的平衡性,需要增加一些结构上的限制条件,即平衡条件,这样的二叉搜索树就成为了平衡二叉搜索树。对于任何一种`BBST`,都需要关注两个问题,即平衡条件和失衡调整算法。 diff --git a/thu_dsa/chp7/images/BSTSearch.png b/数据结构/chp7/images/BSTSearch.png similarity index 100% rename from thu_dsa/chp7/images/BSTSearch.png rename to 数据结构/chp7/images/BSTSearch.png diff --git a/thu_dsa/chp7/images/monotony.png b/数据结构/chp7/images/monotony.png similarity index 100% rename from thu_dsa/chp7/images/monotony.png rename to 数据结构/chp7/images/monotony.png diff --git a/thu_dsa/chp7/images/non-BST.png b/数据结构/chp7/images/non-BST.png similarity index 100% rename from thu_dsa/chp7/images/non-BST.png rename to 数据结构/chp7/images/non-BST.png diff --git a/thu_dsa/chp7/test_AVL.cpp b/数据结构/chp7/test_AVL.cpp similarity index 100% rename from thu_dsa/chp7/test_AVL.cpp rename to 数据结构/chp7/test_AVL.cpp diff --git a/thu_dsa/chp7/test_BST.cpp b/数据结构/chp7/test_BST.cpp similarity index 100% rename from thu_dsa/chp7/test_BST.cpp rename to 数据结构/chp7/test_BST.cpp diff --git a/thu_dsa/chp7/notes.md b/数据结构/chp7/关于几个平衡搜索树的深入探讨.md similarity index 100% rename from thu_dsa/chp7/notes.md rename to 数据结构/chp7/关于几个平衡搜索树的深入探讨.md diff --git a/thu_dsa/chp8/B-tree.md b/数据结构/chp8/B-tree.md similarity index 100% rename from thu_dsa/chp8/B-tree.md rename to 数据结构/chp8/B-tree.md diff --git a/thu_dsa/chp8/BTNode.h b/数据结构/chp8/BTNode.h similarity index 100% rename from thu_dsa/chp8/BTNode.h rename to 数据结构/chp8/BTNode.h diff --git a/thu_dsa/chp8/BTree.h b/数据结构/chp8/BTree.h similarity index 100% rename from thu_dsa/chp8/BTree.h rename to 数据结构/chp8/BTree.h diff --git a/thu_dsa/chp8/RB-tree.md b/数据结构/chp8/RB-tree.md similarity index 100% rename from thu_dsa/chp8/RB-tree.md rename to 数据结构/chp8/RB-tree.md diff --git a/数据结构/chp8/README.md b/数据结构/chp8/README.md new file mode 100644 index 0000000..17a0f77 --- /dev/null +++ b/数据结构/chp8/README.md @@ -0,0 +1,11 @@ +知识总结:二叉搜索树 +================= + +本章和上一章讨论了几个常见到的平衡二叉搜索树,在本章中是主要讨论了伸展树,B 树以及红黑树。它们的知识总结如下: + ++ [伸展树](SplayTree.md) ++ [B 树](B-tree.md) ++ [红黑树](RB-tree.md) ++ [关于几个平衡搜索树的深入探讨](../chp7/关于几个平衡搜索树的深入探讨.md) + +另外,我在 [这篇文章](../chp7/README.md) 中梳理了各个平衡二叉搜索树的知识脉络。 diff --git a/thu_dsa/chp8/RedBlack.h b/数据结构/chp8/RedBlack.h similarity index 100% rename from thu_dsa/chp8/RedBlack.h rename to 数据结构/chp8/RedBlack.h diff --git a/thu_dsa/chp8/SplayTree.h b/数据结构/chp8/SplayTree.h similarity index 100% rename from thu_dsa/chp8/SplayTree.h rename to 数据结构/chp8/SplayTree.h diff --git a/thu_dsa/chp8/SplayTree.md b/数据结构/chp8/SplayTree.md similarity index 100% rename from thu_dsa/chp8/SplayTree.md rename to 数据结构/chp8/SplayTree.md diff --git a/thu_dsa/chp8/images/Btree_and_BST.png b/数据结构/chp8/images/Btree_and_BST.png similarity index 100% rename from thu_dsa/chp8/images/Btree_and_BST.png rename to 数据结构/chp8/images/Btree_and_BST.png diff --git a/thu_dsa/chp8/images/Btree_merge.png b/数据结构/chp8/images/Btree_merge.png similarity index 100% rename from thu_dsa/chp8/images/Btree_merge.png rename to 数据结构/chp8/images/Btree_merge.png diff --git a/thu_dsa/chp8/images/Btree_rotation.png b/数据结构/chp8/images/Btree_rotation.png similarity index 100% rename from thu_dsa/chp8/images/Btree_rotation.png rename to 数据结构/chp8/images/Btree_rotation.png diff --git a/thu_dsa/chp8/images/Btree_split.png b/数据结构/chp8/images/Btree_split.png similarity index 100% rename from thu_dsa/chp8/images/Btree_split.png rename to 数据结构/chp8/images/Btree_split.png diff --git a/thu_dsa/chp8/images/SplayTreeInsert.png b/数据结构/chp8/images/SplayTreeInsert.png similarity index 100% rename from thu_dsa/chp8/images/SplayTreeInsert.png rename to 数据结构/chp8/images/SplayTreeInsert.png diff --git a/thu_dsa/chp8/images/SplayTreeRemove.png b/数据结构/chp8/images/SplayTreeRemove.png similarity index 100% rename from thu_dsa/chp8/images/SplayTreeRemove.png rename to 数据结构/chp8/images/SplayTreeRemove.png diff --git a/thu_dsa/chp8/images/one-step-splay.png b/数据结构/chp8/images/one-step-splay.png similarity index 100% rename from thu_dsa/chp8/images/one-step-splay.png rename to 数据结构/chp8/images/one-step-splay.png diff --git a/thu_dsa/chp8/images/onestep_versus_twostep.png b/数据结构/chp8/images/onestep_versus_twostep.png similarity index 100% rename from thu_dsa/chp8/images/onestep_versus_twostep.png rename to 数据结构/chp8/images/onestep_versus_twostep.png diff --git a/thu_dsa/chp8/test_BTree.cpp b/数据结构/chp8/test_BTree.cpp similarity index 100% rename from thu_dsa/chp8/test_BTree.cpp rename to 数据结构/chp8/test_BTree.cpp diff --git a/thu_dsa/chp8/test_RedBlack.cpp b/数据结构/chp8/test_RedBlack.cpp similarity index 100% rename from thu_dsa/chp8/test_RedBlack.cpp rename to 数据结构/chp8/test_RedBlack.cpp diff --git a/thu_dsa/chp8/test_SplayTree.cpp b/数据结构/chp8/test_SplayTree.cpp similarity index 100% rename from thu_dsa/chp8/test_SplayTree.cpp rename to 数据结构/chp8/test_SplayTree.cpp diff --git a/数据结构/chp9/README.md b/数据结构/chp9/README.md new file mode 100644 index 0000000..4975d6b --- /dev/null +++ b/数据结构/chp9/README.md @@ -0,0 +1,7 @@ +知识总结: 跳转表与散列 +===================== + +本章讨论了跳转表和散列,它们的知识总结如下: + ++ [跳转表](skiplist.md) ++ [散列](hash.md) diff --git a/thu_dsa/chp9/dict.h b/数据结构/chp9/dict.h similarity index 100% rename from thu_dsa/chp9/dict.h rename to 数据结构/chp9/dict.h diff --git a/thu_dsa/chp9/hash.md b/数据结构/chp9/hash.md similarity index 100% rename from thu_dsa/chp9/hash.md rename to 数据结构/chp9/hash.md diff --git a/thu_dsa/chp9/quadlist.h b/数据结构/chp9/quadlist.h similarity index 100% rename from thu_dsa/chp9/quadlist.h rename to 数据结构/chp9/quadlist.h diff --git a/thu_dsa/chp9/skiplist.h b/数据结构/chp9/skiplist.h similarity index 100% rename from thu_dsa/chp9/skiplist.h rename to 数据结构/chp9/skiplist.h diff --git a/thu_dsa/chp9/skiplist.md b/数据结构/chp9/skiplist.md similarity index 100% rename from thu_dsa/chp9/skiplist.md rename to 数据结构/chp9/skiplist.md diff --git a/thu_dsa/chp9/skiplist.png b/数据结构/chp9/skiplist.png similarity index 100% rename from thu_dsa/chp9/skiplist.png rename to 数据结构/chp9/skiplist.png diff --git a/thu_dsa/chp9/test_skiplist.cpp b/数据结构/chp9/test_skiplist.cpp similarity index 100% rename from thu_dsa/chp9/test_skiplist.cpp rename to 数据结构/chp9/test_skiplist.cpp diff --git a/cod/chp1.md b/计算机组成与设计/chp1.md similarity index 100% rename from cod/chp1.md rename to 计算机组成与设计/chp1.md diff --git a/cod/chp2.md b/计算机组成与设计/chp2.md similarity index 100% rename from cod/chp2.md rename to 计算机组成与设计/chp2.md diff --git a/cod/single_cycle.png b/计算机组成与设计/single_cycle.png similarity index 100% rename from cod/single_cycle.png rename to 计算机组成与设计/single_cycle.png diff --git a/network/IP layer.md b/计算机网络/IP layer.md similarity index 100% rename from network/IP layer.md rename to 计算机网络/IP layer.md diff --git a/network/Transport Layer.md b/计算机网络/Transport Layer.md similarity index 100% rename from network/Transport Layer.md rename to 计算机网络/Transport Layer.md diff --git a/network/application layer.md b/计算机网络/application layer.md similarity index 100% rename from network/application layer.md rename to 计算机网络/application layer.md diff --git a/network/conclusion.md b/计算机网络/conclusion.md similarity index 100% rename from network/conclusion.md rename to 计算机网络/conclusion.md diff --git a/network/phy_dataLink.md b/计算机网络/phy_dataLink.md similarity index 100% rename from network/phy_dataLink.md rename to 计算机网络/phy_dataLink.md