Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A no of changes added from different forks #23

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/acm-cheat-sheet.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions C++/abstract.tex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ \subsubsection{内容简介}

全书的代码,使用“纯C + STL”的风格。本书中的代码规范,跟在公司中的工程规范略有不同,为了使代码短(方便迅速实现):


test file
\begindot
\item 所有代码都是单一文件。这是因为一般OJ网站,提交代码的时候只有一个文本框,如果还是
按照标准做法,比如分为头文件.h和源代码.cpp,无法在网站上提交;
Expand Down
Binary file modified C++/diagrams/Tower-of-Hanoi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bigintmul1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bigintmul2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bigintmul3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bigintmul4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bigintmul5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/blackwhite-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/bubblesort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/chessboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/criticalpath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/dijkstra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/eight-digits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/eight-queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/floyd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/gallery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/horse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/inorder-morris-traversal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/kruskal1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/kruskal2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/lplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/maze.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/mergesort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/next-permutation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/numbers-triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/postorder-morris-traversal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified C++/diagrams/preorder-morris-traversal.png
Binary file modified C++/diagrams/prim1.png
Binary file modified C++/diagrams/prim2.png
Binary file modified C++/diagrams/prim3.png
Binary file modified C++/diagrams/radixsort.png
Binary file modified C++/diagrams/radixsorta.png
Binary file modified C++/diagrams/radixsortb.png
Binary file modified C++/diagrams/radixsortc.png
Binary file modified C++/diagrams/radixsortd.png
Binary file modified C++/diagrams/radixsorte.png
Binary file modified C++/diagrams/radixsortf.png
Binary file modified C++/diagrams/radixsortg.png
Binary file modified C++/diagrams/river.png
Binary file modified C++/diagrams/roundrobin.png
Binary file modified C++/diagrams/shellsort.png
Binary file modified C++/diagrams/threaded-binary-tree.png
Binary file modified C++/diagrams/ufs1.png
Binary file modified C++/diagrams/ufs2.png
Binary file modified C++/diagrams/ufs3.png
Binary file modified C++/diagrams/uva10054.png
89 changes: 89 additions & 0 deletions C++/src/maxNetProfit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include<iostream>
using namespace std;
/*
Author: Jiankai Sun
Date: 2014.10.15
*/
/*
N jobs are to be scheduled for processing on one machine. Job i, 1 ≤ i ≤ N,
needs ti units of processing time. If job i is finished by time T, where T is a given
deadline, then a profit pi is earned; otherwise, a penalty qi
is imposed. (Both pi and qi are positive integers.) We want to select a subset S of jobs such that
(i) ∑i∈S ti ≤ T, and (ii) f(S) = ∑i∈S pi −∑i /∈S qi is maximum.
Show how to find such a set of jobs using dynamic programming.
*/
int const N=3;
int const T=6;
//profit
int p[N]={12,3,4};
//penalty
int q[N]={1,4,2};
//time consuming
int t[N]={1,4,3};
//net profit table
int f[N+1][T+1];
//pointer to index which job should be scheduled
int P[N+1][T+1];

void findMaxNetProfit(){
//non recursive implementation
for(int i=0;i<N;i++){
for(int j=0;j<T;j++){
//k is the job index; from 1 to N
int k=i+1;
// j is the time unit; form 1 to T
int l=j+1;
if(t[i]>l){
f[k][l]=f[k-1][l]-q[i];
}
else{
f[k][l]=max(f[k-1][l]-q[i],f[k-1][l-t[i]]+p[i]);
if(f[k][l]==f[k-1][l-t[i]]+p[i]){
P[k][l]=1;
}
}
}
}
cout<<"net profit is "<<f[N][T]<<endl;
}
int findMaxNetProfit_rec(int job_n,int deadline){
//recursive implementation
if(job_n==0){
//boundary condition
return 0;
}
if(t[job_n-1]>deadline){
return findMaxNetProfit_rec(job_n-1,deadline)-q[job_n-1];
}
else{
int a=findMaxNetProfit_rec(job_n-1,deadline)-q[job_n-1];
int b=findMaxNetProfit_rec(job_n-1,deadline-t[job_n-1])+p[job_n-1];
if(b>a)
P[job_n][deadline]=1;
return max(a,b);
}

}
void printJobSet(){
int l=T;
for(int i=N;i>=1;i--){
if(P[i][l]==1){
cout<<"job "<<i<<" is in job set"<<endl;
l=l-t[i-1];
}
}
}
int main(){

for(int i=0;i<i+1;i++)
f[0][0]=0;
for(int i=0;i<N;i++){
for(int j=0;j<T;j++){
P[i][j]=0;
}
}
//findMaxNetProfit();
cout<<findMaxNetProfit_rec(N,T)<<endl;
printJobSet();
return 0;
}
76 changes: 76 additions & 0 deletions C++/src/max_selected_intervals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Author: Jiankai Sun
Date: 2014.10.21
*/
#include<iostream>
using namespace std;
//find the largest j which makes f[j]<=s[i]
int find_opt(double [], double [],int);
//return the maximum sum of the selected intervals
//non recurssive version
double max_select_nonrec(double [],double [],int);
//recurssive version
double max_select_rec(double [],double [],int);
//void print selected intervals
void print_interval(int *,int);

int find_opt(double s[],double f[],int i){
for(int j=i-1;j>=0;j--){
if(f[j]<=s[i]){
return j;
}
}
return 0;
}

double max_select_nonrec(double s[], double f[],int n){
double * T=new double[n];
int * P=new int[n];
for(int i=0;i<n;i++){
T[i]=0;
}
for(int i=1;i<n;i++){
int j=find_opt(s,f,i);
double temp=T[j]+f[i]-s[i];
if(temp>=T[i-1]){
T[i]=temp;
P[i]=j;
}
else{
T[i]=T[i-1];
P[i]=-1;
}

}
//print
print_interval(P,n);
return T[n-1];
}
void print_interval(int * P, int n){
int i=n-1;
while(P[i]!=0){
if(P[i]>0){
cout<<i<<" ";
i=P[i];
}
}
//print the last one
cout<<i<<endl;
}

double max_select_rec(double s[],double f[], int i){

if(i<=0)
return 0;
else
return max(max_select_rec(s,f,find_opt(s,f,i))+f[i]-s[i],max_select_rec(s,f,i-1));
}

int main(){
int n=7;
double s[]={0,1,5,6,7,15,1};
double f[]={0,4,10,12,14,20,21};
cout<<max_select_rec(s,f,n-1)<<endl;
cout<<max_select_nonrec(s,f,n)<<endl;
return 0;
}
54 changes: 54 additions & 0 deletions C++/src/min_operation_dp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<iostream>
using namespace std;
/*
Author: Jiankai Sun
Date: 2014/10/14
*/
/*
Let A=a1a2...am and B=b1b2...bn be two strings of characers. we
want to transform A into B using following operations:
delete a character
and a character
change a character
write a dynamic programming algorithm that finds the minimum number
of operations needed to transform A into B
*/
int min_ope(char * c1,const int m, char *c2,const int n){

int F[m+1][n+1];//define operation matrix

//boundary condition
for(int i=0;i<=m;i++){
F[i][0]=i;
}
for(int i=0;i<=n;i++){
F[0][i]=i;
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
//the index of F starts from [1,1] to store c1[0] and c2[0]
int k=i+1;
int l=j+1;
if(c1[i]==c2[j])
//equal, don't need to change
F[k][l]=F[k-1][l-1];
else{
// add, delete or change operation
F[k][l]=min(min(F[k-1][l-1]+1,F[k-1][l]+1),F[k][l-1]+1);
}

}
}
return F[m][n];
}

int main(){
int const m=2;
int const n=1;
char c1[m]={'a','c'};
char c2[n]={'b'};

cout<<min_ope(c1,m,c2,n)<<endl;

return 0;
}
Binary file added C++/src/problems.pdf
Binary file not shown.
Loading