Interview question-solve the problem of finding the maximum length of a substring that contains another string
Question: Find the maximum substring in the test string contained in the string text, for example, text = "abcgfdd", test = "hhmcgreen", the maximum substring is cgf, the length is 3. My solution is as follows: Find the sub-string of text from the largest to the smallest, and use kmp to compare whether the Sub-string is in text. If the sub-string is the largest, return the length. The Code is as follows:
Int maxsublength (string s1, string s2) {int ls1 = s1.length (); int ls2 = s2.length (); int maxsublen = ls1; if (ls2 <ls1) maxsublen = ls2; while (maxsublen> 0) {for (I = 0; I + maxsublen <= ls1; I ++) {if (kmp (s2, s1.substr (I, maxsublen ))! = 0) // whether it is a substring. If it is a substring, return maxsublen;} -- maxsublen;} return 0;} void getnext (string * t, int next []) {int j, k; j = 0; k =-1; next [0] =-1; while (j <t-> length ()) {if (k =-1 | t [j] = t [k]) // if it is equal or the first one, add 1 {j ++; k ++; next [j] = k;} // next Else k = next [k];} // while} int KMPindex (string * s, string * t) {// int stp = t-> length (); int next [max]; int I, j; getnext (t, next); I = 0; j = 0; while (I <s-> length () & j <t-> length ()) if (j =-1 | s [I] = t [j]) // the corresponding characters are the same, and the pointer is moved back to a position {I ++; j ++ ;} else // if it fails to match j = next [j] From t [next [j] of the substring; // while if (j> = t-> length ()) return I-t-> length () + 1; // the position where the first character appears. else return 0 is returned if the match is successful. // If the match is unsuccessful, the return value is zero}
Teach, Information Question, give a string with different numbers of substrings
This scale of violence is obviously hopeless.
The key to this question is the order of numbers. If you have obtained the lcp of each suffix, the number can be as follows:
Use s [I. n] to indicate the suffix starting with the I-bit of the string s (the subscript starts from 1 ).
Assume that we know the number of all substrings of s [I + 1 .. n] u [I + 1 ],
U [I] = u [I + 1] + (n-I + 1)-lcp [I]
Apparently u [n] = 1
That is to say, from the end of the string, add characters one by one, and count the number of substrings. u [1] is the result.
The maximum number of consecutive increments of a string s.
Only one digit?
It can still be understood as 1112 .?
Can there be spaces in the string?
What is the maximum length of a string?