diff --git a/thu_dsa/chp1.md b/thu_dsa/chp1/chp1.md similarity index 100% rename from thu_dsa/chp1.md rename to thu_dsa/chp1/chp1.md diff --git a/thu_dsa/chp1/fib/fib b/thu_dsa/chp1/fib/fib new file mode 100644 index 0000000..82979f2 Binary files /dev/null and b/thu_dsa/chp1/fib/fib differ diff --git a/thu_dsa/chp1/fib/fib.cpp b/thu_dsa/chp1/fib/fib.cpp new file mode 100644 index 0000000..c269e5e --- /dev/null +++ b/thu_dsa/chp1/fib/fib.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +typedef long long __int64; + +__int64 fib_re1(int n); +__int64 fib_re2(int n); +__int64 fib_it(int n); + +int main(int argc, char** argv){ + int n = 48; + + printf("iterative version: --------------------------\n"); + for( int ix = 0; ix <= n; ix++){ + printf("#%d: %lld\n", ix, fib_it(ix)); + } + + printf("recursive version two: ----------------------\n"); + for(int ix = 0; ix <= n; ix++){ + printf("#%d: %lld\n", ix, fib_re2(ix)); + } + + printf("recursive version :--------------------------\n"); + for( int ix = 0; ix <= n; ix++){ + printf("#%d: %lld\n", ix, fib_re1(ix)); + } + return 0; +} + +__int64 fib_re1(int n){ + if( n == 0 || n == 1 ) return 1; + return fib_re1(n - 1) + fib_re1(n - 2); +} + +__int64 fib_re2(int n, __int64 &prev){ + __int64 prevPrev; + if( n == 0){ + prev = 0; + return 1; + } + prev = fib_re2(n - 1, prevPrev); + return prev + prevPrev; +} + +__int64 fib_re2(int n){ + __int64 prev; + return fib_re2(n, prev); +} + +__int64 fib_it(int n){ + __int64 prev = 1, prevPrev = 0; + __int64 res = 0; + for(int ix = 0; ix <= n; ++ix){ + res = prevPrev + prev; + prevPrev = prev; + prev = res; + } + return res; +} + diff --git a/thu_dsa/chp1/lcs/Makefile b/thu_dsa/chp1/lcs/Makefile new file mode 100644 index 0000000..571a157 --- /dev/null +++ b/thu_dsa/chp1/lcs/Makefile @@ -0,0 +1,12 @@ +lcstest: lcs.o test_lcs.o + g++ lcs.o test_lcs.o -o lcstest + +lcs.o: lcs.cpp lcs.h + g++ -std=c++11 -c lcs.cpp + +test_lcs.o: test_lcs.cpp lcs.cpp + g++ -c test_lcs.cpp + +clean: + rm -rf *.o lcstest + @echo "Clean Done!" diff --git a/thu_dsa/chp1/lcs/lcs.cpp b/thu_dsa/chp1/lcs/lcs.cpp new file mode 100644 index 0000000..826ab95 --- /dev/null +++ b/thu_dsa/chp1/lcs/lcs.cpp @@ -0,0 +1,31 @@ +#include "lcs.h" + +int lcsRe(string one, string two){ + int len; + if(one.empty() || two.empty()) return 0; + + int len1 = one.size(), len2 = two.size(); + if(one[len1 - 1] == two[len2 - 1]){ + len = lcsRe(one.substr(0, len1 - 1), two.substr(0, len2 - 1)) + 1; + return len; + } + len = MAX(lcsRe(one.substr(0, len1 - 1), two), lcsRe(one, two.substr(0, len2 - 1))); + return len; +} + +int lcsIt(string one, string two){ + if(one.empty() || two.empty()) return 0; + + int len1 = one.size(), len2 = two.size(); + vector> lens(len1 + 1, vector(len2 + 1, 0)); + for(int ix = 0; ix != len1; ++ix){ + for(int jx = 0; jx != len2; ++jx){ + if(one[ix] == two[jx]) + lens[ix + 1][jx + 1] = lens[ix][jx] + 1; + else + lens[ix+1][jx+1] = MAX(lens[ix][jx+1], lens[ix+1][jx]); + } + } + return lens[len1][len2]; +} + diff --git a/thu_dsa/chp1/lcs/lcs.h b/thu_dsa/chp1/lcs/lcs.h new file mode 100644 index 0000000..5c71655 --- /dev/null +++ b/thu_dsa/chp1/lcs/lcs.h @@ -0,0 +1,8 @@ +#include +#include +#define MAX(X,Y) ((X)>(Y)?(X):(Y)) +using namespace std; + +int lcsIt(string one, string two); +int lcsRe(string one, string two); + diff --git a/thu_dsa/chp1/lcs/lcs.o b/thu_dsa/chp1/lcs/lcs.o new file mode 100644 index 0000000..886f972 Binary files /dev/null and b/thu_dsa/chp1/lcs/lcs.o differ diff --git a/thu_dsa/chp1/lcs/lcstest b/thu_dsa/chp1/lcs/lcstest new file mode 100644 index 0000000..03ba90f Binary files /dev/null and b/thu_dsa/chp1/lcs/lcstest differ diff --git a/thu_dsa/chp1/lcs/test_lcs.cpp b/thu_dsa/chp1/lcs/test_lcs.cpp new file mode 100644 index 0000000..649d086 --- /dev/null +++ b/thu_dsa/chp1/lcs/test_lcs.cpp @@ -0,0 +1,43 @@ +#include +#include +#include "lcs.h" +#include +#define NUMOFLOOP 10 +using namespace std; + +void test_lcsIt(); +void test_lcsRe(); + +int main(){ + test_lcsIt(); + test_lcsRe(); +} + +void test_lcsIt(){ + clock_t begin, end; + int len; + + begin = clock(); + for(int ix = 0; ix != NUMOFLOOP; ++ix) + len = lcsIt(string("educational"), string("advantage")); + end = clock(); + assert(len == 4); + assert(lcsIt(string("didactical"), string("advantage")) == 4); + cout << "Iterative lcs test passed." << endl; + cout << "Running time: " << end - begin << endl; +} + +void test_lcsRe(){ + clock_t begin, end; + int len; + + begin = clock(); + for(int ix = 0; ix != NUMOFLOOP; ++ix) + len = lcsRe(string("educational"), string("advantage")); + end = clock(); + assert(len == 4); + assert(lcsRe(string("didactical"), string("advantager")) == 4); + cout << "Recursive lcs test passed." << endl; + cout << "Running time: " << end - begin << endl; +} + diff --git a/thu_dsa/chp1/lcs/test_lcs.o b/thu_dsa/chp1/lcs/test_lcs.o new file mode 100644 index 0000000..cb9b00d Binary files /dev/null and b/thu_dsa/chp1/lcs/test_lcs.o differ diff --git a/thu_dsa/chp1/max2/Makefile b/thu_dsa/chp1/max2/Makefile new file mode 100644 index 0000000..63d82ef --- /dev/null +++ b/thu_dsa/chp1/max2/Makefile @@ -0,0 +1,16 @@ +OBJS = max2.o max2test.o +CC = g++ +CFLAG = -c + +max2test: $(OBJS) + $(CC) $(OBJS) -o max2test + +max2.o: max2.cpp max2.h + $(CC) $(CFLAG) max2.cpp + +max2test.o: max2test.cpp + $(CC) -std=c++11 $(CFLAG) max2test.cpp + +clean: + rm -rf *.o max2test + @echo "Clean done!" diff --git a/thu_dsa/chp1/max2/data.txt b/thu_dsa/chp1/max2/data.txt new file mode 100644 index 0000000..e69de29 diff --git a/thu_dsa/chp1/max2/generateTestData.py b/thu_dsa/chp1/max2/generateTestData.py new file mode 100644 index 0000000..0fd9892 --- /dev/null +++ b/thu_dsa/chp1/max2/generateTestData.py @@ -0,0 +1,10 @@ +from numpy import random + +f = open(r'testData.txt', 'w') + +f.write(str(random.randint(10000))) +for ix in range(10000): + f.write('\n' + str(random.randint(10000))) + +f.close() + diff --git a/thu_dsa/chp1/max2/max2.cpp b/thu_dsa/chp1/max2/max2.cpp new file mode 100644 index 0000000..e781ce9 --- /dev/null +++ b/thu_dsa/chp1/max2/max2.cpp @@ -0,0 +1,71 @@ +#include "max2.h" + +inline void swap(int &one, int&two){ + int tmp = one; + one = two; + two = tmp; +} + +/* + * compute max two elements in A[lo, hi) + * iterative versions + */ +void max2It(int* A, int lo, int hi, int &one, int &two){ + if(hi - lo < 2) return; + one = MAX(A[lo], A[lo + 1]); + two = MIN(A[lo], A[lo + 1]); + for(int ix = lo + 2; ix != hi; ++ix){ + if(A[ix] > two){ + two = A[ix]; + if(two > one) swap(one, two); + } + } +} + +void max2It2(int* A, int lo, int hi, int &one, int &two){ + if(hi - lo < 2) return; + + int maxIndex = lo; + one = A[lo]; + for(int ix = lo + 1; ix != hi; ++ix) + if(A[ix] > one) {one = A[ix];maxIndex = ix;} + + two = 0; + for(int ix = lo; ix != maxIndex; ++ix) + if(A[ix] > two) two = A[ix]; + + int tmp = 0; + for(int ix = maxIndex + 1; ix != hi; ++ix) + if(A[ix] > tmp) tmp = A[ix]; + + two = MAX(two, tmp); +} + +void max2Re(int* A, int lo, int hi, int &one, int &two){ + if(hi - lo == 2){ + if(A[lo] > A[lo + 1]){one = A[lo], two = A[lo + 1];return;} + else {one = A[lo + 1], two = A[lo]; return;} + } + if(hi - lo == 3){ + one = A[lo], two = A[lo + 1]; + if(one < two) swap(one, two); + if(A[lo + 2] > two){ + two = A[lo + 2]; + if(one < two) swap(one, two); + } + return; + } + int mid = (lo + hi) >> 1; + int oneL, twoL, oneR, twoR; + max2Re(A, lo, mid, oneL, twoL); + max2Re(A, mid, hi, oneR, twoR); + if(oneL > oneR){ + one = oneL; + two = MAX(oneR, twoL); + } + else{ + one = oneR; + two = MAX(oneL, twoR); + } +} + diff --git a/thu_dsa/chp1/max2/max2.h b/thu_dsa/chp1/max2/max2.h new file mode 100644 index 0000000..f1d7d70 --- /dev/null +++ b/thu_dsa/chp1/max2/max2.h @@ -0,0 +1,11 @@ +#include + +#define MAX(X,Y) ((X)>(Y)?(X):(Y)) +#define MIN(X,Y) ((X)>(Y)?(Y):(X)) + +using namespace std; + +void max2It(int* A, int lo, int hi, int &one, int &two); +void max2Re(int* A, int lo, int hi, int &one, int &two); +void max2It2(int* A, int lo, int hi, int &one, int &two); + diff --git a/thu_dsa/chp1/max2/max2.o b/thu_dsa/chp1/max2/max2.o new file mode 100644 index 0000000..3d81c69 Binary files /dev/null and b/thu_dsa/chp1/max2/max2.o differ diff --git a/thu_dsa/chp1/max2/max2test b/thu_dsa/chp1/max2/max2test new file mode 100644 index 0000000..0394b98 Binary files /dev/null and b/thu_dsa/chp1/max2/max2test differ diff --git a/thu_dsa/chp1/max2/max2test.cpp b/thu_dsa/chp1/max2/max2test.cpp new file mode 100644 index 0000000..0599762 --- /dev/null +++ b/thu_dsa/chp1/max2/max2test.cpp @@ -0,0 +1,78 @@ +#include "max2.h" +#include +#include +#include +#include +#include +#include +#define LEN 10001 +#define NUMOFLOOP 1000 +using namespace std; + +void max2ItTest(); +void max2It2Test(); +void max2ReTest(); + +int A[LEN]; + +int main(){ + ifstream in("testData.txt"); + char oneline[256]; + string str; + int ix = 0; + while(!in.eof()){ + in.getline(oneline, 100); + str = string(oneline); + if(str.size()) A[ix++] = stoi(str); + } + + max2ItTest(); + max2It2Test(); + max2ReTest(); +} + +void max2ReTest(){ + int one, two; + clock_t begin, end; + + begin = clock(); + for(int ix = 0; ix