1 / 36

SE106 Exercise Class 3

SE106 Exercise Class 3. Quiz & Survey Homework 4 Homework 5. 2014.10.29. Quiz. 2. 密码推荐 输入一个表示用户名的字符串 推荐一个由大小写字母、数字和特殊字符组成的密码 要求相同的用户名总是给出相同的密码,不同的用户名密码要尽可能不通. 1. 算式 8 个数字 8 ,使用加减乘除(含括号)运算符连接,得到值为 100 的表达式,求出所有这样的表达式。 可以假定没有浮点误差 可以不考虑效率问题. Survey. 对于程序设计 II 你有 什么意见和建议。 哪些方面比较令人满意

Download Presentation

SE106 Exercise Class 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SE106 Exercise Class 3 Quiz & Survey Homework 4 Homework 5 2014.10.29

  2. Quiz • 2. 密码推荐 • 输入一个表示用户名的字符串 • 推荐一个由大小写字母、数字和特殊字符组成的密码 • 要求相同的用户名总是给出相同的密码,不同的用户名密码要尽可能不通 • 1. 算式 • 8个数字8,使用加减乘除(含括号)运算符连接,得到值为100的表达式,求出所有这样的表达式。 • 可以假定没有浮点误差 • 可以不考虑效率问题

  3. Survey • 对于程序设计II你有什么意见和建议。 • 哪些方面比较令人满意 • 那些方面存在不足

  4. 表达式的穷举 • function find(set of expression S) If Size(S) == 1 and 100 in S then report solution for each element x in S for each element y in S have x <> y for each operator op S’ = S -{x, y} + {op(x, y)} find(S’) • Generate expression

  5. 密码推荐 • 先将输入的字符串散列成一个整数 • 用这个整数初始化srand • 使用rand生成密码序列 • 也可换用其它的随机数生成器,或自行实现

  6. 字符串散列函数 • 将字符串散列为整数 • 运算速度快(运算简单) • 散列值分布均匀,雪崩效应

  7. Fowler-Noll-Vo hash function • hash = FNV_offset_basis • for each octet_of_data to be hashed hash = hash * FNV_prime hash = hash XOR octet_of_data • return hash

  8. FNV_1a hash • hash = FNV_offset_basis • for each octet_of_data to be hashed hash = hash XOR octet_of_data hash = hash * FNV_prime • return hash • Better avalanche characteristics • http://www.isthe.com/chongo/tech/comp/fnv/

  9. Homwork 4 王若愚

  10. K-Queens Problem • We are going to place K Queens on a MxN chessboard, such that no queen will fight with others. • Please figure out the maximum K and show one valid plan.

  11. Solutions • Find K in a direct way. • Take advantage of math short cuts.

  12. Direct Way • Put first queen on the board, then find a valid position for a second queen, then the third, fourth... Until we failed to find a valid position to place the (K+1)th queen. • Low efficiency.

  13. Short Cuts • Assert: K <= min(M, N); • In fact, when M >= 4 and N >= 4, K = min(M, N).

  14. 5130309018

  15. 5 1 1 0 3 7 9 0 8 0

  16. Other Problems • Not following the instruction. • Chessboard unset. • Return value is always 0 or -1 • write one's own main() function in KQueens.cpp. • Abandoned KQueens() function • Rewrite class "Chessbaord"

  17. Other Problems

  18. Other Problems

  19. Other Problems • Compiling failure • Not following the instruction • namespace, header file... • Redundent output

  20. Other Problems

  21. Summery • Good for most of you • Keep up!

  22. Homework 5 魏星达 some slides are borrowed from class ppt

  23. 0 • 1 • 2 • 3 • 4 merge sort • /* • * The merge sort algorithm consists of the following steps: • * • * 1. Divide the vector into two halves. • * 2. Sort each of these smaller vectors recursively. • * 3. Merge the two vectors back into the original one. • */ • void sort(Vector<int> & vec) { • int n = vec.size(); • if (n <= 1) return; • Vector<int> v1; • Vector<int> v2; • for (inti = 0; i < n; i++) { • if (i < n / 2) { • v1.add(vec[i]); • } else { • v2.add(vec[i]); • } • } • sort(v1); • sort(v2); • vec.clear(); • merge(vec, v1, v2); • } • Divide the vector into two halves: v1 and v2. • Sort each of v1 and v2 recursively. • Clear the original vector. • Merge elements into the original vector by choosing the smallest element from v1 or v2 on each cycle.

  24. merge sort • /* • * Function: merge • * --------------- • * This function merges two sorted vectors (v1 and v2) into the • * vector vec, which should be empty before this operation. • * Because the input vectors are sorted, the implementation can • * always select the first unused element in one of the input • * vectors to fill the next position. • */ • void merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2) { • int n1 = v1.size(); • int n2 = v2.size(); • int p1 = 0; • int p2 = 0; • while (p1 < n1 && p2 < n2) { • if (v1[p1] < v2[p2]) { • vec.add(v1[p1++]); • } else { • vec.add(v2[p2++]); • } • } • while (p1 < n1) vec.add(v1[p1++]); • while (p2 < n2) vec.add(v2[p2++]); • }

  25. in-place merge sort • In computer science, an in-place algorithm (or in Latinin situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space.

  26. 23 • 33 • 12 • 23 • 73 • 33 • 15 • 19 • 15 • 19 • 73 • 12 in-place merge sort naive approach • 33 • 19 • 73 • 12 • 15 • 23 • 12 • 12 • 15 • 23 • 19 • 73 • 33 • 23 • 33 • 15 • 19 • 73

  27. in-place merge sort • naive approach • voidinplace_merge(Vector<int> & vec,intstart,int end){ • int middle = (start + end) / 2; • inti = begin; • int j = middle + 1; • while(i <= middle && j <= end){ • if(vec[i] >= vec[j]) { • int temp = vec[j]; • for(int k = j; k > i; k--) • vec[k]=vec[k - 1]; • vec[i] = temp; • middle++; • j++; • } • i++; • } • }

  28. 11 • 23 • 33 • 19 • 12 • 73 • 23 • 73 • 11 • 33 • 19 • 73 • switching blocks • 12 • 12 • 11 • 23 • 19 • 33 • using binary search • recusive merge • 23 • 33 • 73 • 12 • 11 • 19 • 12 • 12 • 11 • 11 • 23 • 23 • 19 • 19 • 73 • 73 • 33 • 33 • in-place merge sort • a better approach

  29. in-place merge sort • a better approach • //find val between [left ... right] inclusive • int _binary_search(intval,vector<int> &vec,intleft,int right) { • int low = left; • int high = right; • while( low <= high){ • int mid = (low + high) / 2; • if(vec[mid] > val) • high = mid - 1; • else • low = mid + 1; • } • return high; • } • using binary sort to find the index

  30. in-place merge sort • a better approach • voidblock_exchange(vector<int> &vec,intl,intm,int r){ • reverse(vec,l,m); • reverse(vec,m+1,r); • reverse(vecr,k,r)); • } • swaping blocks in o(N) time • void reverse(vector<int> &vec,intl,int m) { • while(l < m) { • int temp = vec[m]; • vec[m] = vec[l]; • vec[l] = temp; • l++;m--; • } • }

  31. in-place merge sort • a better approach • voidin_place_merge(vector<int> &vec,ints,intmiddle,int e){ • if(s > middle) • return; • if(middle > e) • return; • int q1 = (s + middle) / 2; • int q2 = _binary_search(vec[q1], vec, middle+1, e); • int q3 = q1 + q2 - middle; • block_exchange(vec, q1, middle, q2); • in_place_merge(vec, s, q1-1, q3-1); • in_place_merge(vec, q3+1, q2, e); • }

  32. in-place merge sort • a better approach • voidin_place_merge_sort(vector<int> &vec,ints,int e) { • if(s < e) { • int pivot = (s+e) / 2; • in_place_merge_sort(vec, s, pivot); • in_place_merge_sort(vec,pivot+1,e); • in_place_merge(vec, s,pivot, e); • } • } • Finally..

  33. Complexity • in_place merge: O(N log N) • T(n) = 2 T(N/2) + O(N log N) • Which is the known best.(according to http://wikipedia.org)

  34. Conclusions • 大部分同学都实现了第一种in_place的sort并做了随机数据的测试。 • 少数同学实现了更好的解法。

  35. Conclusions

  36. Conclusions

More Related