CodeForces 159c String Manipulation 1.0

本文介绍了一种使用线段树实现的用户名变更算法,该算法能够高效地处理字符串中特定字符的删除操作,尤其适用于需要多次变更的情况。通过实例演示了算法的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String Manipulation 1.0

Time Limit: 3000ms
Memory Limit: 262144KB
This problem will be judged on  CodeForces. Original ID: 159C
64-bit integer IO format: %I64d      Java class name: (Any)

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

<p< p="">

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 100characters 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 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

<p< p="">

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

 

Sample Input

<p< p=""><p< p="">
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

Hint

<p< p="">

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".

 

Source

 
解题:直接用线段树记录二十六个字母分别的出现位置。。。
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010;
 4 int tree[26][maxn<<2],k,n,p;
 5 bool isdel[maxn];
 6 string str,s,c;
 7 void pushup(int wd,int v) {
 8     tree[wd][v] = tree[wd][v<<1] + tree[wd][v<<1|1];
 9 }
10 void update(int L,int R,int wd,int id,int v) {
11     if(L == R) {
12         tree[wd][v]++;
13         return;
14     }
15     int mid = (L + R)>>1;
16     if(id <= mid) update(L,mid,wd,id,v<<1);
17     if(id > mid) update(mid+1,R,wd,id,v<<1|1);
18     pushup(wd,v);
19 }
20 void del(int L,int R,int wd,int v,int rk){
21     if(L == R) {
22         tree[wd][v]--;
23         isdel[L] = true;
24         return;
25     }
26     int mid = (L + R)>>1;
27     if(tree[wd][v<<1] >= rk) del(L,mid,wd,v<<1,rk);
28     else del(mid+1,R,wd,v<<1|1,rk-tree[wd][v<<1]);
29     pushup(wd,v);
30 }
31 int main() {
32     cin.sync_with_stdio(false);
33     cin>>k>>s>>n;
34     str = "";
35     for(int i = 0; i < k; ++i) str += s;
36     int len = str.length();
37     for(int  i = 0; i < len; ++i)
38         update(0,len-1,str[i]-'a',i,1);
39     while(n--){
40         cin>>p>>c;
41         del(0,len-1,c[0]-'a',1,p);
42     }
43     for(int i = 0; i < len; ++i)
44         if(!isdel[i]) cout<<str[i];
45     cout<<endl;
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4440076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值