题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393

题意:中文题面。

把0看成是-1,并且存一遍前缀和。这样-1和1相等数量的时候前缀和为0,这个特判一次。还有就是区间[l,r]和为0的时候的充要条件是s[r]-s[l]=0,得解。

 #include <bits/stdc++.h>
using namespace std; const int maxn = ;
char str[maxn];
int t[maxn], s[maxn];
map<int, int> pos;
int n; int main() {
// freopen("in", "r", stdin);
while(~scanf("%s", str)) {
n = strlen(str);
memset(s, , sizeof(s));
pos.clear();
for(int i = ; i <= n; i++) {
if(str[i-] == '') t[i] = ;
if(str[i-] == '') t[i] = -;
s[i] = s[i-] + t[i];
}
int ret = ;
for(int i = ; i <= n; i++) {
if(s[i] == ) ret = max(ret, i);
if(pos.find(s[i]) == pos.end()) pos[s[i]] = i;
else {
ret = max(ret, i-pos[s[i]]);
}
}
printf("%d\n", ret);
}
return ;
}

[51NOD1393]0和1相等串(前缀和,map)的更多相关文章

  1. 1393 0和1相等串 51nod

    1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...

  2. 51nod 1393:0和1相等串

    1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...

  3. 51nod 1393 0和1相等串 思路 : map存前缀和

    题目: 思路:把'0'当成数字-1,'1'当成数字1,求前缀和,用map更新当前前缀和最早出现的位置.(用map而不用数组是因为可能会出现负数) 当前缀和的值之前出现过,比如i = 10时,sum = ...

  4. 1393 0和1相等串 鸽笼原理 || 化简dp公式

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...

  5. Centos命令行窗口显示一大串前缀,777;notify;Command completed;的解决方法

    How to remove the return code from the terminal prompt In addition to the PS1 environment variable, ...

  6. HDU4821---字符串hash,map判重

    这是2013年长春区域赛的铜牌题...然而第一次做的时候一直觉得会超时的..最后才知道并没有想象中的那么恐怖: 这题有两个注意的地方: (1)h[i] = h[i-1] * seed + s[i] - ...

  7. hdu 2197 求长度为n的本原串 (快速幂+map)

    Problem Description由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,10 ...

  8. Codeforces 1006C:Three Parts of the Array(前缀和+map)

    题目链接:http://codeforces.com/problemset/problem/1006/C (CSDN又改版了,复制粘贴来过来的题目没有排版了,好难看,以后就截图+题目链接了) 题目截图 ...

  9. bzoj 4236: JOIOJI【前缀和+map】

    设sj,so,si分别是J O I的个数前缀和,然后要求求最长(l,r)满足sj[r]-sj[l-1]==so[r]-so[l-1]==si[r]-si[l-1],化简一下就是满足so[r]-so[l ...

随机推荐

  1. google pinyin elmentary os

    sudo apt-get install software-properties-common for ppa. I have been using Sun Pinyin for quite a lo ...

  2. Elasticsearch--Date math在索引中的使用

    在Elasticsearch,有时要通过索引日期来筛选某段时间的数据,这时就要用到ES提供的日期数学表达式 描述: 特别在日志数据中,只是查询一段时间内的日志数据,这时就可以使用日期数学表达式,这样可 ...

  3. android 学习随笔六(网络要求及配置)

    android在4.0之后已经不允许在主线程执行http请求了. 主线程阻塞,应用会停止刷新界面,停止响应用户任何操作,耗时操作不要写在主线程   只有主线程才能修改UI ANR异常:Applicat ...

  4. css经典布局学习

    . 布局 布局是css的重头戏,每个系统的布局都有其各自的特点.无好无坏,肯定是各有优缺点,不妨拿出几个比较典型的例子来一起分析一下.例如: 经典三列布局 Bootstrap栅格布局 百度首页布局 微 ...

  5. C#(winform)浏览按钮

    FolderBrowserDialog folderBrowser = new FolderBrowserDialog();            //folderBrowser.SelectedPa ...

  6. std的find和reverse_iterator联合使用

    上代码: // test2013.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #in ...

  7. oracle查看表占磁盘大小

    select segment_name, bytes/1024/1024 from user_segments S where S.segment_type = 'TABLE' AND S.segme ...

  8. 在Windows Live Writer中插入C# code

    平时都是用Windows Live Writer写博客,发布博客.遇到需要插入代码都是先在notepad中写好,或者是拷贝到notepad,再从notepad中拷到Windows Live Write ...

  9. Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

    Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...

  10. 关于接收json以及使用json

    Common: FileIO.cs using System; using System.Collections.Generic; //using System.Linq; using System. ...