-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0833.cpp
More file actions
29 lines (25 loc) · 966 Bytes
/
Copy path0833.cpp
File metadata and controls
29 lines (25 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
string findReplaceString(const string &s, const vector<int> &indices, const vector<string> &sources,
const vector<string> &targets) const {
const int n = size(indices);
static int order[10001];
iota(begin(order), begin(order) + n, 0);
sort(begin(order), begin(order) + n, [&indices](int i, int j) { return indices[i] < indices[j]; });
string res;
int crnt = 0;
for (int i = 0; i < n; i++) {
const int idx = order[i], m = size(sources[idx]);
int pos = 0;
if (indices[idx] < crnt) continue;
while (indices[idx] > crnt)
res += s[crnt++];
while (pos < m && s[crnt + pos] == sources[idx][pos])
pos++;
if (pos == m) res += targets[idx], crnt += m;
}
while (crnt < size(s))
res += s[crnt++];
return res;
}
};