update directory & add some src codes

This commit is contained in:
Shine wOng
2019-05-08 09:49:12 +08:00
parent a3b96076d2
commit fae4437d18
20 changed files with 10343 additions and 0 deletions

BIN
thu_dsa/chp1/fib/fib Normal file

Binary file not shown.

62
thu_dsa/chp1/fib/fib.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include <iostream>
#include <stdio.h>
#include <time.h>
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;
}

12
thu_dsa/chp1/lcs/Makefile Normal file
View File

@@ -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!"

31
thu_dsa/chp1/lcs/lcs.cpp Normal file
View File

@@ -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<vector<int>> lens(len1 + 1, vector<int>(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];
}

8
thu_dsa/chp1/lcs/lcs.h Normal file
View File

@@ -0,0 +1,8 @@
#include <string>
#include <vector>
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
using namespace std;
int lcsIt(string one, string two);
int lcsRe(string one, string two);

BIN
thu_dsa/chp1/lcs/lcs.o Normal file

Binary file not shown.

BIN
thu_dsa/chp1/lcs/lcstest Normal file

Binary file not shown.

View File

@@ -0,0 +1,43 @@
#include <iostream>
#include <cassert>
#include "lcs.h"
#include <time.h>
#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;
}

BIN
thu_dsa/chp1/lcs/test_lcs.o Normal file

Binary file not shown.

View File

@@ -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!"

View File

View File

@@ -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()

View File

@@ -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);
}
}

11
thu_dsa/chp1/max2/max2.h Normal file
View File

@@ -0,0 +1,11 @@
#include <iostream>
#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);

BIN
thu_dsa/chp1/max2/max2.o Normal file

Binary file not shown.

BIN
thu_dsa/chp1/max2/max2test Normal file

Binary file not shown.

View File

@@ -0,0 +1,78 @@
#include "max2.h"
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#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 <NUMOFLOOP; ++ix)
max2Re(A, 0, LEN, one, two);
end = clock();
assert(one == 9999);
assert(two == 9999);
cout << "max2Re test passed." << endl;
cout << "Running time: " << end - begin << endl;
}
void max2ItTest(){
int one, two;
clock_t begin, end;
begin = clock();
for(int ix = 0; ix != NUMOFLOOP; ++ix)
max2It(A, 0, LEN, one, two);
end = clock();
assert(one == 9999);
assert(two == 9999);
printf("max2It test passed.\n");
cout << "Running time: " << end - begin << endl;
}
void max2It2Test(){
int one, two;
clock_t begin, end;
begin = clock();
for(int ix = 0; ix != NUMOFLOOP; ++ix)
max2It2(A, 0, LEN, one, two);
end = clock();
assert(one == 9999);
assert(two == 9999);
cout << "max2It2 test passed." << endl;
cout << "Running time: " << end - begin << endl;
}

Binary file not shown.

10001
thu_dsa/chp1/max2/testData.txt Normal file

File diff suppressed because it is too large Load Diff