CF 159C String Manipulation 1.0

来源:互联网 发布:纳什生涯总数据 编辑:程序博客网 时间:2024/06/06 03:40
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
2bac32 a1 b2 c
output
acb
input
1abacaba41 a1 a1 c2 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);elseans = 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
原创粉丝点击