Educational Codeforces Round 93 (Rated for Div. 2) C. Good Subarrays

Educational Codeforces Round 93 (Rated for Div. 2) C. Good Subarrays

题目链接
You are given an array a1,a2,…,an consisting of integers from 0 to 9. A subarray al,al+1,al+2,…,ar−1,ar is good if the sum of elements of this subarray is equal to the length of this subarray (∑i=lrai=r−l+1).

For example, if a=[1,2,0], then there are 3 good subarrays: a1…1=[1],a2…3=[2,0] and a1…3=[1,2,0].

Calculate the number of good subarrays of the array a.

Input

The first line contains one integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains one integer n (1≤n≤1e5) — the length of the array a.

The second line of each test case contains a string consisting of n decimal digits, where the i-th digit is equal to the value of ai.

It is guaranteed that the sum of n over all test cases does not exceed 1e5.

Output

For each test case print one integer — the number of good subarrays of the array a.

Example

input

3
3
120
5
11011
6
600005

output

3
6
1

简单 DP,预处理出前缀和 p r e [ i ] pre[i] pre[i],不难发现题目要求就可以转化为:
d p [ r ] − d p [ l ] = r − l dp[r]-dp[l]=r-l dp[r]dp[l]=rl,这是很明显的 DP,用 m a p map map 标记一下即可,AC代码如下:

#include
using namespace std;
typedef long long ll;
int main()
{
     
    int t,n;
    string s;
    cin>>t;
    while(t--){
     
        cin>>n>>s;
        ll pre=0,ans=0;
        map<ll,ll>m;
        m[0]=1;
        for(int i=1;i<=n;i++) {
     
            pre+=s[i-1]-'0';
            ans+=m[pre-i];
            m[pre-i]++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(动态规划,map,Codeforces)