51nod 0和1相等串
来源:互联网 发布:性格测试软件 编辑:程序博客网 时间:2024/05/22 04:15
题目链接:
https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=424616
题解:
很好的一道思维题,自己一开始的想法就是dp,但是范围比较大,也没什么好的想法。后来又想用二分,但没什么用(范围还是太大),最后看到了大佬的思路,因为数的种类比较少,直接进行+和-的操作就能纪录所有的状态,学到了。
代码:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define met(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fconst int maxn = 1e6+10;int num[2*maxn];string s;int main(){ cin>>s; int n=(int)s.size(); met(num,-1); num[n]=0;//ps:需要注意对于的第一个的时候,要进行判断的,不然会先0111111这种情况 int cnt=n; int ans=0; for(int i=0;i<n;i++) { if(s[i]=='1') cnt++; else cnt--; if(num[cnt]!=-1) ans=max(ans,i-num[cnt]+1);//这里原来的初始化都是-1,所以,使用的时候需要在坐标的基础上+1 else num[cnt]=i+1; } printf("%d\n",ans);}
阅读全文
0 0
- 51nod 0和1相等串
- 51nod 1393 0和1相等串(前缀和)
- 51NOD 1393 0和1相等串
- 51nod 1393:0和1相等串
- 51nod 1393 0和1相等串 (连续区间)
- 51nod 1393 0和1相等串
- 51Nod-1393-0和1相等串
- 51nod 1393 0和1相等的串
- 51nod oj 1393 0和1相等串 【贪心】
- 51Nod 1393 0和1相等串
- 51nod 1393 0和1相等串(鸽巢)
- 51nod 1393 0和1相等串
- 51nod-【1393 0和1相等串】
- 51nod 1393 0和1相等串【思维】
- 51Nod 1393 0和1相等串
- 51nod 1393 0和1相等串
- 51nod 1393 0和1相等串 思路
- 51nod 1393 0和1相等串
- 0pe单文件夹,grub菜单全外置版
- bzoj2809 [Apio2012]dispatching(可并堆)
- 求最大值(经典线段树)
- 大纲
- 初学者---Android 学习资料
- 51nod 0和1相等串
- Ciclop开源3D扫描仪软件---Horus源码分析之src\horus\engine\calibration\platform_extrinsics.py
- Java抽象(方法,类)和Java的接口
- springboot---helloworld
- 将virtio集成slipstream到windows iso,winpe – 原生方法和利用0pe
- history.go(-1)和history.back()的区别和联系
- [LeetCode] 232. Implement Queue using Stacks
- mysql 基本操作 存储过程和函数详解 (第四章)
- vb.net 教程 8-3 数据库操作1