Skip to content

Commit 364a4ad

Browse files
authored
Create 3614.Process-String-with-Special-Operations-II.cpp
1 parent fdc9b7d commit 364a4ad

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using ll = long long;
2+
class Solution {
3+
public:
4+
char processStr(string s, long long k) {
5+
k++;
6+
int n = s.size();
7+
s = "#"+s;
8+
vector<ll>len(n+1);
9+
const ll INF = 1e15+5;
10+
11+
for (int i=1; i<=n; i++) {
12+
char c = s[i];
13+
ll prev = len[i-1];
14+
ll now = prev;
15+
if ('a'<=c && c<='z')
16+
now = prev+1;
17+
else if (c == '*')
18+
now = prev>0 ? (prev-1): 0;
19+
else if (c=='#')
20+
now = prev*2;
21+
else if (c=='%')
22+
now = prev;
23+
len[i] = min(now, INF);
24+
}
25+
if (k==0 || k>(ll)len[n]) return '.';
26+
27+
for (int i=n; i>=1; i--) {
28+
char c = s[i];
29+
ll before = len[i-1];
30+
ll after = len[i];
31+
if ('a'<=c && c<='z') {
32+
if (k==after)
33+
return c;
34+
} else if (c=='*') {
35+
36+
} else if (c=='#') {
37+
if (k>before)
38+
k-=before;
39+
} else if ( c=='%') {
40+
k = before-k+1;
41+
}
42+
}
43+
44+
return '.';
45+
}
46+
};

0 commit comments

Comments
 (0)