1.求最长公共子串
#pragma once #pragma once #include <random> #include <iostream> #include <tchar.h> #include <bitset> #include <cassert> #include <crtdbg.h> using namespace std; //----------------------------------------------- //a:97 z:122 A:65 Z:90 //生成随机字符串 //----------------------------------------------- void CreateStr(string& str,int len) { random_device rv; char* buffer = new char(len); assert(sizeof(buffer) > 0); for (int i = 0; i < len; ++i) { int ascii = 0; if (rv() % 2) { ascii = rv() % 25 + 97; } else { //大写 ascii = rv() % 25 + 65; } buffer[i] = ascii; } buffer[len] = '\0'; str = buffer; cout<<"随机生成字符串:"<<endl; cout<<str<<endl; } void LCS(string stra,string strb,int lena,int lenb) { string str; int** array; array = new int*[lena]; for (int i = 0; i < lena; ++i) { array[i] = new int[lenb]; } array[0][0] = 0; for (int i = 0; i < lena; ++i) { array[i][0] = 0; } for (int j = 0; j < lenb; ++j) { array[0][j] = 0; } for (int i = 1; i < lena; ++i) { for (int j = 1; j < lenb; ++j) { if (stra[i] == strb[j]) { //str += stra[i]; array[i][j] = array[i-1][j-1] + 1; if (str.length() < array[i][j]) { str += stra[i]; } } else { array[i][j] = (array[i][j-1] > array[i-1][j]) ? array[i][j-1] : array[i-1][j]; } } } cout<<str<<endl; cout<<array[lena-1][lenb-1]<<endl; for (int i = 0; i < lena; ++i) { delete [] array[i]; } delete [] array; } int _tmain(int argc,TCHAR* argv[]) { do { int in = 0; cin>>in; if (!in) { break; } string strA; string strB; CreateStr(strA,20); CreateStr(strB,14); LCS(strA,strB,20,14); } while (true); return 0; }
2.管子切割问题
3.矩阵乘法
4.背包问题