Creation August: The light of the World
Light of the World
Light. c/cpp/pas
-- When your light is bright enough, you can change errors.
In the world where there is light, there is hope for pregnancy.
LightAugust is a kind girl who enjoys playing with a group of light elements.
Each light dollar has its own name. They may have the same name but only one letter.
The range is a-z. Now LightAugust is playing games with light elements. She makes the light elements in two queues,
Makes them A string A, B.
Now LightAugust wants you to find a light element:
(1) A is A shortest substring. It is not A substring of B.
(2) A is A shortest substring. It is not A subsequence of B.
Of course, he doesn't want you to output these two strings, but he wants you to output the answer strings for the two questions separately.
Length.
Input
Two rows. Each row has one string, which is A string and B.
Output
The length of the answer string for each question is output. If there is no answer that meets the requirements, output "Orz TA ".
Data range: Hint
The length of 30%, A, and B cannot exceed 10.
The length of A, B cannot exceed 100%.
Sample Input
Aabbcc
Abcabc
Sample Output
2
4
The title adapted by the great god of Creation August is very literary. The idea and code provided by TA's great gods can save me from this brute force hacker attack.
When I first saw this question, I suddenly thought of the first question to use kmp for matching, and the second question to use greedy, but this ruined the kmp matching algorithm, because if kmp is used in the first question, You need to perform self-matching on each string and then match string B. This time complexity cannot be tolerated.
So we can associate the first question. We can think of a very basic Longest Common substring, F [I] [j] String a I In this position, string B J The longest Public String at this location is easy to get, If (a [I] = B [j]) f [I] [j] = f [I −1] [j −1] + 1
If (a [I]! = B [j]) f [I] [j] = 0
In this way, F [I] The maximum value in a indicates the maximum length of a string matching from the position I, as long as it is 1 larger than this length, it starts from the position I, it cannot be matched in string B. So you only need to enumerate I and then select the minimum value in the maximum value.
The second question is basically no problem. Each time you determine an initial position in string a, you are greedy, that is, you should select matching characters as far as possible in string B, this greedy policy must be established.
Appendix TA ye code (prohibited for any commercial purposes ):
# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> using namespace std; int f [2005] [2005], Max; char A [2005], B [2005]; int main () {freopen ("light. in "," r ", stdin); freopen (" light. out "," w ", stdout); int alen, blen, I, j, ans = 3000, tmp; scanf (" % s ", A + 1, B + 1); // read alen = strlen (A + 1), blen = strlen (B + 1); // record length for (I = 1; I <= alen; ++ I) {tmp = 0; for (j = 1; j <= blen; ++ j) {if (A [I] = B [j]) f [I] [j] = f [I-1] [J-1] + 1; tmp = max (Tmp, f [I] [j]);} if (tmp! = I) ans = min (ans, tmp);} // first, perform the longest Public String and obtain the maximum and minimum if (ans = 3000) puts ("Orz TA"); else printf ("% d \ n", ans + 1); memset (f, 0, sizeof (f); ans = 3000; for (I = 1; I <= alen; ++ I) {Max = tmp = 0; for (j = 1; j <= blen; ++ j) {if (A [I] = B [j]) if (I = 1) f [I] [j] = 1; else f [I] [j] = Max + 1; if (I! = 1) Max = max (Max, f [I-1] [j]); tmp = max (tmp, f [I] [j]);} // greedy if (tmp! = I) ans = min (ans, tmp);} if (ans = 3000) puts ("Orz TA"); else printf ("% d \ n ", ans + 1 );}'
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.