博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A - Sherlock Bones Gym - 101350A
阅读量:4114 次
发布时间:2019-05-25

本文共 1386 字,大约阅读时间需要 4 分钟。

题意: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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
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
=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<
<

转载地址:http://zbgsi.baihongyu.com/

你可能感兴趣的文章
MODULE_DEVICE_TABLE的理解
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
乘法逆元
查看>>
Objective-C 基础入门(一)
查看>>
Flutter Boost的router管理
查看>>
iOS开发支付集成之微信支付
查看>>
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
DirectX11 光照演示示例Demo
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>