-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTRCH.CPP
42 lines (40 loc) · 790 Bytes
/
STRCH.CPP
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
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
int n, L = 0, R, ans = 0;
string s;
bool matched = false;
char x;
cin >> n;
cin >> s >> x;
for(int i = 1; i <= n; i++) {
matched = false;
if(s[i-1] == x) {
if(L == 0) {
R = i;
ans = ans + (R*(R+1)/2 - L*(L+1)/2) - ((R-1)*R/2 - (L-1)*L/2);
cout << L << " " << R << " " <<ans << endl;
L = R;
R = -1;
}
else if(R == -1) {
R = i;
ans = ans + (R*(R+1)/2 - (L+1)*(L+2)/2);
cout << L << " " << R << " " <<ans<< endl;
L = R;
R = -1;
matched = true;
}
}
}
if(!matched) { //When there's no matching R of last L
R = n;
ans = ans + (R*(R+1)/2 - (L+1)*(L+2)/2);
}
cout << ans << endl;
}
return 0;
}