0
点赞
收藏
分享

微信扫一扫

中秋时节赏明月,五子棋戏月饼趣 — Flutter中秋限定版五子棋

Problem - 1398C - Codeforces

解析:

        可以写出 sum[ j ] - sum[ i ] = j - i +1,对其移项得 sum[ j ] - j = sum[ i ] - (i - 1),问题转换为有多少个前缀和相等,则用map遍历统计一遍即可。 

        注意,当sum[ j ] - j = 0时,说明 1 到 j 区间也符合题意,但是却计算不进去,所以开始令mp[ 0 ] = 1

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int t,n,x,sum[N],res;
signed main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&n);
		map<int,int>mp;
		mp[0]=1,res=0;
		for(int i=1;i<=n;i++){
			scanf("%1lld",&x);
			sum[i]=sum[i-1]+x;
			res+=mp[sum[i]-i]++;
		}
		printf("%lld\n",res);
	}
	return 0;
}
举报

相关推荐

0 条评论