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

#BS0000468. Số ký tự có thể xóa nhiều nhất (Maximum Number of Removable Characters)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số ký tự có thể xóa nhiều nhất (Maximum Number of Removable Characters)

Maximum Number of Removable Characters

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given strings ss and pp, the latter can initially be formed by deleting letters from ss without reordering. Positions of ss start at zero. An ordered list r0,…,rm−1r_0,\ldots,r_{m-1} identifies deletable positions. Choose 0≤k≤m0\le k\le m and delete the first kk listed original positions. Maximize kk while retaining pp as a subsequence.

Input

The first and second lines contain ss and pp. The third line contains integer mm. The fourth line contains mm integers r0,…,rm−1r_0,\ldots,r_{m-1} (an empty line if m=0m=0).

Output

Print the maximum number of allowed deletions.

Subtasks

  • Subtask 1 (20 points): ∣p∣=1|p|=1; 1≤∣p∣≤∣s∣=n≤1051\le |p|\le |s|=n\le 10^5; 0≤m<n0\le m<n; 0≤ri<n0\le r_i<n (0≤i<m0\le i<m); all rir_i are distinct; s,ps,p use lowercase English letters; pp is a subsequence of ss.
  • Subtask 2 (30 points): 1≤∣p∣≤∣s∣=n≤20001\le |p|\le |s|=n\le 2000; 0≤m<n0\le m<n; 0≤ri<n0\le r_i<n (0≤i<m0\le i<m); all rir_i are distinct; s,ps,p use lowercase English letters; pp is a subsequence of ss.
  • Subtask 3 (50 points): 1≤∣p∣≤∣s∣=n≤1051\le |p|\le |s|=n\le 10^5; 0≤m<n0\le m<n; 0≤ri<n0\le r_i<n (0≤i<m0\le i<m); all rir_i are distinct; s,ps,p use lowercase English letters; pp is a subsequence of ss.

Examples

Input 1

abcacb
ab
3
3 1 0

Output 1

2

Explanation 1

Removing positions 3,13,1 leaves accb; original positions 0,50,5 form ab. Removing position 00 as well leaves ccb, with no a. Thus k=2k=2.

Input 2

abcbddddd
abcd
6
3 2 1 4 5 6

Output 2

1

Explanation 2

Deleting position 33 leaves abcddddd, starting with abcd. The next deletion removes the only c, so the maximum is 11.

Input 3

abcab
abc
4
0 1 2 3

Output 3

0

Explanation 3

Initially the prefix abc is available. Deleting position 00 leaves bcab, where the only remaining a follows the only c. No deletion is possible.