├── heap.h ├── BST.in ├── 1.7.in ├── 8.2.in ├── 20.12.in ├── 9.7.in ├── BST ├── 8.6.in ├── 9.6.in ├── 13.1.in ├── 4.2.in ├── 20.11.in ├── README.md ├── 19.10.cpp ├── 7.1.cpp ├── 9.2.cpp ├── 19.4.cpp ├── 19.3.cpp ├── 1.8.cpp ├── 13.6.cpp ├── 5.5.cpp ├── 9.1.cpp ├── 1.2.cpp ├── 20.2.cpp ├── 20.3.cpp ├── 8.5.cpp ├── 19.11.cpp ├── 9.6.cpp ├── 20.1.cpp ├── 9.5.cpp ├── 16.10.cpp ├── 5.6.cpp ├── 13.1.cpp ├── 19.7.cpp ├── 8.8.cpp ├── 9.3.cpp ├── 1.6.cpp ├── 5.2.cpp ├── 20.5.cpp ├── 12.4.cpp ├── 5.1.cpp ├── 1.4.cpp ├── 3.6.cpp ├── 4.2.cpp ├── 12.3.cpp ├── 3.4.cpp ├── 10.7.cpp ├── 4.3.cpp ├── 1.7.cpp ├── 8.4.cpp ├── 9.7.cpp ├── 1.1.cpp ├── 20.12.cpp ├── 5.7.cpp ├── 2.3.cpp ├── 10.4.cpp ├── 2.2.cpp ├── 12.3.1.cpp ├── 20.8.cpp ├── 20.7.cpp ├── hash.h ├── 4.5.cpp ├── 8.6.cpp ├── 8.1.cpp ├── 8.7.cpp ├── 13.9.cpp ├── 2.5.cpp ├── 1.5.cpp ├── 8.3.cpp ├── 4.7.cpp ├── 19.5.cpp ├── 20.11.cpp ├── 2.1.cpp ├── 7.1.h ├── 20.4.cpp ├── 8.2.cpp ├── 19.2.cpp ├── 2.4.cpp ├── 4.4.cpp ├── 4.1.cpp ├── 5.3.cpp ├── 10.6.1.cpp ├── 4.8.cpp ├── 20.9.cpp ├── 10.6.cpp ├── 3.1.cpp ├── 4.6.cpp ├── 3.2.cpp ├── ac_automation.cpp ├── 1.3.cpp ├── 3.3.cpp ├── 3.5.cpp └── BST.cpp /heap.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /BST.in: -------------------------------------------------------------------------------- 1 | 2 4 3 1 9 0 7 6 8 5 2 | -------------------------------------------------------------------------------- /1.7.in: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 3 4 3 | 5 0 7 8 4 | 9 10 11 0 5 | -------------------------------------------------------------------------------- /8.2.in: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 1 1 0 3 | 0 1 1 1 4 | 1 1 1 1 5 | -------------------------------------------------------------------------------- /20.12.in: -------------------------------------------------------------------------------- 1 | 4 2 | -1 0 -2 1 3 | -4 9 3 2 4 | -1 2 3 -1 5 | 0 11 -2 3 6 | -------------------------------------------------------------------------------- /9.7.in: -------------------------------------------------------------------------------- 1 | 6 2 | 65 100 3 | 70 150 4 | 56 90 5 | 75 190 6 | 56 95 7 | 68 110 8 | -------------------------------------------------------------------------------- /BST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hawstein/cracking-the-coding-interview/HEAD/BST -------------------------------------------------------------------------------- /8.6.in: -------------------------------------------------------------------------------- 1 | 5 5 2 | 0 3 3 2 1 3 | 3 0 1 3 3 4 | 3 3 0 2 3 5 | 0 3 3 3 1 6 | 0 1 0 2 1 7 | -------------------------------------------------------------------------------- /9.6.in: -------------------------------------------------------------------------------- 1 | 5 5 2 | 1 2 3 4 5 3 | 3 7 8 9 11 4 | 5 9 10 17 18 5 | 7 12 15 19 23 6 | 9 13 16 20 25 7 | -------------------------------------------------------------------------------- /13.1.in: -------------------------------------------------------------------------------- 1 | one 2 | two 3 | three 3 4 | last 4 lines:begin 5 | the second line 6 | continue 7 | end 8 | -------------------------------------------------------------------------------- /4.2.in: -------------------------------------------------------------------------------- 1 | 8 11 2 | 0 1 3 | 1 0 4 | 1 5 5 | 2 3 6 | 2 4 7 | 4 7 8 | 5 2 9 | 5 7 10 | 6 2 11 | 6 4 12 | 7 4 13 | -------------------------------------------------------------------------------- /20.11.in: -------------------------------------------------------------------------------- 1 | 8 2 | 1 1 1 1 0 1 0 1 3 | 1 0 0 0 0 0 1 1 4 | 1 0 1 1 1 0 1 1 5 | 1 0 0 1 0 0 1 1 6 | 1 0 1 1 1 0 1 0 7 | 1 0 0 0 0 0 0 0 8 | 1 1 1 0 1 0 1 0 9 | 0 1 1 0 0 1 1 1 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Solutions for the book: Cracking the coding interview V4. Written in C++. 2 | 3 | See the detailed solutions: 4 | 5 | 6 | -------------------------------------------------------------------------------- /19.10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Rand5(){ 5 | int x = ~(1<<31); // max int 6 | while(x > 5) 7 | x = Rand7(); 8 | return x; 9 | } 10 | 11 | 12 | 13 | int main(){ 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /7.1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "7.1.h" 3 | using namespace std; 4 | 5 | int main(){ 6 | Deck deck; 7 | deck.shuffle(); 8 | vector vc = deck.deal_hand(5); 9 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool cmp(string s1, string s2){ 6 | sort(&s1[0], &s1[0]+s1.length()); 7 | sort(&s2[0], &s2[0]+s2.length()); 8 | return s1 < s2; 9 | } 10 | int main(){ 11 | string s[] = { 12 | "axyz", "abc", "yzax", "bac", "zyxa", "fg", "gf" 13 | }; 14 | sort(s, s+7, cmp); 15 | for(int i=0; i<7; ++i) 16 | cout< 2 | using namespace std; 3 | 4 | int Max1(int a, int b){ 5 | int c[2] = { 6 | a, b 7 | }; 8 | int z = a - b; 9 | z = (z>>31) & 1; 10 | return c[z]; 11 | } 12 | int Max2(int a, int b){ 13 | int z = a - b; 14 | int k = (z>>31) & 1; 15 | return a - k * z; 16 | } 17 | int main(){ 18 | int a = 5, b = 10; 19 | cout< 2 | using namespace std; 3 | 4 | int NumZeros(int n){ 5 | if(n < 0) return -1; 6 | int num = 0; 7 | while((n /= 5) > 0){ 8 | num += n; 9 | } 10 | return num; 11 | } 12 | int NumZeros1(int n){ 13 | if(n < 0) return -1; 14 | int num = 0; 15 | for(int i = 5; n/i > 0; i *= 5) 16 | num += n/i; 17 | return num; 18 | } 19 | int main(){ 20 | for(int i=1; i<100; ++i){ 21 | if(NumZeros(i) == NumZeros1(i)) 22 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isSubstring(string s1, string s2){ 6 | if(s1.find(s2) != string::npos) return true; 7 | else return false; 8 | } 9 | bool isRotation(string s1, string s2){ 10 | if(s1.length() != s2.length() || s1.length()<=0) 11 | return false; 12 | return isSubstring(s1+s1, s2); 13 | } 14 | 15 | int main(){ 16 | string s1 = "apple"; 17 | string s2 = "pleap"; 18 | cout< 2 | using namespace std; 3 | 4 | class FirstClass { 5 | public: 6 | virtual void MethodA(int) { 7 | cout<<"FirstClass::MethodA(int)"< 2 | using namespace std; 3 | int count_one(int x){ 4 | x = (x & (0x55555555)) + ((x >> 1) & (0x55555555)); 5 | x = (x & (0x33333333)) + ((x >> 2) & (0x33333333)); 6 | x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f)); 7 | x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff)); 8 | x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff)); 9 | return x; 10 | } 11 | 12 | int convert_num(int a, int b){ 13 | return count_one(a^b); 14 | } 15 | int main(){ 16 | int a = 7, b = 14; 17 | cout< 2 | using namespace std; 3 | 4 | void merge(int a[], int b[], int n, int m){ 5 | int k = n + m - 1; 6 | int i = n - 1, j = m - 1; 7 | while(i>=0 && j>=0){ 8 | if(a[i] > b[j]) a[k--] = a[i--]; 9 | else a[k--] = b[j--]; 10 | } 11 | while(j>=0) a[k--] = b[j--]; 12 | } 13 | 14 | int main(){ 15 | int a[15] = { 16 | 1, 3, 7, 8, 9 17 | }; 18 | int b[] = { 19 | 2, 4, 5, 6, 10 20 | }; 21 | int n = 5, m = 5; 22 | merge(a, b, 5, 5); 23 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void swap(char &a, char &b){ 6 | a = a^b; 7 | b = a^b; 8 | a = a^b; 9 | } 10 | 11 | void reverse2(char *s){ 12 | int n = strlen(s); 13 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void Swap(int &a, int &b){// 有可能swap同一变量,不能用异或版本 6 | int t = a; 7 | a = b; 8 | b = t; 9 | } 10 | void RandomShuffle(int a[], int n){ 11 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void Swap(int &a, int &b){// 有可能swap同一变量,不能用异或版本 6 | int t = a; 7 | a = b; 8 | b = t; 9 | } 10 | void PickMRandomly(int a[], int n, int m){ 11 | for(int i=0; i 2 | using namespace std; 3 | 4 | void print_pare(int l, int r, char str[], int cnt){ 5 | if(l<0 || r 0){ 14 | str[cnt] = '('; 15 | print_pare(l-1, r, str, cnt+1); 16 | } 17 | if(r > l){ 18 | str[cnt] = ')'; 19 | print_pare(l, r-1, str, cnt+1); 20 | } 21 | } 22 | } 23 | int main(){ 24 | int cnt = 3; 25 | char str[2*cnt]; 26 | print_pare(cnt, cnt, str, 0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /19.11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void PrintPairSum(int a[], int n, int sum){ 6 | if(a==NULL || n<2) return; 7 | 8 | sort(a, a+n); 9 | int low = 0, high = n-1; 10 | while(low < high){ 11 | if(a[low]+a[high] > sum) 12 | --high; 13 | else if(a[low]+a[high] < sum) 14 | ++low; 15 | else{ 16 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int d[20][20]; 6 | int search(int m, int n, int x){ 7 | int r = 0, c = n-1; 8 | while(r=0){ 9 | if(d[r][c] == x) return (r * n + c); 10 | else if(d[r][c] < x) ++r; 11 | else --c; 12 | } 13 | return -1; 14 | } 15 | int main(){ 16 | freopen("9.6.in", "r", stdin); 17 | int m, n; 18 | cin>>m>>n; 19 | for(int i=0; i>d[i][j]; 22 | 23 | int k = search(m, n, 13); 24 | if(k == -1) cout<<"not found"< 2 | using namespace std; 3 | 4 | int Add1(int a, int b){ 5 | char *c = (char*)a; 6 | return (int)&c[b]; // c+sizeof(char)*b=a + b 7 | } 8 | int Add2(int a, int b){ 9 | if(b == 0) return a; 10 | int sum = a ^ b; // 各位相加,不计进位 11 | int carry = (a & b) << 1; // 记下进位 12 | return Add2(sum, carry); // 求sum和carry的和 13 | } 14 | int Add3(int a, int b){ 15 | while(b != 0){ 16 | int sum = a ^ b; 17 | int carry = (a & b) << 1; 18 | a = sum; 19 | b = carry; 20 | } 21 | return a; 22 | } 23 | int main(){ 24 | int a = 70, b = -134310; 25 | cout< 2 | using namespace std; 3 | 4 | int search(string s[], int low, int high, string x){ 5 | if(x == "") return -1; 6 | while(low <= high){ 7 | int mid = (low+high)>>1; 8 | int t = mid; 9 | while(s[t] == "" && t <= high) ++t; 10 | if(t > high) high = mid - 1; 11 | else{ 12 | if(s[t] == x) return t; 13 | else if(s[t] < x) low = t + 1; 14 | else high = mid - 1; //or t-1, (mid, t)为空字符串 15 | } 16 | } 17 | return -1; 18 | } 19 | int main(){ 20 | string s[13] = { 21 | "at", "", "", "", "ball", "", "", "car", "", "", "dad", "", "" 22 | }; 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int** My2DAlloc(int rows, int cols){ 6 | int **arr = (int**)malloc(rows*sizeof(int*)); 7 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void print_binary(int x){ 6 | string s = ""; 7 | for(int i=0; i<32 && x!=0; ++i, x >>= 1){ 8 | if(x&1) s = "1" + s; 9 | else s = "0" + s; 10 | } 11 | cout<> 1) & 0x55555555); 15 | } 16 | // x & 0xAAAAAAAA得出结果是unsigned,所以最高位为1时,右移1位,最高位是0 17 | int swap_bits1(int x){ 18 | return ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1); 19 | } 20 | int main(){ 21 | int x = 0xFFFFFFFF;//-7665543; 22 | x = x & 0xFFFFFFFF; 23 | cout<<(x & 0xFFFFFFFF)<> 1); 25 | print_binary(x); 26 | print_binary(swap_bits(x)); 27 | print_binary(swap_bits1(x)); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /13.1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void printLastKLines(ifstream &fin, int k){ 6 | string line[k]; 7 | int lines = 0; 8 | string tmp; 9 | while(getline(fin, tmp)){//&& !fin.eof() 10 | line[lines%k] = tmp; 11 | ++lines; 12 | } 13 | // while(!fin.eof()){ 14 | // getline(fin, line[lines%k]); 15 | // ++lines; 16 | // } 17 | int start, cnt; 18 | if(lines < k){ 19 | start = 0; 20 | cnt = lines; 21 | } 22 | else{ 23 | start = lines%k; 24 | cnt = k; 25 | } 26 | for(int i=0; i 2 | using namespace std; 3 | 4 | bool g_Invalid = false; 5 | 6 | int GetMaxSum(int a[], int n){ 7 | if(a==NULL || n<=0){ 8 | g_Invalid = true; 9 | return 0; 10 | } 11 | g_Invalid = false; 12 | 13 | int max_sum = 1<<31; // Min Int 14 | int cur_sum = 0; 15 | for(int i=0; i max_sum) 22 | max_sum = cur_sum; 23 | } 24 | 25 | return max_sum; 26 | } 27 | int main(){ 28 | int a[] = { 29 | -2, -8, 3, -2, 4, -10 30 | }; 31 | int max_sum = GetMaxSum(a, 6); 32 | if(g_Invalid) 33 | cout<<"Invalid Input!"< 2 | using namespace std; 3 | 4 | int c[20], n=8, cnt=0; 5 | void print(){ 6 | for(int i=0; i 2 | using namespace std; 3 | 4 | int search(int a[], int low, int high, int x){ 5 | while(low <= high){ 6 | int mid = low + (high - low)/2; 7 | if(a[mid] == x) return mid; 8 | if(a[mid] >= a[low]) { 9 | if(x=a[low]) 10 | high = mid - 1; 11 | else 12 | low = mid + 1; 13 | } 14 | else { 15 | if(x>a[mid] && x 2 | using namespace std; 3 | 4 | void swap(int &a, int &b){ 5 | int t = a; 6 | a = b; 7 | b = t; 8 | } 9 | void transpose(int a[][4], int n){ 10 | for(int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | string print_binary(string val){ 7 | int pos = val.find('.', 0); 8 | int intpart = atoi(val.substr(0, pos).c_str()); 9 | double decpart = atof(val.substr(pos, val.length()-pos).c_str()); 10 | string intstr = "", decstr = ""; 11 | while(intpart > 0){ 12 | if(intpart&1) intstr = "1" + intstr; 13 | else intstr = "0" + intstr; 14 | intpart >>= 1; 15 | } 16 | while(decpart > 0){ 17 | if(decstr.length() > 32) return "ERROR"; 18 | decpart *= 2; 19 | if(decpart >= 1){ 20 | decstr += "1"; 21 | decpart -= 1; 22 | } 23 | else 24 | decstr += "0"; 25 | } 26 | return intstr + "." + decstr; 27 | } 28 | int main(){ 29 | string val = "19.25"; 30 | cout< 2 | using namespace std; 3 | 4 | const int kMaxInt = ~(1<<31); 5 | 6 | int ShortestDist(string text[], int n, string word1, string word2){ 7 | int min = kMaxInt / 2; 8 | int pos1 = -min; 9 | int pos2 = -min; 10 | 11 | for(int pos=0; pos 2 | using namespace std; 3 | 4 | class Bitmap{ 5 | public: 6 | Bitmap(int size = 32){ 7 | bits = new int[size/32 + 1]; 8 | } 9 | ~Bitmap(){ 10 | delete[] bits; 11 | } 12 | bool get(int pos){// true if bit is 1, else: false 13 | return (bits[pos/32] & (1<<(pos&0x1f))) != 0; 14 | } 15 | void set(int pos){ 16 | bits[pos/32] |= (1<<(pos&0x1f)); 17 | } 18 | private: 19 | int *bits; 20 | }; 21 | 22 | void print_duplicates(int a[], int n, int bitsize){ 23 | Bitmap bm(bitsize); 24 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void print_binary(int n){ 6 | vector v; 7 | int len = 8 * sizeof(int); 8 | int mask = 1; 9 | while(len--){ 10 | if(n&mask) v.push_back(1); 11 | else v.push_back(0); 12 | mask <<= 1; 13 | } 14 | while(!v.empty()){ 15 | cout<>(j+1)) << (j+1)) | (m< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isAnagram1(string s, string t){ 7 | if(s=="" || t=="") return false; 8 | if(s.length() != t.length()) return false; 9 | 10 | sort(&s[0], &s[0]+s.length()); 11 | sort(&t[0], &t[0]+t.length()); 12 | if(s == t) return true; 13 | else return false; 14 | } 15 | bool isAnagram(string s, string t){ 16 | if(s=="" || t=="") return false; 17 | if(s.length() != t.length()) return false; 18 | 19 | int len = s.length(); 20 | int c[256]; 21 | memset(c, 0, sizeof(c)); 22 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void Qsort(stack &s){ 8 | priority_queue< int,vector,greater > q; 9 | while(!s.empty()){ 10 | q.push(s.top()); 11 | s.pop(); 12 | } 13 | while(!q.empty()){ 14 | s.push(q.top()); 15 | q.pop(); 16 | } 17 | } 18 | 19 | stack Ssort(stack s){ 20 | stack t; 21 | while(!s.empty()){ 22 | int data = s.top(); 23 | s.pop(); 24 | while(!t.empty() && t.top()>data){ 25 | s.push(t.top()); 26 | t.pop(); 27 | } 28 | t.push(data); 29 | } 30 | return t; 31 | } 32 | 33 | int main(){ 34 | srand((unsigned)time(0)); 35 | stack s; 36 | 37 | for(int i=0; i<10; ++i) 38 | s.push((rand()%100)); 39 | s = Ssort(s); 40 | //Qsort(s); 41 | while(!s.empty()){ 42 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int maxn = 100; 8 | bool g[maxn][maxn], visited[maxn]; 9 | int n; 10 | queue q; 11 | 12 | void init(){ 13 | memset(g, false, sizeof(g)); 14 | memset(visited, false, sizeof(visited)); 15 | } 16 | bool route(int src, int dst){ 17 | q.push(src); 18 | visited[src] = true; 19 | while(!q.empty()){ 20 | int t = q.front(); 21 | q.pop(); 22 | if(t == dst) return true; 23 | for(int i=0; i>n>>m; 37 | for(int i=0; i>u>>v; 39 | g[u][v] = true; 40 | } 41 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | // freopen("12.3.in", "w", stdout); 8 | // int miss = 12345; 9 | // for(int i=0; i<20000; ++i){ 10 | // if(i == miss) continue; 11 | // cout< 2 | #include 3 | using namespace std; 4 | 5 | struct op{ 6 | int begin, end; 7 | char src, bri, dst; 8 | op(){ 9 | 10 | } 11 | op(int pbegin, int pend, int psrc, int pbri, int pdst):begin(pbegin), end(pend), src(psrc), bri(pbri), dst(pdst){ 12 | 13 | } 14 | }; 15 | void hanoi(int n, char src, char bri, char dst){ 16 | stack st; 17 | op tmp; 18 | st.push(op(1, n, src, bri, dst)); 19 | while(!st.empty()){ 20 | tmp = st.top(); 21 | st.pop(); 22 | if(tmp.begin != tmp.end){ 23 | st.push(op(tmp.begin, tmp.end-1, tmp.bri, tmp.src, tmp.dst)); 24 | st.push(op(tmp.end, tmp.end, tmp.src, tmp.bri, tmp.dst)); 25 | st.push(op(tmp.begin, tmp.end-1, tmp.src, tmp.dst, tmp.bri)); 26 | } 27 | else{ 28 | cout<<"Move disk "< 2 | #include 3 | using namespace std; 4 | 5 | int mini(int a, int b){ 6 | return a < b ? a : b; 7 | } 8 | int mini(int a, int b, int c){ 9 | return mini(mini(a, b), c); 10 | } 11 | int get_num(int k){ 12 | if(k <= 0) return 0; 13 | int res = 1, cnt = 1; 14 | queue q3, q5, q7; 15 | q3.push(3); q5.push(5); q7.push(7); 16 | for(; cnt 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | struct Node{ 8 | int key; 9 | Node *lchild, *rchild, *parent; 10 | }; 11 | Node *p, node[maxn]; 12 | int cnt; 13 | 14 | void init(){ 15 | p = NULL; 16 | memset(node, '\0', sizeof(node)); 17 | cnt = 0; 18 | } 19 | void create_minimal_tree(Node* &head, Node *parent, int a[], int start, int end){ 20 | if(start <= end){ 21 | int mid = (start + end)>>1; 22 | node[cnt].key = a[mid]; 23 | node[cnt].parent = parent; 24 | head = &node[cnt++]; 25 | create_minimal_tree(head->lchild, head, a, start, mid-1); 26 | create_minimal_tree(head->rchild, head, a, mid+1, end); 27 | } 28 | } 29 | int height(Node *head){ 30 | if(head == NULL) return 0; 31 | return max(height(head->lchild), height(head->rchild)) + 1; 32 | } 33 | int main(){ 34 | init(); 35 | int a[] = { 36 | 0, 1, 2, 3, 4, 5, 6, 7, 8 37 | }; 38 | Node *head = NULL; 39 | create_minimal_tree(head, NULL, a, 0, 8); 40 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void zero(int **a, int m, int n){ 7 | bool row[m], col[n]; 8 | memset(row, false, sizeof(row)); 9 | memset(col, false, sizeof(col)); 10 | for(int i=0; i>m>>n; 27 | int **a; 28 | a = new int*[m]; 29 | for(int i=0; i>a[i][j]; 34 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | typedef vector vs; 6 | 7 | vs permu(string s){ 8 | vs result; 9 | if(s == ""){ 10 | result.push_back(""); 11 | return result; 12 | } 13 | string c = s.substr(0, 1); 14 | vs res = permu(s.substr(1)); 15 | for(int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | struct person{ 8 | int h, w; 9 | }; 10 | person p[maxn]; 11 | int d[maxn]; 12 | 13 | bool cmp(person p1, person p2){ 14 | if(p1.h == p2.h) return p1.w < p2.w; 15 | else return p1.h < p2.h; 16 | } 17 | int lis(person p[], int n){ 18 | int k = 1; 19 | d[0] = p[0].w; 20 | for(int i=1; i d[k-1]) d[k++] = p[i].w; 22 | else{ 23 | int j; 24 | for(j=k-1; j>=0 && d[j]>p[i].w; --j);//用二分可将复杂度降到O(nlogn) 25 | d[j+1] = p[i].w; 26 | } 27 | } 28 | return k; 29 | } 30 | int lis1(person p[], int n) { 31 | int k = 1; 32 | person d[n]; 33 | d[0] = p[0]; 34 | for(int i=1; id[k-1].w && p[i].h>d[k-1].h) d[k++] = p[i]; 36 | else { 37 | int j; 38 | for(j=k-1; j>=0 && (p[i].w<)) 39 | } 40 | } 41 | } 42 | int main(){ 43 | freopen("9.7.in", "r", stdin); 44 | int n; 45 | cin>>n; 46 | for(int i=0; i>p[i].h>>p[i].w; 48 | sort(p, p+n, cmp); 49 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isUnique1(string s) 6 | { 7 | bool a[256]; 8 | memset(a, 0, sizeof(a)); 9 | int len = s.length(); 10 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | const int MAX_N = 100; 6 | int p[MAX_N][MAX_N], A[MAX_N][MAX_N]; 7 | 8 | void PreCompute(int n){ 9 | for(int i=0; i<=n; ++i) 10 | p[0][i] = p[i][0] = 0; 11 | for(int i=1; i<=n; ++i) 12 | for(int j=1; j<=n; ++j) 13 | p[i][j] = p[i-1][j] + p[i][j-1] - p[i-1][j-1] + A[i][j]; 14 | } 15 | 16 | int MaxSum(int n){ 17 | int max_sum = 1<<31; //min int 18 | for(int i=1; i<=n; ++i) 19 | for(int j=i; j<=n; ++j){ 20 | int cur_sum = 0; 21 | for(int k=1; k<=n; ++k){ 22 | int val = p[j][k]-p[j][k-1]-p[i-1][k]+p[i-1][k-1]; 23 | if(cur_sum <= 0) 24 | cur_sum = val; 25 | else 26 | cur_sum += val; 27 | 28 | if(cur_sum > max_sum) 29 | max_sum = cur_sum; 30 | } 31 | } 32 | 33 | return max_sum; 34 | } 35 | 36 | int main(){ 37 | freopen("20.12.in", "r", stdin); 38 | int n; 39 | cin>>n; 40 | for(int i=1; i<=n; ++i)//元素存储从1开始 41 | for(int j=1; j<=n; ++j) 42 | cin>>A[i][j]; 43 | PreCompute(n); 44 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int fetch(int a[], int i, int j){ 6 | return (a[i] >> j) & 1; //return 0/1 7 | } 8 | int get(int a[], int i){ 9 | int ret = 0; 10 | for(int j=31; j>=0; --j) 11 | ret = (ret << 1) | fetch(a, i, j); 12 | return ret; 13 | } 14 | int missing(int a[], int n){ 15 | bool *b = new bool[n+1]; 16 | memset(b, false, (n+1)*sizeof(bool)); 17 | for(int i=0; i> (j % 32)) & 1; 26 | } 27 | int get1(int a[], int i){ 28 | int ret = 0; 29 | int base = 32*i; 30 | for(int j=base+31; j>=base; --j) 31 | ret = (ret << 1) | fetch1(a, j); 32 | return ret; 33 | } 34 | int missing1(int a[], int n){ 35 | bool *b = new bool[n+1]; 36 | memset(b, false, (n+1)*sizeof(bool)); 37 | for(int i=0; i 2 | using namespace std; 3 | 4 | typedef struct node{ 5 | int data; 6 | node *next; 7 | }node; 8 | 9 | node* init(int a[], int n){ 10 | node *head, *p; 11 | for(int i=0; idata = a[i]; 14 | if(i==0){ 15 | head = p = nd; 16 | continue; 17 | } 18 | p->next = nd; 19 | p = nd; 20 | } 21 | return head; 22 | } 23 | 24 | bool remove(node *c){ 25 | if(c==NULL || c->next==NULL) return false; 26 | // if(c->next==NULL){//c为最后一个元素时直接删除,不行,最后还是会打印出一个为0的结点,需要特殊处理 27 | // delete c; 28 | // return; 29 | // } 30 | node *q = c->next; 31 | c->data = q->data; 32 | c->next = q->next; 33 | delete q; 34 | return true; 35 | } 36 | void print(node *head){ 37 | while(head){ 38 | cout<data<<" "; 39 | head = head->next; 40 | } 41 | cout<next; 53 | print(head); 54 | if(remove(c)) 55 | print(head); 56 | else 57 | cout<<"failure"< 2 | using namespace std; 3 | 4 | const int INF = ~(1<<31); 5 | 6 | void swap(int &a, int &b){ 7 | a = a^b; 8 | b = a^b; 9 | a = a^b; 10 | } 11 | int flipsign(int a){ 12 | int d = a < 0 ? 1 : -1; 13 | int opa = 0; 14 | while(a != 0){ 15 | a += d; 16 | opa += d; 17 | } 18 | return opa; 19 | } 20 | int abs(int a){ 21 | if(a < 0) a = flipsign(a); 22 | return a; 23 | } 24 | bool opsign(int a, int b){ 25 | return (a>0 && b<0) || (a<0 && b>0); 26 | } 27 | int times(int a, int b){ 28 | int aa = abs(a), bb = abs(b); 29 | int res = 0; 30 | if(aa < bb) swap(aa, bb); 31 | for(int i=0; i=0; ++res); 43 | if(opsign(a, b)) res = flipsign(res); 44 | return res; 45 | } 46 | int main(){ 47 | int a[] = { 48 | 8, 0, -8, -5, 9 49 | }; 50 | int b[] = { 51 | 3, 5, 3, 0, -3 52 | }; 53 | for(int i=0; i<5; ++i){ 54 | cout< 2 | using namespace std; 3 | 4 | typedef struct node{ 5 | int data; 6 | node *next; 7 | }node; 8 | 9 | node* init(int a[], int n){ 10 | node *head, *p; 11 | for(int i=0; idata = a[i]; 14 | if(i==0){ 15 | head = p = nd; 16 | continue; 17 | } 18 | p->next = nd; 19 | p = nd; 20 | } 21 | return head; 22 | } 23 | 24 | node* findNthToLast(node *head, int n){ 25 | if(head==NULL || n<1) return NULL; 26 | node *p=head, *q=head; 27 | while(n>0 && q){ 28 | q = q->next; 29 | --n; 30 | } 31 | if(n>0) return NULL; 32 | while(q){ 33 | p = p->next; 34 | q = q->next; 35 | } 36 | return p; 37 | } 38 | node *pp; 39 | int nn; 40 | void findNthToLast1(node *head){ 41 | if(head==NULL) return; 42 | findNthToLast1(head->next); 43 | if(nn==1) pp = head; 44 | --nn; 45 | } 46 | int main(){ 47 | int n = 10; 48 | int a[] = { 49 | 9, 2, 1, 3, 5, 6, 2, 6, 3, 1 50 | }; 51 | node *head = init(a, n); 52 | node *p = findNthToLast(head, 6); 53 | if(p) cout<data<data< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | freopen("12.3.in", "r", stdin);// 20000 number 7 | int int_len = sizeof(int) * 8; 8 | int totalnum = 20000; 9 | int blocksize = 2000; 10 | int blocknum = totalnum / blocksize; 11 | int* block = new int[blocknum]; 12 | int* bit = new int[blocksize/int_len+1]; 13 | int v; 14 | while(scanf("%d", &v) != EOF){ 15 | ++block[v/blocksize]; 16 | } 17 | fclose(stdin); 18 | int start; 19 | for(int i=0; i=start && v 2 | #include 3 | using namespace std; 4 | 5 | class Trie{ 6 | public: 7 | static const int MAX_N = 100 * 100;// 100为主串长度 8 | static const int CLD_NUM = 26; 9 | int size; 10 | int trie[MAX_N][CLD_NUM]; 11 | 12 | Trie(const char* s); 13 | void insert(const char* s); 14 | bool find(const char* s); 15 | }; 16 | 17 | Trie::Trie(const char* s){ 18 | memset(trie[0], -1, sizeof(trie[0])); 19 | size = 1; 20 | while(*s){ 21 | insert(s); 22 | ++s; 23 | } 24 | } 25 | 26 | void Trie::insert(const char* s){ 27 | int p = 0; 28 | while(*s){ 29 | int i = *s - 'a'; 30 | if(-1 == trie[p][i]){ 31 | memset(trie[size], -1, sizeof(trie[size])); 32 | trie[p][i] = size++; 33 | } 34 | p = trie[p][i]; 35 | ++s; 36 | } 37 | } 38 | 39 | bool Trie::find(const char* s){ 40 | int p = 0; 41 | while(*s){ 42 | int i = *s - 'a'; 43 | if(-1 == trie[p][i]) 44 | return false; 45 | p = trie[p][i]; 46 | ++s; 47 | } 48 | return true; 49 | } 50 | 51 | int main(){ 52 | Trie tree("mississippi"); 53 | string patt[] = { 54 | "is", "sip", "hi", "sis", "mississippa" 55 | }; 56 | int n = 5; 57 | for(int i=0; i 2 | #include 3 | #include "hash.h" 4 | using namespace std; 5 | 6 | Hash hash; 7 | 8 | inline bool cmp(string s1, string s2){//按长度从大到小排 9 | return s2.length() < s1.length(); 10 | } 11 | 12 | bool MakeOfWords(string word, int length){ 13 | //cout<<"curr: "< 2 | 3 | const int kWordSize = 26 + 5; 4 | const int kNodeSize = 1200 + 5; 5 | const int kHashSize = 10001; //大质数 6 | 7 | struct Node{ 8 | char word[kWordSize]; 9 | Node *next; 10 | }; 11 | Node node[kNodeSize + 1]; 12 | Node* head[kHashSize + 1]; 13 | 14 | class Hash{ 15 | public: 16 | Hash(); 17 | unsigned int hash(const char* str); 18 | void insert(const char* str); 19 | bool find(const char* str); 20 | private: 21 | unsigned int seed; 22 | unsigned int size; 23 | }; 24 | 25 | Hash::Hash():seed(131),size(0){ 26 | memset(head, 0, sizeof(head)); 27 | } 28 | 29 | unsigned int Hash::hash(const char* str){ 30 | unsigned int hash = 0; 31 | while(*str++) 32 | hash = hash * seed + (*str); 33 | return (hash & 0x7FFFFFFF) % kHashSize; 34 | } 35 | 36 | void Hash::insert(const char* str){ 37 | unsigned int id = hash(str); 38 | char *dst = (char*)node[size].word; 39 | while(*dst++ = *str++); 40 | node[size].next = head[id]; 41 | head[id] = &node[size]; 42 | ++size; 43 | } 44 | 45 | bool Hash::find(const char* str){ 46 | unsigned int id = hash(str); 47 | for(Node* p=head[id]; p ; p=p->next){ 48 | char *dst = (char*)p->word; 49 | int i = 0; 50 | while(*(str+i) && *(dst+i)==*(str+i)) 51 | ++i; 52 | if(*(str+i)=='\0' && *(dst+i)=='\0') 53 | return true; 54 | } 55 | return false; 56 | } 57 | -------------------------------------------------------------------------------- /4.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int maxn = 100; 6 | struct Node{ 7 | int key; 8 | Node *lchild, *rchild, *parent; 9 | }; 10 | Node *p, node[maxn]; 11 | int cnt; 12 | 13 | void init(){ 14 | p = NULL; 15 | memset(node, '\0', sizeof(node)); 16 | cnt = 0; 17 | } 18 | void create_minimal_tree(Node* &head, Node *parent, int a[], int start, int end){ 19 | if(start <= end){ 20 | int mid = (start + end)>>1; 21 | node[cnt].key = a[mid]; 22 | node[cnt].parent = parent; 23 | head = &node[cnt++]; 24 | create_minimal_tree(head->lchild, head, a, start, mid-1); 25 | create_minimal_tree(head->rchild, head, a, mid+1, end); 26 | } 27 | } 28 | Node* minimal(Node* no){ 29 | if(no == NULL) return NULL; 30 | while(no->lchild) 31 | no = no->lchild; 32 | return no; 33 | } 34 | Node* successor(Node* no){ 35 | if(no == NULL) return NULL; 36 | if(no->rchild) return minimal(no->rchild); 37 | Node *y = no->parent; 38 | while(y && y->rchild==no){ 39 | no = y; 40 | y = y->parent; 41 | } 42 | return y; 43 | } 44 | int main(){ 45 | int a[] = { 46 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 47 | }; 48 | init(); 49 | Node *head = NULL; 50 | create_minimal_tree(head, NULL, a, 0, 9); 51 | cout<<"the head is "<key<key< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | enum color{ 7 | red, yellow, blue, green 8 | }; 9 | 10 | bool paint_fill(color **screen, int m, int n, int x, int y, color c){ 11 | if(x<0 || x>=m || y<0 || y>=n) return false; 12 | if(screen[x][y] == c) return false; 13 | else{ 14 | screen[x][y] = c; 15 | paint_fill(screen, m, n, x-1, y, c); 16 | paint_fill(screen, m, n, x+1, y, c); 17 | paint_fill(screen, m, n, x, y-1, c); 18 | paint_fill(screen, m, n, x, y+1, c); 19 | } 20 | return true; 21 | } 22 | int main(){ 23 | freopen("8.6.in", "r", stdin); 24 | int m, n; 25 | cin>>m>>n; 26 | color **screen = new color*[m]; 27 | for(int i=0; i>t; 33 | screen[i][j]=(color)t; 34 | } 35 | 36 | // color screen[5][5] = { 37 | // {red, green, green, blue, yellow}, 38 | // {green, red, yellow, green, green}, 39 | // {green, green, red, blue, green}, 40 | // {red, green, green, green, yellow}, 41 | // {red, yellow, red, blue, yellow} 42 | // }; 43 | paint_fill(screen, 5, 5, 1, 2, green); 44 | for(int i=0; i<5; ++i){ 45 | for(int j=0; j<5; ++j) 46 | cout< 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | ll fib(ll n){ 6 | if(n < 1) return -1; 7 | if(n == 1 || n == 2) return 1; 8 | else return fib(n-1) + fib(n-2); 9 | } 10 | ll fib1(ll n){ 11 | if(n < 1) return -1; 12 | if(n == 1 || n == 2) return 1; 13 | ll a = 1, b = 1; 14 | for(ll i=3; i<=n; ++i){ 15 | ll c = a + b; 16 | a = b; 17 | b = c; 18 | } 19 | return b; 20 | } 21 | void mul(ll c[2][2], ll a[2][2], ll b[2][2]){ 22 | ll t[4]; 23 | t[0] = a[0][0]*b[0][0] + a[0][1]*b[1][0]; 24 | t[1] = a[0][0]*b[0][1] + a[0][1]*b[1][1]; 25 | t[2] = a[1][0]*b[0][0] + a[1][1]*b[1][0]; 26 | t[3] = a[1][0]*b[0][1] + a[1][1]*b[1][1]; 27 | c[0][0] = t[0]; 28 | c[0][1] = t[1]; 29 | c[1][0] = t[2]; 30 | c[1][1] = t[3]; 31 | } 32 | void pow(ll s[2][2], ll a[2][2], ll n){ 33 | while(n > 0){ 34 | if(n&1) mul(s, s, a); 35 | mul(a, a, a); 36 | n >>= 1; 37 | } 38 | } 39 | ll fib2(ll n){ 40 | if(n < 1) return -1; 41 | if(n == 1 || n == 2) return 1; 42 | 43 | ll a[2][2] = { {1, 1}, {1, 0} }; 44 | ll s[2][2] = { {1, 0}, {0, 1} }; 45 | pow(s, a, n-2); 46 | return s[0][0] + s[0][1]; 47 | } 48 | int main(){ 49 | for(int i=1; i<20; ++i) 50 | cout< 2 | using namespace std; 3 | 4 | int cnt = 0; 5 | void sumN(int sum, int c, int n){ 6 | if(sum >= n){ 7 | if(sum == n) ++cnt; 8 | return; 9 | } 10 | else{ 11 | if(c >= 25) 12 | sumN(sum+25, 25, n); 13 | if(c >= 10) 14 | sumN(sum+10, 10, n); 15 | if(c >= 5) 16 | sumN(sum+5, 5, n); 17 | if(c >= 1) 18 | sumN(sum+1, 1, n); 19 | } 20 | } 21 | int sum_n(int sum, int c, int n){ 22 | int ways = 0; 23 | if(sum <= n){ 24 | if(sum == n) return 1; 25 | if(c >= 25) 26 | ways += sum_n(sum+25, 25, n); 27 | if(c >= 10) 28 | ways += sum_n(sum+10, 10, n); 29 | if(c >= 5) 30 | ways += sum_n(sum+5, 5, n); 31 | if(c >= 1) 32 | ways += sum_n(sum+1, 1, n); 33 | } 34 | return ways; 35 | } 36 | int make_change(int n, int denom){ 37 | int next_denom = 0; 38 | switch(denom){ 39 | case 25: 40 | next_denom = 10; 41 | break; 42 | case 10: 43 | next_denom = 5; 44 | break; 45 | case 5: 46 | next_denom = 1; 47 | break; 48 | case 1: 49 | return 1; 50 | } 51 | int ways = 0; 52 | for(int i=0; i*denom<=n; ++i) 53 | ways += make_change(n-i*denom, next_denom); 54 | return ways; 55 | } 56 | int main(){ 57 | int n = 10; 58 | sumN(0, 25, n); 59 | cout< 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class SmartPointer{ 7 | public: 8 | SmartPointer(T* ptr){ 9 | ref = ptr; 10 | ref_count = (unsigned*)malloc(sizeof(unsigned)); 11 | *ref_count = 1; 12 | } 13 | 14 | SmartPointer(SmartPointer &sptr){ 15 | ref = sptr.ref; 16 | ref_count = sptr.ref_count; 17 | ++*ref_count; 18 | } 19 | 20 | SmartPointer& operator=(SmartPointer &sptr){ 21 | if (this != &sptr) { 22 | if (--*ref_count == 0){ 23 | clear(); 24 | cout<<"operator= clear"< sp1(ip1), sp2(ip2); 62 | SmartPointer spa = sp1; 63 | sp2 = spa; 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /2.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef struct node{ 6 | int data; 7 | node *next; 8 | }node; 9 | 10 | node* init(int a[], int n, int m){ 11 | node *head, *p, *q; 12 | for(int i=0; idata = a[i]; 15 | if(i==m) q = nd; 16 | if(i==0){ 17 | head = p = nd; 18 | continue; 19 | } 20 | p->next = nd; 21 | p = nd; 22 | } 23 | p->next = q; 24 | return head; 25 | } 26 | 27 | node* loopstart(node *head){ 28 | if(head==NULL) return NULL; 29 | node *fast = head, *slow = head; 30 | while(fast && fast->next){ 31 | fast = fast->next->next; 32 | slow = slow->next; 33 | if(fast==slow) break; 34 | } 35 | if(!fast || !fast->next) return NULL; 36 | slow = head; 37 | while(fast!=slow){ 38 | fast = fast->next; 39 | slow = slow->next; 40 | } 41 | return fast; 42 | } 43 | 44 | map hash; 45 | node* loopstart1(node *head){ 46 | while(head){ 47 | if(hash[head]) return head; 48 | else{ 49 | hash[head] = true; 50 | head = head->next; 51 | } 52 | } 53 | return head; 54 | } 55 | int main(){ 56 | int n = 10, m = 9;// mdata< 2 | #include 3 | using namespace std; 4 | 5 | char* replace1(char *c){ 6 | if(c == NULL) return NULL; 7 | int len = strlen(c); 8 | if(len == 0) return NULL; 9 | int cnt = 0; 10 | for(int i=0; i=0; --i) 49 | { 50 | if(c[i] == ' ') 51 | { 52 | c[p] = '0'; 53 | c[p-1] = '2'; 54 | c[p-2] = '%'; 55 | p -= 3; 56 | } 57 | else 58 | { 59 | c[p] = c[i]; 60 | --p; 61 | } 62 | } 63 | } 64 | int main(){ 65 | const int len = 100; 66 | char c[len] = ""; 67 | cout< 2 | #include 3 | using namespace std; 4 | 5 | typedef vector > vvi; 6 | typedef vector vi; 7 | vvi get_subsets(int a[], int n){ //O(n2^n) 8 | vvi subsets; 9 | int max = 1< 0){ 15 | if(j&1){ 16 | subset.push_back(a[idx]); 17 | } 18 | j >>= 1; 19 | ++idx; 20 | } 21 | subsets.push_back(subset); 22 | } 23 | return subsets; 24 | } 25 | vvi get_subsets1(int a[], int idx, int n){ 26 | vvi subsets; 27 | if(idx == n){ 28 | vi subset; 29 | subsets.push_back(subset); //empty set 30 | } 31 | else{ 32 | vvi rsubsets = get_subsets1(a, idx+1, n); 33 | int v = a[idx]; 34 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | const int maxn = 100; 6 | struct Node{ 7 | int key; 8 | Node *lchild, *rchild, *parent; 9 | }; 10 | Node node[maxn]; 11 | int cnt; 12 | 13 | void init(){ 14 | memset(node, '\0', sizeof(node)); 15 | cnt = 0; 16 | } 17 | void create_minimal_tree(Node* &head, Node *parent, int a[], int start, int end){ 18 | if(start <= end){ 19 | int mid = (start + end)>>1; 20 | node[cnt].key = a[mid]; 21 | node[cnt].parent = parent; 22 | head = &node[cnt++]; 23 | create_minimal_tree(head->lchild, head, a, start, mid-1); 24 | create_minimal_tree(head->rchild, head, a, mid+1, end); 25 | } 26 | } 27 | bool match(Node* r1, Node* r2){ 28 | if(r1 == NULL && r2 == NULL) return true; 29 | else if(r1 == NULL || r2 == NULL) return false; 30 | else if(r1->key != r2->key) return false; 31 | else return match(r1->lchild, r2->lchild) && match(r1->rchild, r2->rchild); 32 | } 33 | bool subtree(Node* r1, Node* r2){ 34 | if(r1 == NULL) return false; 35 | else if(r1->key == r2->key){ 36 | if(match(r1, r2)) return true; 37 | } 38 | else return subtree(r1->lchild, r2) || subtree(r1->rchild, r2); 39 | } 40 | bool contain_tree(Node* r1, Node* r2){ 41 | if(r2 == NULL) return true; 42 | else return subtree(r1, r2); 43 | } 44 | int main(){ 45 | init(); 46 | int a1[] = { 47 | 0, 1, 2, 3, 4, 5, 6 48 | }; 49 | int a2[] = { 50 | 0, 1, 2 51 | }; 52 | Node *r1 = NULL, *r2 = NULL; 53 | create_minimal_tree(r1, NULL, a1, 0, 6); 54 | create_minimal_tree(r2, NULL, a2, 0, 2); 55 | if(contain_tree(r1, r2)) 56 | cout<<"tree r1 contains r2"< 2 | #include 3 | using namespace std; 4 | 5 | struct Result{ 6 | int hits; 7 | int pseudo_hits; 8 | }; 9 | 10 | Result Estimate(const char* solution, const char* guess){ 11 | Result res; 12 | res.hits = 0; 13 | res.pseudo_hits = 0; 14 | int solution_mask = 0; 15 | for(int i=0; i<4; ++i){ 16 | solution_mask |= 1 << (solution[i] - 'A'); 17 | } 18 | for(int i=0; i<4; ++i){ 19 | if(guess[i] == solution[i]) 20 | ++res.hits; 21 | else if(solution_mask & ( 1<<(guess[i] - 'A'))) 22 | ++res.pseudo_hits; 23 | } 24 | return res; 25 | } 26 | 27 | int Min(int a, int b){ 28 | return a < b ? a : b; 29 | } 30 | Result Estimate1(const char* solution, const char* guess){ 31 | Result res; 32 | res.hits = 0; 33 | res.pseudo_hits = 0; 34 | int num = 26 + 5; 35 | int guess_count[num], solution_count[num]; 36 | memset(guess_count, 0, sizeof(guess_count)); 37 | memset(solution_count, 0, sizeof(solution_count)); 38 | for(int i=0; i<4; ++i){ 39 | if(guess[i] == solution[i]) 40 | ++res.hits; 41 | ++guess_count[(int)(guess[i]-'A')]; 42 | ++solution_count[(int)(solution[i]-'A')]; 43 | } 44 | char color[] = "RGBY"; 45 | for(int i=0; i<4; ++i){ 46 | int idx = (int)(color[i] - 'A'); 47 | res.pseudo_hits += Min(guess_count[idx], solution_count[idx]); 48 | } 49 | res.pseudo_hits -= res.hits; 50 | return res; 51 | } 52 | int main(){ 53 | char solution[] = "RYGB"; 54 | char guess[] = "YRRR"; 55 | Result res = Estimate(solution, guess); 56 | cout< 2 | #include 3 | using namespace std; 4 | 5 | const int MAX_N = 100; 6 | int matrix[MAX_N][MAX_N]; 7 | 8 | struct SubSquare{ 9 | int row, col, size; 10 | }; 11 | 12 | inline int max(int a, int b){ 13 | return a > b ? a : b; 14 | } 15 | 16 | bool IsSquare(int row, int col, int size){ 17 | for(int i=0; i max_size){ 35 | for(int row=0; row max_size){ 38 | if(IsSquare(row, col, size)){ 39 | max_size = size; 40 | sq.row = row; 41 | sq.col = col; 42 | sq.size = size; 43 | break; 44 | } 45 | --size; 46 | } 47 | } 48 | ++col; 49 | } 50 | return sq; 51 | } 52 | 53 | int main(){ 54 | freopen("20.11.in", "r", stdin); 55 | int n; 56 | cin>>n; 57 | for(int i=0; i>matrix[i][j]; 60 | SubSquare sq = FindSubSquare(n); 61 | cout<<"top: "< 2 | #include 3 | using namespace std; 4 | 5 | typedef struct node{ 6 | int data; 7 | node *next; 8 | }node; 9 | bool hash[100]; 10 | 11 | node* init(int a[], int n){ 12 | node *head, *p; 13 | for(int i=0; idata = a[i]; 16 | if(i==0){ 17 | head = p = nd; 18 | continue; 19 | } 20 | p->next = nd; 21 | p = nd; 22 | } 23 | return head; 24 | } 25 | void removedulicate(node *head){ 26 | if(head==NULL) return; 27 | node *p=head, *q=head->next; 28 | hash[head->data] = true; 29 | while(q){ 30 | if(hash[q->data]){ 31 | node *t = q; 32 | p->next = q->next; 33 | q = p->next; 34 | delete t; 35 | } 36 | else{ 37 | hash[q->data] = true; 38 | p = q; q = q->next; 39 | } 40 | } 41 | } 42 | void removedulicate1(node *head){ 43 | if(head==NULL) return; 44 | node *p, *q, *c=head; 45 | while(c){ 46 | p=c; q=c->next; 47 | int d = c->data; 48 | while(q){ 49 | if(q->data==d){ 50 | node *t = q; 51 | p->next = q->next; 52 | q = p->next; 53 | delete t; 54 | } 55 | else{ 56 | p = q; q = q->next; 57 | } 58 | } 59 | c = c->next; 60 | } 61 | } 62 | void print(node *head){ 63 | while(head){ 64 | cout<data<<" "; 65 | head = head->next; 66 | } 67 | cout< 2 | #include 3 | using namespace std; 4 | 5 | enum Suit { 6 | Spade, Heart, Club, Diamond 7 | }; 8 | 9 | class Card { 10 | private: 11 | int v; 12 | Suit s; 13 | bool available; 14 | public: 15 | Card(){ 16 | available = true; 17 | } 18 | Card(int val, Suit su){ 19 | v = val; 20 | s = su; 21 | available = true; 22 | } 23 | int value(){ 24 | return v; 25 | } 26 | Suit suit(){ 27 | return s; 28 | } 29 | bool is_available(){ 30 | return available; 31 | } 32 | void mark_unavailable(){ 33 | available = false; 34 | } 35 | void mark_available(){ 36 | available = true; 37 | } 38 | }; 39 | 40 | class Deck { 41 | private: 42 | vector deck; 43 | int used; 44 | public: 45 | Deck(){ 46 | for(int i=1; i<=13; ++i) 47 | for(int j=0; j<4; ++j) 48 | deck.push_back(Card(i, (Suit)j)); 49 | used = 0; 50 | } 51 | Deck(vector c){ 52 | deck.assign(c.begin(), c.end()); 53 | used = 0; 54 | } 55 | void shuffle(){ 56 | srand((unsigned)time(NULL)); 57 | for(int i=0; i deal_hand(int num){ 75 | vector hand; 76 | if(remain_cards() < num) return hand; 77 | while(num-- > 0) 78 | hand.push_back(deal_card()); 79 | return hand; 80 | } 81 | }; 82 | -------------------------------------------------------------------------------- /20.4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Count2(int n){ 5 | int count = 0; 6 | while(n > 0){ 7 | if(n%10 == 2) 8 | ++count; 9 | n /= 10; 10 | } 11 | return count; 12 | } 13 | 14 | int Count2s1(int n){ 15 | int count = 0; 16 | for(int i=0; i<=n; ++i) 17 | count += Count2(i); 18 | return count; 19 | 20 | } 21 | 22 | int Count2s(int n){ 23 | int count = 0; 24 | int factor = 1; 25 | int low = 0, cur = 0, high = 0; 26 | 27 | while(n/factor != 0){ 28 | low = n - (n/factor) * factor;//低位数字 29 | cur = (n/factor) % 10;//当前位数字 30 | high = n / (factor*10);//高位数字 31 | 32 | switch(cur){ 33 | case 0: 34 | case 1: 35 | count += high * factor; 36 | break; 37 | case 2: 38 | count += high * factor + low + 1; 39 | break; 40 | default: 41 | count += (high + 1) * factor; 42 | break; 43 | } 44 | 45 | factor *= 10; 46 | } 47 | 48 | return count; 49 | } 50 | 51 | int Countis(int n, int i){ 52 | if(i<1 || i>9) return -1;//i只能是1到9 53 | 54 | int count = 0; 55 | int factor = 1; 56 | int low = 0, cur = 0, high = 0; 57 | 58 | while(n/factor != 0){ 59 | low = n - (n/factor) * factor;//低位数字 60 | cur = (n/factor) % 10;//当前位数字 61 | high = n / (factor*10);//高位数字 62 | 63 | if(cur < i) 64 | count += high * factor; 65 | else if(cur == i) 66 | count += high * factor + low + 1; 67 | else 68 | count += (high + 1) * factor; 69 | 70 | factor *= 10; 71 | } 72 | 73 | return count; 74 | } 75 | int main(){ 76 | for(int i=1; i<1000; ++i) 77 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | typedef struct point{ 8 | int x, y; 9 | }point; 10 | stack sp; 11 | const int MAXN = 20; 12 | int g[MAXN][MAXN]; 13 | point vp[MAXN+MAXN]; 14 | 15 | ll path(ll m, ll n){ 16 | if(m == 1 || n == 1) return 1; 17 | else return path(m-1, n) + path(m, n-1); 18 | } 19 | ll fact(ll n){ 20 | if(n == 0) return 1; 21 | else return n*fact(n-1); 22 | } 23 | ll path1(ll m, ll n){ 24 | return fact(m-1+n-1)/(fact(m-1)*fact(n-1)); 25 | } 26 | bool get_path(int m, int n){ 27 | point p; p.x=n; p.y=m; 28 | sp.push(p); 29 | if(n==1 && m==1) return true; 30 | bool suc = false; 31 | if(m>1 && g[m-1][n]) 32 | suc = get_path(m-1, n); 33 | if(!suc && n>1 && g[m][n-1]) 34 | suc = get_path(m, n-1); 35 | if(!suc) sp.pop(); 36 | return suc; 37 | } 38 | void print_paths(int m, int n, int M, int N, int len){ 39 | if(g[m][n] == 0) return; 40 | point p; p.x=n; p.y=m; 41 | vp[len++] = p; 42 | if(m == M && n == N){ 43 | for(int i=0; i>M>>N; 62 | for(int i=1; i<=M; ++i) 63 | for(int j=1; j<=N; ++j) 64 | cin>>g[i][j]; 65 | cout<<"one of the paths:"< 2 | using namespace std; 3 | 4 | enum Check{ 5 | ROW, COLUMN, DIAGONAL, REDIAGONAL 6 | }; 7 | 8 | int CheckRowColumn(int board[], int n, Check check){ 9 | int type = 0; 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | typedef struct node{ 5 | int data; 6 | node *next; 7 | }node; 8 | 9 | node* init(int a[], int n){ 10 | node *head=NULL, *p; 11 | for(int i=0; idata = a[i]; 14 | if(i==0){ 15 | head = p = nd; 16 | continue; 17 | } 18 | p->next = nd; 19 | p = nd; 20 | } 21 | return head; 22 | } 23 | 24 | node* addlink(node *p, node *q){ 25 | if(p==NULL) return q; 26 | if(q==NULL) return p; 27 | node *res, *pre=NULL; 28 | int c = 0; 29 | while(p && q){ 30 | int t = p->data + q->data + c; 31 | node *r = new node(); 32 | r->data = t%10; 33 | if(pre){ 34 | pre->next = r; 35 | pre = r; 36 | } 37 | else pre = res = r; 38 | c = t/10; 39 | p = p->next; q = q->next; 40 | } 41 | while(p){ 42 | int t = p->data + c; 43 | node *r = new node(); 44 | r->data = t%10; 45 | pre->next = r; 46 | pre = r; 47 | c = t/10; 48 | p = p->next; 49 | } 50 | while(q){ 51 | int t = q->data + c; 52 | node *r = new node(); 53 | r->data = t%10; 54 | pre->next = r; 55 | pre = r; 56 | c = t/10; 57 | q = q->next; 58 | } 59 | if(c>0){ 60 | node *r = new node(); 61 | r->data = c; 62 | pre->next = r; 63 | } 64 | return res; 65 | } 66 | 67 | void print(node *head){ 68 | while(head){ 69 | cout<data<<" "; 70 | head = head->next; 71 | } 72 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int maxn = 100; 8 | struct Node{ 9 | int key; 10 | Node *lchild, *rchild, *parent; 11 | }; 12 | Node *p, node[maxn]; 13 | int cnt; 14 | 15 | void init(){ 16 | p = NULL; 17 | memset(node, '\0', sizeof(node)); 18 | cnt = 0; 19 | } 20 | void createMinimalTree(Node* &head, Node *parent, int a[], int start, int end){ 21 | if(start <= end){ 22 | int mid = (start + end)>>1; 23 | node[cnt].key = a[mid]; 24 | node[cnt].parent = parent; 25 | head = &node[cnt++]; 26 | createMinimalTree(head->lchild, head, a, start, mid-1); 27 | createMinimalTree(head->rchild, head, a, mid+1, end); 28 | } 29 | } 30 | 31 | vector > find_level_linklists(Node *head){ 32 | vector > res; 33 | int level = 0; 34 | list li; 35 | li.push_back(head); 36 | res.push_back(li); 37 | while(!res[level].empty()){ 38 | list l; 39 | list::iterator it; 40 | for(it=res[level].begin(); it!=res[level].end(); ++it){ 41 | Node *n = *it; 42 | if(n->lchild) l.push_back(n->lchild); 43 | if(n->rchild) l.push_back(n->rchild); 44 | } 45 | ++level; 46 | res.push_back(l); 47 | } 48 | return res; 49 | } 50 | void print(vector > res){ 51 | vector >::iterator vit; 52 | for(vit=res.begin(); vit!=res.end(); ++vit){ 53 | list li = *vit; 54 | list::iterator lit; 55 | for(lit=li.begin(); lit!=li.end(); ++lit){ 56 | Node *n = *lit; 57 | cout<key<<" "; 58 | } 59 | cout< > res; 70 | res = find_level_linklists(head); 71 | print(res); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /4.1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | struct Node{ 8 | int key; 9 | Node *lchild, *rchild, *parent; 10 | }; 11 | Node *head, *p, node[maxn]; 12 | int cnt; 13 | 14 | int getHeight(Node *head){ 15 | if(head == NULL) return 0; 16 | else return max(getHeight(head->lchild), getHeight(head->rchild)) + 1; 17 | 18 | } 19 | bool isBanlance(Node *head){ 20 | if(head == NULL) return true; 21 | int dif = getHeight(head->lchild) - getHeight(head->rchild); 22 | if(abs(dif) > 1) 23 | return false; 24 | else 25 | return isBanlance(head->lchild) && isBanlance(head->rchild); 26 | } 27 | void init(){ 28 | head = p = NULL; 29 | memset(node, '\0', sizeof(node)); 30 | cnt = 0; 31 | } 32 | void insert(Node* &head, int x){ 33 | if(head == NULL){ 34 | node[cnt].key = x; 35 | node[cnt].parent = p; 36 | head = &node[cnt++]; 37 | return; 38 | } 39 | p = head; 40 | if(x < head->key) 41 | insert(head->lchild, x); 42 | else 43 | insert(head->rchild, x); 44 | } 45 | int d = 0, num = 0, dep[maxn]; 46 | void getDepth(Node *head){ 47 | if(head == NULL) return; 48 | ++d; 49 | getDepth(head->lchild); 50 | if(head->lchild == NULL && head->rchild == NULL) 51 | dep[num++] = d; 52 | getDepth(head->rchild); 53 | --d; 54 | } 55 | bool isBalance(Node *head){ 56 | if(head == NULL) return true; 57 | getDepth(head); 58 | int max = dep[0], min = dep[0]; 59 | for(int i=0; imax) max = dep[i]; 61 | if(dep[i] 1) return false; 64 | else return true; 65 | } 66 | int main(){ 67 | init(); 68 | int a[] = { 69 | 5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12 70 | }; 71 | for(int i=0; i<12; ++i) 72 | insert(head, a[i]); 73 | getDepth(head); 74 | for(int i=0; i 2 | using namespace std; 3 | 4 | int count_one0(int x){ 5 | int cnt = 0; 6 | for(int i=0; i<32; ++i){ 7 | if(x & 1) ++cnt; 8 | x >>= 1; 9 | } 10 | return cnt; 11 | } 12 | int count_one(int x){ 13 | x = (x & (0x55555555)) + ((x >> 1) & (0x55555555)); 14 | x = (x & (0x33333333)) + ((x >> 2) & (0x33333333)); 15 | x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f)); 16 | x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff)); 17 | x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff)); 18 | return x; 19 | } 20 | int next(int x){ 21 | int max_int = ~(1<<31); 22 | int num = count_one(x); 23 | if(num == 0 || x == -1) return -1; 24 | for(++x; count_one(x) != num && x < max_int; ++x); 25 | if(count_one(x) == num) return x; 26 | return -1; 27 | } 28 | int previous(int x){ 29 | int min_int = (1<<31); 30 | int num = count_one(x); 31 | if(num == 0 || x == -1) return -1; 32 | for(--x; count_one(x) != num && x > min_int; --x); 33 | if(count_one(x) == num) return x; 34 | return -1; 35 | } 36 | int next1(int x){ 37 | int xx = x, bit = 0; 38 | for(; (x&1) != 1 && bit < 32; x >>= 1, ++bit); 39 | for(; (x&1) != 0 && bit < 32; x >>= 1, ++bit); 40 | if(bit == 31) return -1; //011.., none satisify 41 | x |= 1; 42 | x <<= bit; // wtf, x<<32 != 0,so use next line to make x=0 43 | if(bit == 32) x = 0; // for 11100..00 44 | int num1 = count_one(xx) - count_one(x); 45 | int c = 1; 46 | for(; num1 > 0; x |= c, --num1, c <<= 1); 47 | return x; 48 | } 49 | int previous1(int x){ 50 | int xx = x, bit = 0; 51 | for(; (x&1) != 0 && bit < 32; x >>= 1, ++bit); 52 | for(; (x&1) != 1 && bit < 32; x >>= 1, ++bit); 53 | if(bit == 31) return -1; //100..11, none satisify 54 | x -= 1; 55 | x <<= bit; 56 | if(bit == 32) x = 0; 57 | int num1 = count_one(xx) - count_one(x); 58 | x >>= bit; 59 | for(; num1 > 0; x = (x<<1) | 1, --num1, --bit); 60 | //for(; bit > 0; x <<= 1, --bit); 61 | x <<= bit; 62 | return x; 63 | } 64 | int main(){ 65 | int a = -9756;//(1<<31)+(1<<29);//-8737776; 66 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct point{ 9 | double x, y; 10 | }; 11 | class line{ 12 | public: 13 | double epsilon, slope, intercept; 14 | bool bslope; 15 | public: 16 | line(){} 17 | line(point p, point q){ 18 | epsilon = 0.0001; 19 | if(abs(p.x - q.x) > epsilon){ 20 | slope = (p.y-q.y) / (p.x-q.x); 21 | intercept = p.y - slope * p.x; 22 | bslope = true; 23 | } 24 | else{ 25 | bslope = false; 26 | intercept = p.x; 27 | } 28 | } 29 | int hashcode(){ 30 | int sl = (int)(slope * 1000); 31 | int in = (int)(intercept * 1000); 32 | return sl*1000 + in; 33 | } 34 | void print(){ 35 | cout<<"y = "< mii; 43 | for(int i=0; i mii[bestline.hashcode()]) 56 | bestline = l; 57 | } 58 | } 59 | } 60 | // int a = mii[bestline.hashcode()]; 61 | // cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | struct Node{ 8 | int key; 9 | Node *lchild, *rchild, *parent; 10 | }; 11 | Node node[maxn]; 12 | int cnt; 13 | 14 | void init(){ 15 | memset(node, '\0', sizeof(node)); 16 | cnt = 0; 17 | } 18 | void create_minimal_tree(Node* &head, Node *parent, int a[], int start, int end){ 19 | if(start <= end){ 20 | int mid = (start + end)>>1; 21 | node[cnt].key = a[mid]; 22 | node[cnt].parent = parent; 23 | head = &node[cnt++]; 24 | create_minimal_tree(head->lchild, head, a, start, mid-1); 25 | create_minimal_tree(head->rchild, head, a, mid+1, end); 26 | } 27 | } 28 | void print(Node* head, int level){ 29 | vector v; 30 | for(int i=0; ikey); 32 | head = head->parent; 33 | } 34 | while(!v.empty()){ 35 | cout<key; 46 | if(tmp == sum) 47 | print(head, i); 48 | no = no->parent; 49 | } 50 | find_sum(head->lchild, sum); 51 | find_sum(head->rchild, sum); 52 | } 53 | void print2(vector v, int level){ 54 | for(int i=level; i v, int level){ 59 | if(head == NULL) return; 60 | v.push_back(head->key); 61 | int tmp = 0; 62 | for(int i=level; i>-1; --i){ 63 | tmp += v.at(i); 64 | if(tmp == sum) 65 | print2(v, i); 66 | } 67 | vector v1(v), v2(v); 68 | find_sum2(head->lchild, sum, v1, level+1); 69 | find_sum2(head->rchild, sum, v2, level+1); 70 | } 71 | int main(){ 72 | init(); 73 | int a[] = { 74 | 4, 3, 8, 5, 2, 1, 6 75 | }; 76 | Node *head = NULL; 77 | create_minimal_tree(head, NULL, a, 0, 6); 78 | // find_sum(head, 8); 79 | vector v; 80 | find_sum2(head, 8, v, 0); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /20.9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Median{ 8 | private: 9 | priority_queue,less > max_heap;//左边的数 10 | priority_queue,greater > min_heap;//右边的数 11 | 12 | public: 13 | void Insert(int v); 14 | int GetValue(); 15 | }; 16 | 17 | void Median::Insert(int v){ 18 | if(max_heap.empty() && min_heap.empty()) 19 | max_heap.push(v); 20 | // max_heap不为空,则往max_heap插入数据, 21 | // 往min_heap插入数据的话可能导致较小的数放到右边的堆 22 | else if(!max_heap.empty() && min_heap.empty()) 23 | max_heap.push(v); 24 | else if(max_heap.empty() && !min_heap.empty()) 25 | min_heap.push(v); 26 | else{ 27 | if(v < max_heap.top()) 28 | max_heap.push(v); 29 | else 30 | min_heap.push(v); 31 | } 32 | //调整,保证两个堆的元素数量差别不大于1 33 | //不要用hmax_heap.size()-min_heap.size()>1 34 | //因为size返回的是unsigned类型,当左边相减得到一个负数时,本来为false 35 | //但会被转为一个大的正数,结果为true,出问题 36 | while(max_heap.size() > min_heap.size()+1){ 37 | int data = max_heap.top(); 38 | min_heap.push(data); 39 | max_heap.pop(); 40 | } 41 | while(min_heap.size() > max_heap.size()+1){ 42 | int data = min_heap.top(); 43 | max_heap.push(data); 44 | min_heap.pop(); 45 | } 46 | } 47 | 48 | int Median::GetValue(){//中位数为int,由于有除法,也可改为float 49 | if(max_heap.empty() && min_heap.empty()) 50 | return (1<<31); //都为空时,返回int最小值 51 | if(max_heap.size() == min_heap.size()) 52 | return (max_heap.top()+min_heap.top()) / 2; 53 | else if(max_heap.size() > min_heap.size()) 54 | return max_heap.top(); 55 | else 56 | return min_heap.top(); 57 | } 58 | 59 | int main(){ 60 | srand((unsigned)time(0)); 61 | Median md; 62 | // vector vi; 63 | // int num = rand() % 30; //数量是30以内的随机数 64 | // for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct point{ 9 | double x, y; 10 | }; 11 | class line{ 12 | public: 13 | double epsilon, slope, intercept; 14 | bool bslope; 15 | public: 16 | line(){} 17 | line(point p, point q){ 18 | epsilon = 0.0001; 19 | if(abs(p.x - q.x) > epsilon){ 20 | slope = (p.y-q.y) / (p.x-q.x); 21 | intercept = p.y - slope * p.x; 22 | bslope = true; 23 | } 24 | else{ 25 | bslope = false; 26 | intercept = p.x; 27 | } 28 | } 29 | void print(){ 30 | cout<<"y = "< line_count; 49 | for(int i=0; i line_count[bestline]) 61 | bestline = l; 62 | } 63 | } 64 | } 65 | cout< 2 | using namespace std; 3 | 4 | class stack3{ 5 | public: 6 | stack3(int size = 300){ 7 | buf = new int[size*3]; 8 | ptop[0]=ptop[1]=ptop[2]=-1; 9 | this->size = size; 10 | } 11 | ~stack3(){ 12 | delete[] buf; 13 | } 14 | void push(int stackNum, int val){ 15 | int idx = stackNum*size + ptop[stackNum] + 1; 16 | buf[idx] = val; 17 | ++ptop[stackNum]; 18 | } 19 | void pop(int stackNum){ 20 | --ptop[stackNum]; 21 | } 22 | int top(int stackNum){ 23 | int idx = stackNum*size + ptop[stackNum]; 24 | return buf[idx]; 25 | } 26 | bool empty(int stackNum){ 27 | return ptop[stackNum]==-1; 28 | } 29 | 30 | private: 31 | int *buf; 32 | int ptop[3]; 33 | int size; 34 | }; 35 | 36 | typedef struct node{ 37 | int val,preIdx; 38 | }node; 39 | 40 | class stack3_1{ 41 | public: 42 | stack3_1(int totalSize = 900){ 43 | buf = new node[totalSize]; 44 | ptop[0]=ptop[1]=ptop[2]=-1; 45 | this->totalSize = totalSize; 46 | cur = 0; 47 | } 48 | ~stack3_1(){ 49 | delete[] buf; 50 | } 51 | void push(int stackNum, int val){ 52 | buf[cur].val = val; 53 | buf[cur].preIdx = ptop[stackNum]; 54 | ptop[stackNum] = cur; 55 | ++cur; 56 | } 57 | void pop(int stackNum){ 58 | ptop[stackNum] = buf[ptop[stackNum]].preIdx; 59 | } 60 | int top(int stackNum){ 61 | return buf[ptop[stackNum]].val; 62 | } 63 | bool empty(int stackNum){ 64 | return ptop[stackNum]==-1; 65 | } 66 | 67 | private: 68 | node *buf; 69 | int ptop[3]; 70 | int totalSize; 71 | int cur; 72 | }; 73 | 74 | int main(){ 75 | stack3_1 mystack;//stack3 mystack; 76 | for(int i=0; i<10; ++i) 77 | mystack.push(0, i); 78 | for(int i=10; i<20; ++i) 79 | mystack.push(1, i); 80 | for(int i=100; i<110; ++i) 81 | mystack.push(2, i); 82 | for(int i=0; i<3; ++i) 83 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | struct Node{ 8 | int key; 9 | Node *lchild, *rchild, *parent; 10 | }; 11 | Node *p, node[maxn]; 12 | int cnt; 13 | 14 | void init(){ 15 | p = NULL; 16 | memset(node, '\0', sizeof(node)); 17 | cnt = 0; 18 | } 19 | void create_minimal_tree(Node* &head, Node *parent, int a[], int start, int end){ 20 | if(start <= end){ 21 | int mid = (start + end)>>1; 22 | node[cnt].key = a[mid]; 23 | node[cnt].parent = parent; 24 | head = &node[cnt++]; 25 | create_minimal_tree(head->lchild, head, a, start, mid-1); 26 | create_minimal_tree(head->rchild, head, a, mid+1, end); 27 | } 28 | } 29 | Node* first_ancestor(Node* n1, Node* n2){ 30 | if(n1 == NULL || n2 == NULL) return NULL; 31 | map m; 32 | while(n1){ 33 | m[n1] = true; 34 | n1 = n1->parent; 35 | } 36 | while(n2 && !m[n2]){ 37 | n2 = n2->parent; 38 | } 39 | return n2; 40 | } 41 | bool father(Node* n1, Node* n2){ 42 | if(n1 == NULL) return false; 43 | else if(n1 == n2) return true; 44 | else return father(n1->lchild, n2) || father(n1->rchild, n2); 45 | } 46 | Node* first_ancestor1(Node* n1, Node* n2){ 47 | if(n1 == NULL || n2 == NULL) return NULL; 48 | while(n1){ 49 | if(father(n1, n2)) return n1; 50 | n1 = n1->parent; 51 | } 52 | return NULL; 53 | } 54 | void first_ancestor2(Node* head, Node* n1, Node* n2, Node* &ans){ 55 | if(head==NULL || n1==NULL || n2==NULL) return; 56 | if(head && father(head, n1) && father(head, n2)){ 57 | ans = head; 58 | first_ancestor2(head->lchild, n1, n2, ans); 59 | first_ancestor2(head->rchild, n1, n2, ans); 60 | } 61 | } 62 | Node* search(Node* head, int x){ 63 | if(head == NULL) return NULL; 64 | else if(x == head->key) return head; 65 | else if(x <= head->key) search(head->lchild, x); 66 | else search(head->rchild, x); 67 | } 68 | int main(){ 69 | init(); 70 | int a[] = { 71 | 0, 1, 2, 3, 4, 5, 6 72 | }; 73 | Node *head = NULL; 74 | create_minimal_tree(head, NULL, a, 0, 6); 75 | Node *n1 = search(head, 0); 76 | Node *n2 = search(head, 4); 77 | cout<key<<" "<key<key<key< 2 | using namespace std; 3 | 4 | const int MAX_INT = ~(1<<31);//2147483647 5 | 6 | typedef struct node{ 7 | int val, min; 8 | }node; 9 | 10 | class StackWithMin{ 11 | public: 12 | StackWithMin(int size=1000){ 13 | buf = new node[size]; 14 | buf[0].min = MAX_INT; 15 | cur = 0; 16 | } 17 | ~StackWithMin(){ 18 | delete[] buf; 19 | } 20 | void push(int val){ 21 | buf[++cur].val = val; 22 | if(val 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class ACAutomation{ 7 | public: 8 | static const int MAX_N = 1000 * 50; 9 | static const int CLD_NUM = 26; 10 | 11 | int size; 12 | int fail[MAX_N]; 13 | int tag[MAX_N]; 14 | int trie[MAX_N][CLD_NUM]; 15 | 16 | void reset(); 17 | void insert(const char* s); 18 | void construct(); 19 | int query(const char* s); 20 | }; 21 | 22 | void ACAutomation::reset(){ 23 | memset(trie[0], -1, sizeof(trie[0])); 24 | tag[0] = 0; 25 | size = 1; 26 | } 27 | 28 | void ACAutomation::insert(const char* s){ 29 | int p = 0; 30 | while(*s){ 31 | int i = *s - 'a'; 32 | if(-1 == trie[p][i]){ 33 | memset(trie[size], -1, sizeof(trie[size])); 34 | tag[size] = 0; 35 | trie[p][i] = size++; 36 | } 37 | p = trie[p][i]; 38 | ++s; 39 | } 40 | ++tag[p]; 41 | } 42 | 43 | void ACAutomation::construct(){ 44 | queue q; 45 | fail[0] = 0; 46 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | string removeDuplicate1(string s) 6 | { 7 | int check = 0; 8 | int len = s.length(); 9 | if(len < 2) return s; 10 | string str = ""; 11 | for(int i=0; i 2 | using namespace std; 3 | 4 | const int STACK_SIZE = 100; 5 | const int STACK_NUM = 10; 6 | class stack{ 7 | private: 8 | int *buf; 9 | int cur; 10 | int capacity; 11 | 12 | public: 13 | stack(int capa = STACK_SIZE){ 14 | buf = new int[capa]; 15 | cur = -1; 16 | capacity = capa; 17 | } 18 | ~stack(){ 19 | delete[] buf; 20 | } 21 | void push(int val){ 22 | buf[++cur] = val; 23 | } 24 | void pop(){ 25 | --cur; 26 | } 27 | int top(){ 28 | return buf[cur]; 29 | } 30 | bool empty(){ 31 | return cur==-1; 32 | } 33 | bool full(){ 34 | return cur==capacity-1; 35 | } 36 | }; 37 | 38 | class SetOfStacks{//without popAt() 39 | private: 40 | stack *st; 41 | int cur; 42 | int capacity; 43 | 44 | public: 45 | SetOfStacks(int capa=STACK_NUM){ 46 | st = new stack[capa]; 47 | cur = 0; 48 | capacity = capa; 49 | } 50 | ~SetOfStacks(){ 51 | delete[] st; 52 | } 53 | void push(int val){ 54 | if(st[cur].full()) ++cur; 55 | st[cur].push(val); 56 | } 57 | void pop(){ 58 | if(st[cur].empty()) --cur; 59 | st[cur].pop(); 60 | } 61 | int top(){ 62 | if(st[cur].empty()) --cur; 63 | return st[cur].top(); 64 | } 65 | bool empty(){ 66 | if(cur==0) return st[0].empty(); 67 | else return false; 68 | } 69 | bool full(){ 70 | if(cur==capacity-1) return st[cur].full(); 71 | else return false; 72 | } 73 | }; 74 | class SetOfStacks1{ 75 | private: 76 | stack *st; 77 | int cur; 78 | int capacity; 79 | 80 | public: 81 | SetOfStacks1(int capa=STACK_NUM){ 82 | st = new stack[capa]; 83 | cur = 0; 84 | capacity = capa; 85 | } 86 | ~SetOfStacks1(){ 87 | delete[] st; 88 | } 89 | void push(int val){ 90 | if(st[cur].full()) ++cur; 91 | st[cur].push(val); 92 | } 93 | void pop(){ 94 | while(st[cur].empty()) --cur; 95 | st[cur].pop(); 96 | } 97 | void popAt(int idx){ 98 | while(st[idx].empty()) --idx; 99 | st[idx].pop(); 100 | } 101 | int top(){ 102 | while(st[cur].empty()) --cur; 103 | return st[cur].top(); 104 | } 105 | bool empty(){ 106 | while(cur!=-1 && st[cur].empty()) --cur; 107 | if(cur==-1) return true; 108 | else return false; 109 | } 110 | bool full(){ 111 | if(cur==capacity-1) return st[cur].full(); 112 | else return false; 113 | } 114 | }; 115 | int main(){ 116 | // SetOfStacks ss; 117 | // for(int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template 7 | class MyStack{ 8 | public: 9 | MyStack(){ 10 | 11 | } 12 | ~MyStack(){ 13 | 14 | } 15 | void push(T val){ 16 | qin.push(val); 17 | } 18 | void pop(){ 19 | if(!qin.empty()) 20 | move(qin, qout); 21 | else 22 | move(qout, qin); 23 | } 24 | T top(){ 25 | if(!qin.empty()) 26 | return qin.back(); 27 | else 28 | return qout.back(); 29 | } 30 | bool empty(){ 31 | return qin.empty() && qout.empty(); 32 | } 33 | int size(){ 34 | return qin.size() + qout.size(); 35 | } 36 | void move(queue &src, queue &dst){ 37 | while(!src.empty()){ 38 | T val = src.front(); 39 | src.pop(); 40 | if(src.empty()) break; 41 | dst.push(val); 42 | } 43 | } 44 | private: 45 | queue qin, qout; 46 | }; 47 | template 48 | class MyQueue{ 49 | public: 50 | MyQueue(){ 51 | 52 | } 53 | ~MyQueue(){ 54 | 55 | } 56 | void push(T val){ 57 | move(sout, sin); 58 | sin.push(val); 59 | } 60 | void pop(){ 61 | move(sin, sout); 62 | sout.pop(); 63 | } 64 | T front(){ 65 | move(sin, sout); 66 | return sout.top(); 67 | } 68 | T back(){ 69 | move(sout, sin); 70 | return sin.top(); 71 | } 72 | int size(){ 73 | return sin.size()+sout.size(); 74 | } 75 | bool empty(){ 76 | return sin.empty()&&sout.empty(); 77 | } 78 | void move(stack &src, stack &dst){ 79 | while(!src.empty()){ 80 | dst.push(src.top()); 81 | src.pop(); 82 | } 83 | } 84 | 85 | private: 86 | stack sin, sout; 87 | }; 88 | 89 | template 90 | class MyQueue1{ 91 | public: 92 | MyQueue1(){ 93 | 94 | } 95 | ~MyQueue1(){ 96 | 97 | } 98 | void push(T val){ 99 | sin.push(val); 100 | } 101 | void pop(){ 102 | move(sin, sout); 103 | sout.pop(); 104 | } 105 | T front(){ 106 | move(sin, sout); 107 | return sout.top(); 108 | } 109 | T back(){ 110 | move(sout, sin); 111 | return sin.top(); 112 | } 113 | int size(){ 114 | return sin.size()+sout.size(); 115 | } 116 | bool empty(){ 117 | return sin.empty()&&sout.empty(); 118 | } 119 | void move(stack &src, stack &dst){ 120 | if(dst.empty()){ 121 | while(!src.empty()){ 122 | dst.push(src.top()); 123 | src.pop(); 124 | } 125 | } 126 | } 127 | 128 | private: 129 | stack sin, sout; 130 | }; 131 | int main(){ 132 | MyStack st; 133 | for(int i=0; i<10; ++i){ 134 | st.push(i); 135 | } 136 | cout< q; 148 | MyQueue1 q1; 149 | for(int i=0; i<10; ++i){ 150 | q.push(i); 151 | q1.push(i); 152 | } 153 | 154 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int maxn = 100; 7 | 8 | typedef struct BST{ 9 | int key; 10 | BST *lchild, *rchild, *parent; 11 | }BST; 12 | BST *head, *p, node[maxn]; 13 | int cnt; 14 | 15 | void init(){ 16 | head = p = NULL; 17 | cnt = 0; 18 | memset(node, '\0', sizeof(node)); 19 | } 20 | 21 | void insert(BST* &head, int x){ 22 | if(head == NULL){ 23 | node[cnt].key = x; 24 | node[cnt].parent = p; 25 | head = &node[cnt++]; 26 | return; 27 | } 28 | p = head; 29 | if(head->key > x) 30 | insert(head->lchild, x); 31 | else 32 | insert(head->rchild, x); 33 | } 34 | 35 | void inorderTraver(BST *head){ 36 | if(head == NULL) return; 37 | inorderTraver(head->lchild); 38 | cout<key<<" "; 39 | inorderTraver(head->rchild); 40 | } 41 | 42 | BST* search(BST *head, int x){ 43 | if(head == NULL) return NULL; 44 | if(head->key == x) return head; 45 | else if(head->key > x) 46 | search(head->lchild, x); 47 | else 48 | search(head->rchild, x); 49 | } 50 | 51 | BST* minimum(BST *head){ 52 | if(head == NULL) return NULL; 53 | while(head->lchild != NULL) 54 | head = head->lchild; 55 | return head; 56 | } 57 | 58 | BST* maximum(BST *head){ 59 | if(head == NULL) return NULL; 60 | while(head->rchild != NULL) 61 | head = head->rchild; 62 | return head; 63 | } 64 | 65 | BST* successor(BST *head){ 66 | if(head->rchild != NULL) 67 | return minimum(head->rchild); 68 | BST *y = head->parent; 69 | while(y!=NULL && y->rchild==head){ 70 | head = y; 71 | y = y->parent; 72 | } 73 | return y; 74 | } 75 | 76 | BST* predecessor(BST *head){ 77 | if(head->lchild != NULL) 78 | return maximum(head->lchild); 79 | BST *y = head->parent; 80 | while(y!=NULL && y->lchild==head){ 81 | head = y; 82 | y = y->parent; 83 | } 84 | return y; 85 | } 86 | 87 | void delet(BST *z){ 88 | if(z->lchild==NULL && z->rchild==NULL){ 89 | if(z==head) head = NULL; 90 | else if(z->parent->lchild == z) 91 | z->parent->lchild = NULL; 92 | else 93 | z->parent->rchild = NULL; 94 | } 95 | 96 | else if(z->lchild==NULL || z->rchild==NULL){ 97 | if(z==head){ 98 | if(z->lchild) head = z->lchild; 99 | else head = z->rchild; 100 | head->parent = NULL; 101 | } 102 | else{ 103 | if(z->parent->lchild==z && z->lchild){ 104 | z->parent->lchild = z->lchild; 105 | z->lchild->parent = z->parent; 106 | } 107 | else if(z->parent->lchild==z && z->rchild){ 108 | z->parent->lchild = z->rchild; 109 | z->rchild->parent = z->parent; 110 | } 111 | else if(z->parent->rchild==z && z->lchild){ 112 | z->parent->rchild = z->lchild; 113 | z->lchild->parent = z->parent; 114 | } 115 | else{ 116 | z->parent->rchild = z->rchild; 117 | z->rchild->parent = z->parent; 118 | } 119 | } 120 | } 121 | 122 | else{ 123 | BST *s = predecessor(z); 124 | z->key = s->key; 125 | if(s->parent == z) 126 | s->parent->lchild = s->lchild; 127 | else 128 | s->parent->rchild = s->lchild; 129 | if(s->lchild) 130 | s->lchild->parent = s->parent; 131 | } 132 | } 133 | int main(){ 134 | freopen("BST.in", "r", stdin); 135 | init(); 136 | int x; 137 | while(cin>>x) 138 | insert(head, x); 139 | inorderTraver(head); 140 | cout<key<key<