Skip to content

Commit 473fd34

Browse files
Merge pull request #248 from H4wk-3yE/patch-4
Create string_query.cpp
2 parents ba0ce0e + 31bbc70 commit 473fd34

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

fenwick_tree/string_query.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/string-query-22/
2+
3+
/*
4+
C++
5+
encoding: UTF-8
6+
Modified: <04/Oct/2019 02:37:20 PM>
7+
8+
✪ H4WK3yE乡
9+
Mohd. Farhan Tahir
10+
Indian Institute Of Information Technology (IIIT), Gwalior
11+
*/
12+
13+
#include <bits/stdc++.h>
14+
#ifdef LOCAL_PROJECT
15+
# include <prettypr.hpp>
16+
#endif
17+
18+
using namespace std;
19+
20+
// clang-format off
21+
22+
#define ll long long
23+
#define ve vector
24+
#define pb push_back
25+
#define endl "\n"
26+
#define ff first
27+
#define ss second
28+
#define pii pair<int, int>
29+
#define len(v) int(v.size())
30+
#define all(v) v.begin(), v.end()
31+
#define reset(a, b) memset(a, b, sizeof(a));
32+
#define fr(i, s, n) for (int i = s ; i < n ; ++i)
33+
#define dfr(i, s, n) for (int i = s ; i > n ; --i)
34+
35+
template < typename T > void pr (const T& t) {
36+
cout << t << "\n";
37+
}
38+
39+
template < typename T, typename U, typename... ARGS >
40+
void pr (const T& t, const U& u, const ARGS&... args) {
41+
cout << t << " ";
42+
pr (u, args...);
43+
}
44+
45+
// clang-format on
46+
47+
const int N = 2e5 + 5;
48+
int arr[N], bit[N][26];
49+
string s;
50+
int n;
51+
52+
void update (int i, int c, int val) {
53+
while (i <= n) {
54+
bit[i][c] += val;
55+
i += (i & (-i));
56+
}
57+
}
58+
59+
int query (int i, int x) {
60+
int ans = 0;
61+
while (i > 0) {
62+
ans += bit[i][x];
63+
i -= (i & (-i));
64+
}
65+
return ans;
66+
}
67+
68+
int search (int k, int x) {
69+
int low = 1, high = n;
70+
while (low <= high) {
71+
int mid = (low + high + 1) >> 1;
72+
if (query (mid, x) < k) {
73+
low = mid + 1;
74+
} else {
75+
high = mid - 1;
76+
}
77+
}
78+
return low;
79+
}
80+
81+
signed main () {
82+
ios_base::sync_with_stdio (false), cin.tie (nullptr);
83+
cin >> s;
84+
n = len (s);
85+
fr (i, 0, n) {
86+
update (i + 1, s[i] - 'a', 1);
87+
}
88+
89+
int tc;
90+
cin >> tc;
91+
while (tc--) {
92+
int k;
93+
char x;
94+
cin >> k >> x;
95+
int idx = search (k, x - 'a');
96+
update (idx, x - 'a', -1);
97+
s[idx - 1] = '#';
98+
}
99+
100+
for (auto i : s) {
101+
if (i != '#') cout << i;
102+
}
103+
cout << "\n";
104+
105+
return 0;
106+
}

0 commit comments

Comments
 (0)