├── 8.数塔问题 ├── readme.md └── shuta.c ├── 6.最优二叉检索树 └── OBST.cpp ├── 5.最大子段和问题 ├── readme.txt ├── maxInterval_DC.cpp └── maxInterval_DP.c ├── 4.简单的社交关系映射 ├── readme.txt └── friend.cpp ├── README.md ├── 7.最长子序列 ├── LIS.c └── LCS.c ├── 2.找K大的数 └── find_k.cpp ├── 3.字典排序 └── dicsort.cpp ├── 1.KMP └── KMP.cpp └── .gitignore /8.数塔问题/readme.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dskyu/Algorithm/HEAD/8.数塔问题/readme.md -------------------------------------------------------------------------------- /6.最优二叉检索树/OBST.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dskyu/Algorithm/HEAD/6.最优二叉检索树/OBST.cpp -------------------------------------------------------------------------------- /5.最大子段和问题/readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dskyu/Algorithm/HEAD/5.最大子段和问题/readme.txt -------------------------------------------------------------------------------- /4.简单的社交关系映射/readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dskyu/Algorithm/HEAD/4.简单的社交关系映射/readme.txt -------------------------------------------------------------------------------- /5.最大子段和问题/maxInterval_DC.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dskyu/Algorithm/HEAD/5.最大子段和问题/maxInterval_DC.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Algorithm 2 | ========= 3 | 4 | 算法练习仓库 5 | 6 | 索引 7 | 1.KMP
8 | 2.找K大的数
9 | 3.字典排序
10 | 4.简单的社交关系映射
11 | 5.最大子段和问题
12 | 6.最优二叉检索树
13 | 7.最长子序列 LCS LIS
14 | 8.数塔问题
-------------------------------------------------------------------------------- /5.最大子段和问题/maxInterval_DP.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int maxInterval(int *arr, int left, int right) 5 | { 6 | int i,sum = 0; 7 | int tmp = 0; 8 | for(i=left; i 0) {tmp += arr[i];} 11 | else {tmp = arr[i];} 12 | if(tmp >sum){sum = tmp;} 13 | } 14 | return sum; 15 | 16 | } 17 | 18 | int main() 19 | { 20 | int a[]={-2, 11, -4, 13, -5, -2}; 21 | printf("%d",maxInterval(a,0,5)); 22 | system("pause"); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /7.最长子序列/LIS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int LIS(int a[],int la) 5 | { 6 | int max,i,j; 7 | int s[100]={0}; 8 | 9 | s[0]=1; 10 | for (i=1;i 2 | #include 3 | 4 | int LCS(char a[],char b[]) 5 | { 6 | int i,j; 7 | int f[100][100]; 8 | int la=strlen(a),lb=strlen(b); 9 | 10 | for (i=0;i<=la;i++) 11 | for (j=0;j<=lb;j++) 12 | { 13 | if (i==0 || j==0) f[i][j]=0; 14 | else if (i>0 && j>0 && a[i]!=b[j]) 15 | f[i][j]=f[i-1][j]>f[i][j-1]?f[i-1][j]:f[i][j-1]; 16 | else if (i>0 && j>0 && a[i]==b[j]) 17 | f[i][j]=f[i-1][j-1]+1; 18 | } 19 | 20 | return f[la][lb]; 21 | } 22 | 23 | int main() 24 | { 25 | char a[]="1564137"; 26 | char b[]="1168347"; 27 | printf ("%d",LCS(a,b)); 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /8.数塔问题/shuta.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int shuta(int val[5][5]) 5 | { 6 | int i,j; 7 | int f[100][100]; 8 | int N = 5; 9 | 10 | for (i=0;i=0;i--) 13 | for (j=0;j<=i;j++) 14 | f[i][j]=(f[i+1][j]>f[i+1][j+1]?f[i+1][j]:f[i+1][j+1]) + val[i][j]; 15 | 16 | for (i=0;i 2 | #include 3 | int findNo_k(int a[],int low,int high,int k) 4 | { 5 | int i=low; 6 | int j=high; 7 | int temp=a[low]; 8 | 9 | while (i= temp) j--; 12 | if (ik-1) findNo_k(a,low,i-1,k); 21 | 22 | 23 | 24 | // if (lowi) findNo_k(a,i+1,high,k); 26 | } 27 | 28 | int main() 29 | { 30 | int a[] = {32,543,5,43,65,43,2,42,47,41}; 31 | int i; 32 | printf("%d \n",findNo_k(a,0,9,1)); 33 | for (i = 0;i<10;i++) 34 | printf("%d ",a[i]); 35 | 36 | system("pause"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /3.字典排序/dicsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int next_permutation(string &str) { 6 | int len = str.size(); 7 | int index = -1; 8 | for (int i = len - 2;i > 0;--i) { 9 | if (str[i] < str[i + 1]) { 10 | index = i; 11 | break; 12 | } 13 | } 14 | 15 | if (index == -1) return 0; 16 | 17 | char ch = 'z'+1; 18 | 19 | int num = -1; 20 | for (int i = index + 1;i < len ;++i) { 21 | if (str[i] <= str[index]) continue; 22 | 23 | if (str[i] < 'z') { 24 | num = i; 25 | ch = str[i]; 26 | } 27 | } 28 | 29 | if (num != -1) { 30 | str[num] = str[index]; 31 | str[index] = ch; 32 | } 33 | 34 | 35 | for (int i = index + 1;i < len ;++i) { 36 | ch = str[i]; 37 | str[i] = str[len-i+index]; 38 | str[len-i+index] = ch; 39 | } 40 | return 1; 41 | } 42 | 43 | int main() 44 | { 45 | string input = "3754"; 46 | 47 | cout << input; 48 | next_permutation(input); 49 | cout << endl << input; 50 | cin >> input; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /1.KMP/KMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | inline void get_next(string T,vector &next) 8 | { 9 | next[0] = -1; 10 | for(int i=1;i=0 ) j=next[j]; 13 | if(T[i]==T[j+1])next[i]=j+1; 14 | else next[i]=0; 15 | } 16 | }; 17 | 18 | inline string::size_type KMP(const string& S,const string& T) 19 | { 20 | vector next(T.size()); 21 | get_next(T,next); 22 | 23 | string::size_type index,count=0; 24 | for(index=0;index 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | #define MAX_VERTEX_NUM 100 10 | 11 | 12 | class User { 13 | static unsigned id_auto; 14 | public : 15 | unsigned id; 16 | string username; 17 | string password; 18 | string duty; 19 | User(string &u,string &p,string &d); 20 | void displsy(); 21 | }; 22 | unsigned User::id_auto = 0; 23 | 24 | User::User(string &u, string &p, string &d){ 25 | if (u.length() == 0) throw runtime_error("u length error"); 26 | if (p.length() == 0) throw runtime_error("p length error"); 27 | if (d.length() == 0) throw runtime_error("d length error"); 28 | username = u; 29 | password = p; 30 | duty = d; 31 | id = ++id_auto; 32 | } 33 | 34 | 35 | void User::displsy() { 36 | cout << "id=" << id<<" username="<> buffer; 121 | if(!userIsExist(buffer)) { 122 | cout << buffer << " isn't exist!" << endl; 123 | show_menu(); 124 | return 0; 125 | } 126 | User *user = getUserByUsername(buffer); 127 | 128 | Again: 129 | cout << "Password:"; 130 | fflush(stdin); 131 | cin >> buffer; 132 | if (user->password != buffer) 133 | { 134 | cout << "Access Denied" << endl; 135 | goto Again; 136 | } 137 | currentUser = user; 138 | return 1; 139 | } 140 | 141 | int Operations::Register() { 142 | string username; 143 | string password; 144 | string cpassword; 145 | string duty; 146 | uAgain: 147 | cout << "Enter Username:"; 148 | fflush(stdin); 149 | cin >> username; 150 | if(userIsExist(username)) { 151 | cout << username << " has existed!" << endl; 152 | goto uAgain; 153 | } 154 | cout << "Enter Password:"; 155 | fflush(stdin); 156 | cin >> password; 157 | cAgain: 158 | cout << "Confirm Password:"; 159 | fflush(stdin); 160 | cin >> cpassword; 161 | if (password != cpassword) { 162 | cout << "error - not the same!" << endl; 163 | goto cAgain; 164 | } 165 | cout << "Enter Duty:"; 166 | fflush(stdin); 167 | cin >> duty; 168 | 169 | User *user = new User(username,password,duty); 170 | g->AddVex(user); 171 | return 1; 172 | } 173 | 174 | void Operations::Logout() { 175 | currentUser = NULL; 176 | } 177 | 178 | void Operations::makeFriend() { 179 | int id; 180 | cout << "ID of your friend who you want to make:"; 181 | fflush (stdin); 182 | cin >> id; 183 | if (currentUser->id == id) { 184 | cout << "Never kiss your own ass!" << endl; 185 | return ; 186 | } 187 | if (userIsMyFriendByID(id)) { 188 | cout << "This guy had been your friend yet!" << endl; 189 | return ; 190 | } 191 | g->AddArc(currentUser->id-1,id-1); 192 | cout << "OK,you are friend now!" << endl; 193 | } 194 | void Operations::showPeople() { 195 | g->display(); 196 | } 197 | void Operations::myFriend() { 198 | int id = currentUser->id; 199 | for (int i = 0;i < g->vexnum;i++) 200 | { 201 | if(g->arcs[id-1][i]) { 202 | g->vexs[i]->displsy(); 203 | } 204 | } 205 | } 206 | 207 | int Operations::userIsExist(string &user) { 208 | for (vector::iterator iter = g->vexs.begin();iter != g->vexs.end();++iter){ 209 | if (user == (*iter)->username) return 1; 210 | } 211 | return 0; 212 | } 213 | 214 | int Operations::userIsMyFriendByID(int id){ 215 | int myid = currentUser->id; 216 | return g->arcs[myid-1][id-1]; 217 | } 218 | 219 | User *Operations::getUserByUsername(string &user) { 220 | for (vector::iterator iter = g->vexs.begin();iter != g->vexs.end();++iter){ 221 | if (user == (*iter)->username) return *iter; 222 | } 223 | return NULL; 224 | } 225 | 226 | void Operations::show_menu(){ 227 | cout << "======================================================" << endl; 228 | cout << "== 1.Login 2.Register 3.Exit ==" << endl; 229 | cout << "======================================================" << endl; 230 | } 231 | 232 | void Operations::show_operations() { 233 | cout << "=====================================================================" << endl; 234 | cout << "== 1.My friend 2.Show people 3.Make friend 4.Logout 5.Exit ==" << endl; 235 | cout << "=====================================================================" << endl; 236 | } 237 | 238 | int main() 239 | { 240 | AdjMatrix G; 241 | User *user[5]; 242 | try { 243 | user[0] = new User(string("Obama"),string("111"),string("USA Present")); 244 | user[1] = new User(string("HuGe"),string("222"),string("China Chairman")); 245 | user[2] = new User(string("PuJing"),string("333"),string("Russia Present")); 246 | user[3] = new User(string("Hitler"),string("444"),string("Genmany Present")); 247 | user[4] = new User(string("LinKen"),string("555"),string("USA Old Present")); 248 | }catch(runtime_error e){ 249 | cout << e.what() << endl; 250 | } 251 | 252 | for (int i = 0;i < 5;i++) 253 | { 254 | G.AddVex(user[i]); 255 | } 256 | G.AddArc(0,1); 257 | G.AddArc(0,2); 258 | G.AddArc(0,4); 259 | G.AddArc(1,3); 260 | G.AddArc(2,3); 261 | 262 | 263 | Operations op(&G); 264 | 265 | string buf = ""; 266 | int ch; 267 | while(1) { 268 | if (buf.length() && op.currentUser) { 269 | op.show_operations(); 270 | cout << buf; 271 | cin >> ch; 272 | switch (ch) 273 | { 274 | case 1: 275 | op.myFriend(); 276 | break; 277 | case 2: 278 | op.showPeople(); 279 | break; 280 | case 3: 281 | op.makeFriend(); 282 | break; 283 | case 4: 284 | op.Logout(); 285 | buf = ""; 286 | break; 287 | case 5: 288 | exit(0); 289 | break; 290 | default: 291 | break; 292 | } 293 | } 294 | else { 295 | op.show_menu(); 296 | cout << "Not Logged In:"; 297 | cin >> ch; 298 | switch (ch) 299 | { 300 | case 1: 301 | if(op.Login()) buf = op.currentUser->username+":"; 302 | break; 303 | case 2: 304 | op.Register(); 305 | break; 306 | case 3: 307 | exit(0); 308 | break; 309 | default: 310 | break; 311 | } 312 | } 313 | } 314 | system("pause"); 315 | return 0; 316 | } 317 | --------------------------------------------------------------------------------