PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000005. Tiền tố là dãy con (Prefiquence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tiền tố là dãy con (Prefiquence)

Prefiquence

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two binary strings aa and bb, consisting only of 0 and 1. The length of aa is nn, and the length of bb is mm.

For an integer kk with 0≤k≤n0\le k\le n, consider the prefix of length kk of aa, namely its first kk characters. This prefix is a subsequence of bb if it can be obtained by deleting zero or more characters from bb without changing the order of those left. The selected characters need not be consecutive. The empty prefix (k=0k=0) is always permitted.

Find the greatest possible kk for which this condition holds.

Input

  • The first line contains two integers nn and mm, the lengths of aa and bb.
  • The second line contains the binary string aa, with exactly nn characters.
  • The third line contains the binary string bb, with exactly mm characters.

Output

Print one integer kk: the maximum length of a prefix of aa that is a subsequence of bb.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤2000001\le n,m\le 200000; n+m≤14n+m\le 14; ∣a∣=n|a|=n, ∣b∣=m|b|=m; every character in both strings is 0 or 1.
  • Subtask 2 (30 points): 1≤n,m≤2000001\le n,m\le 200000; n+m≤100n+m\le 100; ∣a∣=n|a|=n, ∣b∣=m|b|=m; every character in both strings is 0 or 1.
  • Subtask 3 (50 points): 1≤n,m≤2000001\le n,m\le 200000; n+m≤400000n+m\le 400000; ∣a∣=n|a|=n, ∣b∣=m|b|=m; every character in both strings is 0 or 1.

Examples

Input

5 4
10011
1110

Output

2

Explanation

Here a=a= 10011 and b=b= 1110. The first character 1 of aa can be matched to the first 1 in bb; the next character 0 can be matched to the last character of bb. Thus 10 (length 22) is a subsequence. To extend to length 33, we would need another 0 after that match, but no characters remain. Hence the answer is 2.