CF 159C String Manipulation 1.0

C. String Manipulation 1.0
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nlines 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 cici 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.

Examples
input
2
bac
3
2 a
1 b
2 c
output
acb
input
1
abacaba
4
1 a
1 a
1 c
2 b
output
baa
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".


k個字符串sm次操作,每次刪除第p個字符c,求最終字符串

思路:把每個字母的個數和出現的位置以線段樹的形式存儲。每個字母(共有26個)分別代表一個線段樹,根節點保存該字母總共出現的次數,葉節點(L==R)保存字符出現的位置,每個“字符”線段樹共同組成總的線段樹,保存的是字符串的信息。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define Lson 2 * o, L, M			//左兒子 
#define Rson 2 * o + 1, M + 1, R	//右兒子 
#define mem0(a) memset(a, 0, sizeof(a))

const int maxn = 200000 + 10;
char s[maxn];
int vis[maxn];
int a[26][4 * maxn];
//向上求和,由 o 的左右兒子求 o  
void getSum(int ch, int o)
{
	a[ch][o] = a[ch][2 * o] + a[ch][2 * o + 1];
}
//第i個位置插入一個ch 
void Build(int ch, int i, int o, int L, int R)
{
	if (L == R) {
		a[ch][o] = 1;
		return;
	}
	int M = (L + R) / 2;
	if (i <= M)
		Build(ch, i, Lson);
	else 
		Build(ch, i, Rson);
	getSum(ch, o);
}
//刪除第n次出現的ch 
int Delete(int ch, int n, int o, int L, int R)
{
	if (L == R) {
		a[ch][o] = 0;
		return L;
	}
	int M = (L + R) / 2, ans;
	if (n <= a[ch][2 * o])
		ans = Delete(ch, n, Lson);
	else
		ans = Delete(ch, n - a[ch][2 * o], Rson);
	getSum(ch, o);
	return ans;
}

int main()
{
	int k, m;

	while (~scanf("%d%s%d", &k, s + 1, &m))
	{
		mem0(vis);
		mem0(a);
		int len = strlen(s + 1);
		int n = len * k, j, i;
		for (j = len + 1; --k;)			//複製 k 個s 
			for (i = 1; i <= len; i++)
				s[j++] = s[i];
		s[j] = 0;

		for (int i = 1; s[i]; i++)	//建樹 
		{
			Build(s[i] - 'a', i, 1, 1, n);
		}
		int p;
		char c[5];
		while (m--)
		{
			scanf("%d%s", &p, c);
			int num = Delete(c[0] - 'a', p, 1,1 , n);	//刪除 
			vis[num] = 1;
		}
		for (int i = 1; s[i]; i++)
			if (vis[i] == 0)
				printf("%c", s[i]);
		puts("");
	}
	return 0;
}


發佈了194 篇原創文章 · 獲贊 27 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章