#TP0000005. Tiền tố là dãy con (Prefiquence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tiền tố là dãy con (Prefiquence)
Prefiquence
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two binary strings and , consisting only of 0 and 1. The length of is , and the length of is .
For an integer with , consider the prefix of length of , namely its first characters. This prefix is a subsequence of if it can be obtained by deleting zero or more characters from without changing the order of those left. The selected characters need not be consecutive. The empty prefix () is always permitted.
Find the greatest possible for which this condition holds.
Input
- The first line contains two integers and , the lengths of and .
- The second line contains the binary string , with exactly characters.
- The third line contains the binary string , with exactly characters.
Output
Print one integer : the maximum length of a prefix of that is a subsequence of .
Subtasks
- Subtask 1 (20 points): ; ; , ; every character in both strings is
0or1. - Subtask 2 (30 points): ; ; , ; every character in both strings is
0or1. - Subtask 3 (50 points): ; ; , ; every character in both strings is
0or1.
Examples
Input
5 4
10011
1110
Output
2
Explanation
Here 10011 and 1110. The first character 1 of can be matched to the first 1 in ; the next character 0 can be matched to the last character of . Thus 10 (length ) is a subsequence. To extend to length , we would need another 0 after that match, but no characters remain. Hence the answer is 2.