A - Sherlock Bones Gym - 101350A

题意:The great dog detective Sherlock Bones is on the verge of a new discovery. But for this problem, he needs the help of his most trusted advisor -you- to help him fetch the answer to this case.

He is given a string of zeros and ones and length N.

Let F(x, y) equal to the number of ones in the string between indices x and yinclusively.

Your task is to help Sherlock Bones find the number of ways to choose indices (i, j, k) such that i < j < ksj is equal to 1, and F(i, j) is equal to F(j, k).

思路:这个题其实就可以转化为求区间的个数,其中区间内1的个数为奇数,其实思路还是挺好转化的,但是个人感觉代码写起来技巧性还是很强的。

先根据输入的串s求出区间内1个数的奇偶(根据异或),然后在倒着统计有奇数个1的区间个数,偶数个1的区间个数,最后统计总区间个数,在统计总区间个数的时候注意统计方法,同时注意对字符串的处理方式还有计数方式。

#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
#include<string>
#include<cmath>
#include <algorithm>
#include <cstdio>

using namespace std;
const int maxn=2e5+20;
int per[maxn];
int g[maxn][2];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(per,0,sizeof(per));
        memset(g,0,sizeof(g));
        int n;
        cin>>n;
        string s;
        cin>>s;
        for(int i=0; i<n; i++)
        {
            per[i+1]=per[i]^(s[i]-'0');
        }

        for(int i=n; i>=0; i--)
        {
            if(per[i]==1)
            {
                g[i][1]=g[i+1][1]+1;
                g[i][0]=g[i+1][0];
            }
            else
            {
                g[i][0]=g[i+1][0]+1;
                g[i][1]=g[i+1][1];
            }
        }
        s="0"+s;
        int flag=0;
        long long ans=0;
        int last=n+1;
        for(int i=n; i>0; i--)
        {
            if(!flag)
            {
                if(s[i]=='1')
                {
                    flag=1;
                    last=i;
                }
            }
            else
            {
                if(per[i-1]==0)
                {
                    ans+=g[last+1][1];
                }
                else
                {
                    ans+=g[last+1][0];
                }
                if(s[i]=='1')
                {
                    last=i;
                }
            }
        }
        cout<<ans<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值