Microsoft's interview questions:
For example, abcbcbcabc, the most frequently-occurring string is BC.
I. Consider the boundary issue.
2. Optimizing Cartesian product combinations,
In general, I think this is the continuous combination set of vertically cut strings, and compare the elements of the Set in one-to-one horizontal jump.
Example: abcbcabc
1. vertical cutting:
To get a combination of all strings, note: the maximum number of consecutive substrings is required here, which is actually the principle of optimizing Cartesian Product and the boundary.
A string contains 8 digits. The length of the substring is 1. It is cut from the first part of the string and is called cut:
1 ---- start from a: (the string is abcbcabc)
Cut out the substring A for the first time, and get: A and bcbcabc,
Cut out the AB substring for the second time, and get AB and cbcabc,
Cut out the ABC substring for the third time, and get: ABC and bcabc,
Cut out the abcb substring for the fourth time, And get: abcb and CABC,
Cut out the abcbc substring for the fifth time, And get: abcbc and ABC,
Cut out the abcbca sub-string for the sixth time, And get: abcbca and BC,
Cut out the abcbcab substring for the seventh time, And get: abcbcab and C,
Cut out the abcbcabc substring for the eighth time, And get: abcbcabc,
Get the array of A1 sets (and it is an array)
Element: a, AB, ABC ,......
2 --- start from B: (the string is abcbcabc)
Cut out the substring B for the first time, and get: B and cbcabc,
Cut out the BC substring for the second time and get: BC and bcabc,
Cut out the BCB substring for the third time, and get: BCB and CABC,
Cut out the bcbc substring for the fourth time, And get: bcbc and ABC,
Cut out the bcbca sub-string for the fifth time and get: bcbca and BC,
Cut out the bcbcab substring for the sixth time, And get: bcbcab and C,
Cut out the bcbcabc substring for the seventh time, And get: bcbcabc
Obtain the B2 set array (and it is an array)
Element: B, BC, BCB ,......
3 --- switch from C: (the string is abcbcabc)
Cut out the C substring for the first time and get: C and bcabc,
Cut out the CB substring for the second time, and get: CB and CABC,
Cut out the CBC substring for the third time, and get: CBC and ABC,
Cut out the cbca sub-string for the fourth time and get: cbca and BC,
Cut out the cbcab substring for the fifth time and get: cbcab and C,
Cut out the cbcabc substring for the sixth time.
Get the B3 set array (and it is an array)
Element: C, CB, CBC ,......
4 ---- start from B: (the string is abcbcabc)
Cut out the substring B for the first time,
Cut out the BC substring for the second time and get: BC and ABC,
Cut out the BCA substring for the third time, and get: BCA and BC,
Cut out the bcab substring for the fourth time and get: bcab and C,
Cut out the bcabc substring for the fifth time.
Obtain the array of B4 sets (and it is an array)
Element: B, BC, BCA ,......
5 ---- start with C: (the string is abcbcabc)
Cut out the C substring for the first time and get: C and ABC,
For the second time, cut out the CA sub-string and obtain Ca and BC,
Cut out the cab substring for the third time, and get: cab and C,
Cut out the CABC substring for the fourth time.
Obtain the C5 collection array (and it is an array)
Element: C, CA, cab ,......
6 ---- start from a: (the string is abcbcabc)
Cut out the substring A for the first time, and get: A and BC,
Cut out the AB sub-string for the second time, and get AB and C,
Cut out the ABC substring for the third time and get: ABC,
Get the A6 collection array (and it is an array)
Element: a, AB, ABC
7 ---- start from B: (the string is abcbcabc)
Cut out the substring B for the first time, and get: B and c,
Cut out the BC substring for the second time and get: BC,
Returns the array of the B7 set (and it is an array)
Element: B, BC
8 ---- start with C: (the string is abcbcabc)
Cut out the C substring for the first time and get: c
Get the C8 collection array (and it is an array)
Element: c
2. Horizontal ratio:
Save all the cut points of A in the cut order to an array called the A1 set (and it is an array)
Save all the cut points of B in the cut order to the B2 set array (and it is an array)
... And so on.
Get the following eight sets: (the string is abcbcabc)
Number of rows/columns 1 2 3 4 5 6 7 8
1 A1: A, AB, ABC, abcb, abcbc, abcbca, abcbcab, abcbcabc;
2 B2: B, BC, BCB, bcbc, bcbca, bcbcab, bcbcabc;
3 C3: C, CB, CBC, cbca, cbcab, cbcabc;
4 B4: B, BC, BCA, bcab, bcabc;
5 C5: C, CA, cab, CABC;
6 A6: A, AB, ABC;
7 B7: B, BC;
8 C8: C;
Set A1 and B2... And other sets for horizontal comparison:
To compare column 1, column 2 is compared with Skip 1, column 3 is compared with Skip 2, and column 3 is compared with Skip 3 .... To the end; because the maximum number of consecutive sub-strings is required, you need to skip!
Obtain the maximum number of records of the same string, that is, obtain the substrings with the most occurrences.
Comparison Method:
As mentioned above, the maximum number of consecutive substrings is required. In fact, it is the principle of optimizing cartesian products and the boundary. So what we need to do is compare all sets one-to-one, not many-to-many or others (simpler reason: the number of digits is different and there is no need to compare ).
It is meaningless to compare the multiple-seat strings in one-to-one mode. For example, if AB is a BC comparison between A and B sets. You need to skip the comparison (and let's talk about it like this ). There are regular jumps. Obviously, I will not talk about it.
The reason for vertical cutting is to solve the optimization problem caused by horizontal comparison.
# Include <iostream> # include <string> # include <vector> using namespace STD; pair <int, string> fun (const string & Str) {vector <string> substrs; int maxcount = 1, Count = 1; string substr; int I, Len = Str. length (); for (I = 0; I <Len; ++ I) {substrs. push_back (Str. substr (I, len-I); // put abcbcbcabc, bcbcbcabc, cbcbcbcabc, bcbcabc, cbcabc, bcabc, CABC, ABC, BC, c put in the container once} for (I = 0; I <Len; I ++) {for (Int J = I + 1; j <Len; j ++) {COUNT = 1; if (substrs [I]. substr (0, J-I) = substrs [J]. substr (0, J-I) // a single character is compared before the first cycle, and then two, three characters are compared {++ count; for (int K = J + (J-I); k <Len; k + = J-I) {If (substrs [I]. substr (0, J-I) = substrs [K]. substr (0, J-I) // if the previous comparison is successful, the next hop processing + + count; else break;} If (count> maxcount) {maxcount = count; substr = substrs [J]. substr (0, J-I) ;}}return make_pair (maxcount, substr) ;}int main () {string STR; pair <int, string> RS; while (CIN> Str) {rs = fun (STR); cout <Rs. second <Rs. first <'\ n';} return 0 ;}
Find the substring with the most consecutive occurrences in a string, for example, abcbcbcabc. The substring with the most consecutive occurrences in this string is BC, which appears three times.
The following is my implementation code, which is implemented in C language and has been compiled.
#include "stdio.h"#include "string.h" int count = 0; char sub_str[256]; void find_str(char *str) { int str_len = strlen(str); int i, j, k; int tmp_cnt = 0; for (i = 0; i < str_len; i++) { for (j = i+1; j < str_len; j++) { int n = j-i; //sub string length tmp_cnt = 1; if (strncmp(&str[i], &str[j], n) == 0) //compare n-lengths strings { tmp_cnt++; //they are equal, so add count for (k = j+n; k < str_len; k += n) //consecutive checking { if (strncmp(&str[i], &str[k], n) == 0) { tmp_cnt++; } else break; } if (count < tmp_cnt) { count = tmp_cnt; memcpy(sub_str, &str[i], n); //record the sub string } } } } } int main() { char *str = "abcbcbcabc"; find_str(str); printf("%d, %s\n", count, sub_str); return 0; }