2015 written summaries of schools recruit written interviews blue flood communication algorithm

Source: Internet
Author: User
Tags repetition

One, ascending matrix problem

Description of the problem: for example, see the following number, in a m*n matrix, each row is sorted from left to right in ascending order. Each column is sorted in ascending order from top to bottom. Complete a function to print this matrix into a one-dimensional array in order from small to large.

。 You should print out the 1,2,2,4,4,6,7,8,8,9,9,10,11,12,13,15.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcm9tyw5jzwdpcmxz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Second, no repetition of the oldest string problem (citation Luxiaoxun blog)

Description of the problem: find a continuous substring in a string that cannot have any two characters in the same substring. And this 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 internal cycle to verify whether the substring meets the "no repetition character" requirement.

When the Chinese and foreign layer loops 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 repetition 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. There are 3 ' a ' characters in the string. There are 2 ' B ' and the rest is a single character. Next[] records the position of the next repeated character, such as str[0]=str[8]=str[12]= ' a ', when next[0]=8. Next[8]=12,next[12]=13, the rest of the same.

It's worth noting that. For non-repeating characters. Next[] Stores the subscript of the character Terminator ' \ S ', which is 13.
Here, First[i] indicates the position of the first occurrence of the repeated character after I.

Like what. Str[0] later. The first occurrence of repeated characters is str[5]= ' B ', of course, from str[1],str[2]. and start with str[3]. The repeat character ' a ' appears only until str[12].

Can prove. The length of the longest meeting requirement from str[i] is first[i]-i. 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] records the position of the next and Str[i] repeated characters      int * first = (int*) malloc (sizeof (int) * (n+1));//first[i] Record Str[i] A recent iteration      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

Constructs a suffix array for this string. In each suffix array, look for the longest prefix without repeated characters, and the longest prefix is the substring to be searched for.

Gets the longest string without repeated prefix length  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 references http://www.ahathinking.com/archives/123.html

Three, string interleaving problem

Description of the problem: infer if the string C is string A and string B are interleaved (interleave) sequentially.


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: An intuitive illusion: to pick out a C string. The rest of the assumptions are B, which means that C is interleave.


This will encounter an error, for example a= "AA", b= "AB", c= "Aaba".

Assume that a is picked out from C. Will directly pick out the beginning of the "AA", and then the rest of the "BA" is not equal to the B string, will make "not Interleave" error inference. This error is due to the pick-and-pick error caused by A and b two strings that have co-owned characters.


Scenario One: Assume that there are no commonly-owned characters in the input string A and B. So each pick, pick it must belong to their own.

Assuming that after picking out c there is still surplus, or can not finish the C is gone, stating that C is not interleave.


Scenario Two: Assume that the input string A, B is a common ownership of characters, then each of the selected, it is very likely to pick up the wrong, to take the pick away. To avoid this error, when encountering a co-owning character (that is, both sides can ask for characters). Consider both cases separately.
Here are two ways to solve this 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. First find sub-problem, design sub-problem state amount.
For the target problem (S1, S2, S3). The sub-problem is (S1 's first I-character, S2 's first J-character, S3 's first i+j character).
Set sub-problem state amount C[i][j], which represents the problem (the first I-character of a s1. The state (true or false) of the S2 's first J-character, S3 's first i+j character).

Then C[s1.length ()][s2.length ()] is the state of the target problem. The
initial problem state c[0][0]=true.
Status recursion 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

Description of the problem: count the number of users at each time 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

The sample data is shown in the 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

adjourned

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

2015 written summaries of schools recruit written interviews blue flood communication algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.