CodeForces 159c String Manipulation 1.0
String Manipulation 1.0
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
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 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.
Sample Input
2
bac
3
2 a
1 b
2 c
acb
1
abacaba
4
1 a
1 a
1 c
2 b
baa
Hint
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步