String Game (Codeforces 779D)
Tags: Others, strings

Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya"  "nastya"  "nastya"  "nastya"  "nastya"  "nastya"  "nastya".Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.It is guaranteed that the word p can be obtained by removing the letters from word t.

Input: The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct).

Output: Print a single integer number, the maximum number of letters that Nastya can remove.

Note: In the first sample test sequence of removing made by Nastya looks like this:"ababcba"  "ababcba"  "ababcba"  "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".So, Nastya will remove only three letters.

Sample Inputs:
ababcba
abb
5 3 4 1 7 6 2

bbbabb
bb
1 6 3 4 2 5

Sample Outputs:
3

4

Solution (Python):
import sys
sys.setrecursionlimit(2 ** 20)
DEBUG = 0

def testRemove(string, target, remove, until):
    copy = string.copy()
    for i in remove[:until]:
        copy[i - 1] = 0
    if DEBUG:
        print(copy, string)
    j = 0
    for i in copy:
        if i == target[j]:
            j += 1
    if j == len(target):
        return 1
    return 0

def binSearch(string, target, remove, lo, hi):
    mid = lo + (hi - lo + 1) // 2
    if lo >= hi:
        if testRemove(string, target, remove, mid):
            return mid
        return mid + 1
    if testRemove(string, target, remove, mid):
        return binSearch(string, target, remove, mid, hi)
    return binSearch(string, target, remove, lo, mid - 1)
string = list(input()) + ['\x00']
target = list(input()) + ['\x00']
remove = list(map(int, input().split()))
answer = binSearch(string, target, remove, 0, len(remove) - 1)
print(answer)

Solution (Python):
start = list(input())
end = list(input())
nums = list(map(int, input().split()))

def ifpossible(mid):
    temp = start.copy()
    for i in range(mid):
        temp[nums[i] - 1] = 0
    ptr = 0
    for i in range(len(temp)):
        if temp[i] == end[ptr]:
            ptr += 1
            if ptr == len(end):
                return True
    return False
low = 0
high = len(nums)
res = 0
while high >= low:
    mid = int(low + (high - low) / 2)
    if ifpossible(mid):
        res = mid
        low = mid + 1
    else:
        high = mid - 1
print(res)

Solution (Python):
def isSubSequence(str1, str2, m, n):
    j = 0
    i = 0
    while j < m and i < n:
        if str1[j] == str2[i]:
            j = j + 1
        i = i + 1
    return j == m
t = list(input())
p = input()
a = list(map(int, input().split()))

def check(x):
    count = 0
    y = t.copy()
    for i in range(x):
        y[a[i] - 1] = ''
    b = ''.join(y)
    return isSubSequence(p, b, len(p), len(b))
l = 0
r = len(t)
m = (l + r) // 2
while r - l > 1:
    if check(m):
        l = m
    else:
        r = m
    m = (r + l) // 2
print(m)