String Manipulation 1.0 (Codeforces 159C)
Tags: Others, *special

One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name.

Input: The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.

Output: Print a single string — the user's final name after all changes are applied to it.

Note: Let's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb".

Sample Inputs:
2
bac
3
2 a
1 b
2 c

1
abacaba
4
1 a
1 a
1 c
2 b

Sample Outputs:
acb

baa

Solution (Python):
from itertools import accumulate
from operator import mul

def main():
    return finalName(int(input()), input(), int(input()))

def finalName(nCopies, origName, nChanges):
    letters = set(origName)
    if len(letters) == 1:
        return origName * (nCopies + -nChanges // len(origName)) + origName[0] * (-nChanges % len(origName))
    (origLength, multiplier) = (len(origName), nCopies - 1)
    repetitionsOf = {}
    for i in range(len(origName)):
        if origName[i] in repetitionsOf:
            repetitionsOf[origName[i]].extend(accumulate(mul((origLength,), multiplier), initial=i))
        else:
            repetitionsOf[origName[i]] = list(accumulate(mul((origLength,), multiplier), initial=i))
    for letter in repetitionsOf:
        repetitionsOf[letter].append(0)
        repetitionsOf[letter].sort()
    origName = list(origName) * nCopies
    for _ in range(nChanges):
        (occurrence, letter) = input().split()
        origName[repetitionsOf[letter].pop(int(occurrence))] = ''
    return ''.join(origName)
if __name__ == '__main__':
    print(main())

Solution (Python):
from itertools import accumulate
from operator import mul

def main():
    return finalName(int(input()), list(input()), int(input()))

def finalName(nCopies, origName, nChanges):
    (origLength, multiplier) = (len(origName), nCopies - 1)
    repetitionsOf = {}
    for i in range(len(origName)):
        if origName[i] in repetitionsOf:
            repetitionsOf[origName[i]].extend(accumulate(mul((origLength,), multiplier), initial=i))
        else:
            repetitionsOf[origName[i]] = list(accumulate(mul((origLength,), multiplier), initial=i))
    for letter in repetitionsOf:
        repetitionsOf[letter].append(0)
        repetitionsOf[letter].sort()
    origName *= nCopies
    for _ in range(nChanges):
        (occurrence, letter) = input().split()
        origName[repetitionsOf[letter].pop(int(occurrence))] = ''
    return ''.join(origName)
if __name__ == '__main__':
    print(main())

Solution (Python):
import bisect
import collections
import itertools
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1

def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1
is_prime = [True] * (10 ** 6 + 1)

def primeseive(n):
    is_prime[0] = is_prime[1] = False
    i = 2
    while i * i <= n:
        if is_prime[i] == True:
            for j in range(i * i, n + 1, i):
                is_prime[j] = False
        i += 1

def solve():
    k = II()
    s = SI()
    ans = list(s * k)
    d = collections.defaultdict(list)
    for i in range(k * len(s)):
        d[ans[i]].append(i)
    for _ in range(II()):
        (p, c) = input().split()
        ans[d[c].pop(int(p) - 1)] = ''
    print(''.join(ans))

def main():
    solve()
if __name__ == '__main__':
    main()