Codechef Count Substrings

Problem Description

Given a string S consisting of only 1s and 0s, find the number of substrings which start and end both in 1.
In this problem, a substring is defined as a sequence of continuous characters Si, Si+1, ..., Sj where 1 ≤ i ≤ j ≤ N.

Input

First line contains T, the number of testcases. Each testcase consists of N(the length of string) in one line and string in second line.

Output

For each testcase, print the required answer in one line.

Constraints

1 ≤ T ≤ 105
1 ≤ N ≤ 105
Sum of N over all testcases ≤ 105

Example

Input:

2
4
1111
5
10001

Output:

10
3

Explanation

test1: All substrings satisfy.
test2: Three substrings S[1,1], S[5,5] and S[1,5] satisfy.

题解

简单的计数问题,要求我们找出“头尾均为1”的子串个数。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int T,n;
ll ct;
char ch[100005];
int main()
{
	scanf("%d",&T);
	while(T--)
	   {scanf("%d",&n);
	    scanf("%s",ch);
	    int i;
	    ct=0;
	    for(i=0;i<n;i++)
		   {if(ch[i]=='1') ct++;}
		printf("%lld\n",(ct+1)*ct/2);
	   }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值