Prelude
For the previous chapter, see this: programmer's preference: Chapter 1, left rotation string. All the code and implementation of all ideas that appear in this chapter are not available on the Internet until now.
The ideas in this article can be thought of intelligently. The mastermaster has also contributed to the clever ideas. If you have better ideas, please feel free to provide them. If you have any suggestions for this fantasy series, you are welcome to share your feedback on Weibo. Anyone. If you have any questions, please feel free to correct them.
If this crazy series is helpful to you, I will be very happy and will give me the motivation to stick to it forever. Thank you.
Section 1: whether or not two strings contain
1.0. Question description:
Assume that there is a character string consisting of letters. Assume that there is another character string, and the number of letters in the string is relatively small. In terms of algorithms, how can we quickly find all the letters in a small string in a large string?
For example, if it is the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOM
The answer is true, and all the letters in string2 also have string1.
For the following two strings:
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOZ
The answer is false because the Z letter in the second string is not in the first string.
Comments:
1. The title description is long, but the meaning of the question is simple and clear. It is to give two long and short strings A and B. Suppose A is long and B is short. Now, do you want to determine whether B is included in string A, that is, B? (-.
2. Although the meaning of the question is simple, it is not easy to implement it. If the interviewer is pressed to reject the methods you can think of one by one and wants you to provide a better and best solution, you may have to worry a lot.
Okay. Before you continue reading, you 'd better take a few minutes to see what the best solution you can think of is, and whether it is consistent with the final implementation method in this article.
1.1. O (n * m) Round Robin Method
Determine if the character in string2 is in string1? :
String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOM
The most intuitive and simplest way to determine whether a string is in another string is to poll and compare each character in the second string string2 one by one with each character in the first string string1, check whether it is in the first string string1.
Assume that n is the length of string string1 and m is the length of string string2. Therefore, this algorithm requires O (n * m) operations. In the example above, in the worst case, there will be 16x8 = 128 operations.
We can easily write the following code:
View plaincopy to clipboardprint?
# Include <iostream>
Using namespace std;
Int compare (string str1, string str2)
{
For (int I = 0; I <str2.length (); I ++)
{
For (int j = 0; j <str1.length (); j ++) // O (n * m)
{
If (str2 [I] = str1 [j]) // one-to-one comparison
{
Break;
}
}
If (j = str1.length ())
{
Cout <"false" <endl;
Return 0;
}
}
Cout <"true" <endl;
Return 1;
}
Int main ()
{
String str1 = "ABCDEFGHLMNOPQRS ";
String str2 = "DCGSRQPOM ";
Compare (str1, str2 );
Return 0;
}
The time complexity of the above Code is O (n * m). Obviously, the time overhead is too large, and we need to find a better way.
1.2. O (mlogm) + O (nlogn) + O (m + n) sorting method
A better solution is to sort the letters of the two strings and Round Robin the two strings at the same time. The sorting of two strings requires (in general cases) O (m log m) + O (n log n) operations, and the subsequent linear scanning requires O (m + n) operations.
Taking the above strings as an example, we will need to add the 16*4 + 8*3 = 88 plus the 16 + 8 = 24 operation for linear scanning of the two strings. (As the length of a string increases, you will find that the algorithm is better and better)
We use the most commonly used quick sorting method for the sort method. The following code for quick sorting is previously written and is easy to understand, I insist that the qsort code of the library function is not used. The only problem is that the Code previously written is to sort integers. However, it is difficult to change the parameter slightly, as shown below:
View plaincopy to clipboardprint?
// Copyright @ 2011 July & yansha
// July, updated, 2011.04.23.
# Include <iostream>
# Include <string>
Using namespace std;
// Make the previous comment retained
Int partition (string & str, int lo, int hi)
{
Int key = str [hi]; // use the last element, data [hi] as the primary element.
Int I = lo-1;
For (int j = lo; j {
If (str [j] <= key)
{
I ++;
Swap (str [I], str [j]);
}
}
Swap (str [I + 1], str [hi]); // it cannot be changed to swap (& data [I + 1], & key)
Return I + 1;
}
// Call the preceding partition procedure recursively to complete sorting.
Void quicksort (string & str, int lo, int hi)
{
If (lo {
Int k = partition (str, lo, hi );
Quicksort (str, lo, k-1 );
Quicksort (str, k + 1, hi );
}
}
// Compare, the above sorting O (m log m) + O (n log n), plus the following O (m + n ),
// The total time complexity is: O (mlogm) + O (nlogn) + O (m + n ).
Void compare (string str1, string str2)
{
Int posOne = 0;
Int posTwo = 0;
While (posTwo <str2.length () & posOne <str1.length ())
{
While (str1 [posOne] <str2 [posTwo] & posOne <str1.length ()-1)
PosOne ++;
// If it is equal to str2. It can be moved only when it is smaller than str2.
If (str1 [posOne]! = Str2 [posTwo])
Break;
// PosOne ++;
// When merging, str1 [str1Pos] = str [str2Pos] can only be str2Pos ++, and str1Pos cannot be auto-incrementing.
// Thanks for correcting helloword.
PosTwo ++;
}
If (posTwo = str2.length ())
Cout <"t