One, increment matrix problem
Description of the problem: as shown, in a m*n matrix, each row is sorted from left to right in ascending order, and each column is sorted in ascending order from top to bottom. Complete a function to print the matrix into a one-dimensional array in order from small to large. , you should print out 1,2,2,4,4,6,7,8,8,9,9,10,11,12,13,15.
Second, no repetition of the oldest string problem (citation Luxiaoxun blog)
Problem Description: finds a contiguous substring in a string that cannot have any two characters in the same substring, and the substring is the longest that meets the requirements. For example, enter "ABCBEF" and Output "CBEF".
Problem Solving: Method One: Exhaustive method, using 2-heavy outer loop to traverse all the intervals, with 2-heavy inner loop to check whether the substring meets the "no repetition character" requirement. Where the outer loop i, J traverse all subscript, M, n is the inner loop, check whether the interval [i,j] meets the requirements. Space complexity is O (1), Time complexity O (n^4).
O (n^4) time complexity int max_unique_substring1 (char * str) { int maxlen = 0; int begin = 0; int n = strlen (str); for (int i=0, i<n; ++i) for (int j=1; j<n; ++j) { int flag = 0; for (int m=i, m<=j; ++m) {for (int n=m+1; n<j; ++n) { if (str[n] = = Str[m]) { flag = 1 ; break; } } if (flag = = 1) break; } if (flag==0 && J-i+1>maxlen) { maxlen = j-i+1; begin = i; } } printf ("%.*s\n", MaxLen, &str[begin]); return maxlen; }
Method Two: The test substring of method one is "no repeating character" to improve, using the hash table to record whether the characters have occurred.
O (n^2) time complexity int max_unique_substring2 (char * str) { int i,j; int begin; int maxlen = 0; int hash[256]; int n = strlen (str); for (i=0; i<n; ++i) { memset (hash,0,sizeof (hash)); Hash[str[i]] = 1; for (j=i+1; j<n; ++j) { if (hash[str[j] = = 0) hash[str[j]] = 1; else break ; } if (J-i > MaxLen) { maxlen = j-i; begin = i; } } printf ("%.*s\n", MaxLen, &str[begin]); return maxlen; }
Method Three: For the string "Axbdebpqawuva", the following table is constructed:
table, The string has 3 ' a ', has 2 ' B ', and the remainder is a single character. Next[] records the position of the next duplicate character, such as str[0]=str[8]=str[12]= ' a ', at which point Next[0]=8,next[8]=12,next[12]=13, the rest of the same. It is worth noting that next[] stores the subscript for the character Terminator ' \ s ', i.e. 13, for non-repeating characters.
Here, First[i] represents the first occurrence of the repeating character after I. For example, Str[0], the first occurrence of the repeating character is str[5]= ' B ', of course, starting from str[1],str[2]. The repeat character ' a ' will appear to str[12] starting from str[3]. It can be proved that the length of the longest meeting requirement from str[i] is first[i]-i, and the interval is [i,first[i]-1] to be solved. The longest string above is when i=3, first[i]-i=12-3=9. The result string is DEBPQAWUV.
O (N) time complexity int max_unique_substring3 (char * str) { int maxlen = 0; int begin = 0; int n = strlen (str); int * next = (int*) malloc (sizeof (int) *n); Next[i] The position of the next duplicate character with Str[i] is recorded int * first = (int*) malloc (sizeof (int) * (n+1));//first[i] Record Str[i] The closest duplicate point after int hash[256]; memset (hash,n,sizeof (hash)); First[n] = n; for (int i=n-1; i>=0; i--) { Next[i] = hash[str[i]]; Hash[str[i]] = i; if (Next[i] < first[i+1]) first[i] = next[i]; else first[i] = first[i+1];//Generate first[] table, the complexity is O (N) of the } for (int i=0; i<n; i++) { if (first[i ]-i > MaxLen) { maxlen = first[i]-i; begin = i; } } Free (first); Free (next); printf ("%.*s\n", MaxLen, &str[begin]); return maxlen; }
Method Four: Use a suffix array
Construct a suffix array for this string, and in each suffix array, look for the longest prefix with no repeating characters, and the longest prefix is the substring to be searched for.
Gets the longest string of no duplicate prefix lengths int longestlen (char * p) { int hash[256]; int len = 0; memset (hash,0,sizeof (hash)); while (*p &&!hash[*p]) { hash[*p] = 1; + + len; + + P; } return len; } Using the suffix array solution int Max_unique_substring4 (char * str) { int maxlen =-1; int begin = 0; Char *a[99999]; int n = 0; while (*str! = ') ') { a[n++] = str++; } for (int i=0; i<n; i++) { int temlen = Longestlen (A[i]); if (Temlen > MaxLen) { maxlen = Temlen; begin = i; } } printf ("%.*s\n", MaxLen, A[begin]); return maxlen; }
Method Five: DP scheme and other reference http://www.ahathinking.com/archives/123.html
Three, string interleaving problem
Problem Description: Determine if the string C is string A and string B in sequential interleaving (Interleave).
What do you call Interleave? Is the complete interleaving of the two strings, and then the original A and B strings will be split apart.
Problem analysis: A visual illusion: the C string of a pick out, the rest of the section if it is B, which means that C is interleave.
This will encounter an error, such as a= "AA", b= "AB", c= "Aaba". If a is picked out from C, the "AA" will be singled out, and then the remaining part "BA" is not equal to the B string, and the "not Interleave" error will be judged. Such errors are due to the pick-and-pick errors caused by the common characters in A and b two strings.
Case one: If the input string A, B does not have a common character, then each pick, pick it must belong to their own. If you pick up after C still have surplus, or can not finish C on it, stating that C is not interleave.
Case two: If the input string A, B is a common character, then each of the selected, it is possible to choose a mistake, the not to pick away. To avoid this error, consider both cases when you encounter a common character (that is, a character that both sides can ask for).
Here are two directions to solve the problem.
1, recursive method: from the outward (from large to small) to solve the problem.
2, DP method: From the inside outward (from small to large) to solve the problem.
Recursive sub-Problem method:
For the target problem (S1, S2, S3), the recursive sub-problem is two (S1.SUBSTR (1), S2, S3.SUBSTR (1)) and (S1, S2.SUBSTR (1), S3.SUBSTR (1)).
Class Solution {public : bool Isinterleave (string s1, String s2, string s3) { bool R1 = false, r2 = false;
if (S1 = = "" && s2 = = "" && s3 = = "") return true; if (S1! = "" && s3! = "" && s1[0] = = s3[0]) r1 = Isinterleave (S1.substr (1), S2, S3.SUBSTR (1)); if (S2! = "" && s3! = "" && s2[0] = = s3[0]) r2 = Isinterleave (S1, S2.SUBSTR (1), S3.SUBSTR (1)); return R1 | | R2; } };
Dynamic Planning methods:
International practice, first find sub-problem, design sub-problem state amount.
For the target problem (S1, S2, S3), the sub-problem is (the first I character of S1, the first J character of S2, the S3 of the first i+j characters).
Set child problem state amount C[i][j], which represents the state (true or false) of the problem (the first I character of the S1, S2 the first J-character, S3 the first i+j characters). Then C[s1.length ()][s2.length ()] is the state of the target problem.
Initial problem state c[0][0]=true.
State recurrence Relationship: c[i][j] = ((s1[i-1] = = s3[i+j-1]) && c[i-1][j]) | | ((s2[j-1] = = s3[i+j-1]) && c[i][j-1])
Code:
Class Solution {public : bool Isinterleave (string s1, String s2, string s3) { size_t len1, len2; Len1 = S1.length (); Len2 = S2.length (); if (len1 + len2! = S3.length ()) return false; BOOL **c = new bool*[len1+1]; Note * In the middle for (int i=0;i<len1+1;i++) c[i] = new bool[len2+1]; C[0][0] = true; for (int i=1;i<len1+1;i++) c[i][0] = (s1[i-1] = = s3[i-1]) && c[i-1][0] ; for (int j=1;j<len2+1;j++) c[0][j] = (s2[j-1] = = s3[j-1]) && c[0][j-1] ; for (int i=1;i<len1+1;i++) for (int j=1;j<len2+1;j++) c[i][j] = ((s1[i-1] = = s3[i+j-1]) && c[i-1 ][J]) | | ((s2[j-1] = = s3[i+j-1]) && c[i][j-1]); BOOL result = C[len1][len2]; for (int i=0;i<len1+1;i++) delete []c[i]; delete []c; return result; } ;
The time complexity of the DP method is O (MN), the Space Complexity O (MN).
Iv. statistics on the number of online users in a certain time period
Problem Description: Counts how many users are at each point in the specified time period. The time period is the start time 00:00 end time 08:00
For example, a user A's record is represented as User:login_time:logout_time
Sample Data in table
User Login_time Logout_time
A 05:57 09:01
B 07:13 08:00
C 05:00 07:00
Last recorded as [5,7], 2 [7,8], 2 [8,9], 1 instead of [5,6], 2 [6,7], 2 [7,8], 2 [8,9], 1
(not to be continued)
2015 school recruit written interview algorithm summary of the Blue flood test