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

#BS0000416. Trò chơi xóa ký tự (String Game)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Trò chơi xóa ký tự (String Game)

String Game

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Sergey wants the word pp to be obtainable from tt by deletions. His sister Nastya deletes positions in the prescribed permutation a1,…,ana_1,\ldots,a_n, where n=∣t∣n=|t|. Positions refer to the original word and are numbered from 11. Sergey may stop her and delete additional letters himself. Determine the largest number of deletions she can perform before he intervenes. Initially pp can be obtained from tt without reordering letters.

Input

The first two lines contain tt and pp, respectively. The third line contains n=∣t∣n=|t| integers a1,…,ana_1,\ldots,a_n in deletion order.

Output

Print the maximum number of allowed deletions.

Subtasks

  • Subtask 1 (20 points): ∣p∣=1|p|=1; 1≤∣p∣<∣t∣=n≤2⋅1051\le |p|<|t|=n\le 2\cdot10^5; 1≤ai≤n1\le a_i\le n (1≤i≤n1\le i\le n); all aia_i are distinct; t,pt,p use lowercase English letters; pp is a subsequence of tt.
  • Subtask 2 (30 points): 1≤∣p∣<∣t∣=n≤20001\le |p|<|t|=n\le 2000; 1≤ai≤n1\le a_i\le n (1≤i≤n1\le i\le n); all aia_i are distinct; t,pt,p use lowercase English letters; pp is a subsequence of tt.
  • Subtask 3 (50 points): 1≤∣p∣<∣t∣=n≤2⋅1051\le |p|<|t|=n\le 2\cdot10^5; 1≤ai≤n1\le a_i\le n (1≤i≤n1\le i\le n); all aia_i are distinct; t,pt,p use lowercase English letters; pp is a subsequence of tt.

Examples

Input 1

ababcba
abb
5 3 4 1 7 6 2

Output 1

3

Explanation 1

Deleting positions 5,3,45,3,4 leaves abba; positions 1,2,61,2,6 form abb. Deleting position 11 next leaves bba, which cannot form abb. Hence 33.

Input 2

bbbabb
bb
1 6 3 4 2 5

Output 2

4

Explanation 2

The first four deletions leave the two letters at positions 2,52,5, both b. The next deletion leaves only one letter, so the answer is 44.