-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count subsequences of type a^i b^j c^k.cpp
75 lines (52 loc) · 1.12 KB
/
Count subsequences of type a^i b^j c^k.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
PROBLEM:
Given a string s, the task is to count number of subsequences of the form aibjck, where i >= 1, j >=1 and k >= 1.
Note: Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
Examples:
Input : abbc
Output : 3
Subsequences are abc, abc and abbc
Input : abcabc
Output : 7
Subsequences are abc, abc, abbc, aabc
abcc, abc and abc
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains
a string s.
Output:
For each test case in a new line print the required output.
Constraints:
1<=T<=100
1<=Length of string <=100
Example:
Input:
2
abbc
abcabc
Output:
3
7
SOLUTION:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,i,a=0,b=0,c=0;
string s;
cin>>s;
n=s.length();
for(i=0;i<n;i++)
{
if(s[i]=='a')
a+=(a+1);
else if(s[i]=='b')
b+=(a+b);
else if(s[i]=='c')
c+=(b+c);
}
cout<<c<<endl;
}
return 0;
}