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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值