-
Notifications
You must be signed in to change notification settings - Fork 22
/
binary string
54 lines (44 loc) · 1.16 KB
/
binary string
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
/*
Given a binary string S. The task is to count the number of substrings that start and end with 1. For example, if the input string is
“00100101”, then there are three substrings “1001”, “100101” and “101”.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of an integer 'N' denoting the string
length and next line is followed by a binary string.
Output:
For each testcase, in a new line, print the number of substring starting and ending with 1 in a separate line.
Constraints:
1 ≤ T ≤ 100
1 ≤ |S| ≤ 104
Example:
Input:
2
4
1111
5
01101
Output:
6
3
Example:
Testcase 1: There are 6 substrings from the given string. They are 11, 11, 11, 111, 111, 1111.
Testcase 2: There 3 substrings from the given string. They are 11, 101, 1101.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t; while(t--){
int n;
cin>>n;
string s;
cin>>s;
int r=0,k;
for(int i=0; s[i]!='\0'; i++){
if(s[i] == '1'){
r++;
}
}
k=(r*(r-1))/2;
cout<<k<<endl;
}
return 0;
}