1. Topic description
Find the longest common substring of two strings a, b. If there are more than one, output the one that appears first in the shorter string.
Note: Definition of substring: A string formed by deleting the prefix and suffix (or not) from a string. Please separate from the concept of "subsequence"!
Data range: string length 1≤length≤300.
2. Enter description
Enter two strings.
3. Output description
Returns repeated occurrences of characters.
4. Problem-solving ideas
- First read the two input strings;
- Determine which string is shorter, use it as a short string, and the other string as a long string;
- Get the length of short string and long string;
- Initialize the variables maxLen and start, which are used to record the length and starting position of the longest common substring respectively;
- Use two layers of loops, the outer loop traverses the short string, and the inner loop traverses the substrings in the short string;
- In each inner loop, judge whether the current substring is a substring of a long string, and compare whether its length is greater than the maximum length of the previous record;
- If the conditions are met, update the maximum length maxLen and the starting position start;
- After the loop ends, the longest common substring is extracted from the short string according to the maximum length and the starting position, and output.