forked from deepakgarg0802/MyCompetitiveCodingSolutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabrepeat.cpp
53 lines (43 loc) · 1016 Bytes
/
abrepeat.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
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
#define MAX 1000000
#define ll long long
#define DEBUG(x) do { std::cout << #x << ": " << x << " "; } while (0)
using namespace std;
int main()
{
long long int t,n,k,ans,i,j,a,b;
char str[MAX];
long long left[MAX],right[MAX];
cin>>t;
while(t--){
cin>>n>>k;
cin>>str;
a=b=ans=0;
for(i=n-1;i>=0;--i){
if(str[i]=='b'){
b++;
}
if(str[i]=='a'){
right[i]=b;
}
}
b=0;
for(i=0;i<n;++i){
if(str[i]=='b'){
b++;
}
if(str[i]=='a'){
left[i]=b;
}
}
for(i=0;i<n;++i){
//DEBUG(left[i]);
//DEBUG(right[i]);
if(str[i]=='a'){
ans+= k*right[i]+ k*(k-1)*(left[i]+right[i])/2;
}
}
cout<<ans<<endl;
}
return 0;
}