Positive Substring

5

1 votes
Dynamic Programming, Introduction to Dynamic Programming 1, Algorithms
Problem

You are given a binary string S of length N. A substring of a binary string is called positive if the number of 1s present in the substring is strictly greater than the number of 0s present. Find the number of positive substrings in the given string S

  Input format

  • The first line contains T denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains N denoting the length of string S.
    • The second line contains the binary string S.

Output format

For each test case, print  the number of positive substrings in the given string S

Constraints

1T1051N3105Scontains0sand1s.SumofNoveralltestcasesdoesnotexceed3105.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case, the positive substrings are S11=1,S33=1,S13=101.

In the second test case, the positive substrings are S11=1,S44=1,S55=1,S45=11,S35=011,S15=10011.

Editor Image

?