zoukankan      html  css  js  c++  java
  • map + 前缀和

    B. Balanced Substring
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.

    You have to determine the length of the longest balanced substring of s.

    Input

    The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.

    The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.

    Output

    If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

    Examples
    Input
    8
    11010111
    Output
    4
    Input
    3
    111
    Output
    0
    Note

    In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.

    In the second example it's impossible to find a non-empty balanced substring.

    题意 : 找出所给串中的子串中 0 与 1 个数相等的最长子串 。

    思路 :

      第一天晚上做的时候 , 当时的思路是想的二分答案 , 但是 WA 在了样例 3 , 啊 ,然后后来想了想 , 二分确实不可行 。

      后来搜了发题解 , 是这么做的 。

      将串中的 所有 0 变为 -1 , 改为求和为 0 的最长子串 ,一直累加求和, 当和为 0 的时候即为符合题意的解, 还有一种情况 , 就是前缀和 求时 比如 -1 ,记录第一次产生的位置,当后面加的过程中再次出现一个 -1 时,第一个 -1 的下一个元素 当当前元素的长度 也是符合题意的解 。至于映射关系 , 就借助于 map 就好了 。

    代码示例 :

      

    /*
     * Author:  ry 
     * Created Time:  2017/10/13 18:55:00
     * File Name: 1.cpp
     */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <time.h>
    using namespace std;
    const int eps = 1e5+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    
    char pre[eps];
    int arr[eps];
    map<int, int>mp;
    
    int main() {
        int n;
    
        while (~scanf("%d%s", &n, pre)){
            for(int i = 0; i < n; i++){
                if (pre[i] == '1') arr[i+1] = 1;
                else arr[i+1] = -1;
            }
            mp.clear();
            int sum = 0, ans = 0;
            
            int sign = 1;
            for(int i = 1; i <= n; i++){
                sum += arr[i];
                if (sum == 0) {
                    ans = i;
                }
                else if (mp[sum]){
                    ans = max(ans, i - mp[sum]);
                }
                else {
                    mp[sum] = i;
                }
                //printf("ans = %d
    ", ans);
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    帝国cms7.0 内容页控制简介字数!
    帝国cms后台 不同栏目发布字段不同
    帝国cms7.0 列表模板调用本栏目缩略图
    戏说三国战纪
    灵动标签调用指定专题
    帝国cms7.0调用出栏目下的东西
    任天堂的游戏之路
    DC游戏《斑鸠》原创赏析[转载]
    一,PHP 语法
    给自己的网站加入“保存到桌面”的按钮
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7662866.html
Copyright © 2011-2022 走看看