zoukankan      html  css  js  c++  java
  • [51NOD1393]0和1相等串(前缀和,map)

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

    题意:中文题面。

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

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn = 1001000;
     5 char str[maxn];
     6 int t[maxn], s[maxn];
     7 map<int, int> pos;
     8 int n;
     9 
    10 int main() {
    11     // freopen("in", "r", stdin);
    12     while(~scanf("%s", str)) {
    13         n = strlen(str);
    14         memset(s, 0, sizeof(s));
    15         pos.clear();
    16         for(int i = 1; i <= n; i++) {
    17             if(str[i-1] == '1') t[i] = 1;
    18             if(str[i-1] == '0') t[i] = -1;
    19             s[i] = s[i-1] + t[i];
    20         }
    21         int ret = 0;
    22         for(int i = 1; i <= n; i++) {
    23             if(s[i] == 0) ret = max(ret, i);
    24             if(pos.find(s[i]) == pos.end()) pos[s[i]] = i;
    25             else {
    26                 ret = max(ret, i-pos[s[i]]);
    27             }
    28         }
    29         printf("%d
    ", ret);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    node中fs模块
    node生成excel,动态替换表格内容
    Postgresql存放数组形式的数据
    ubuntu下安装typescript
    随笔6
    excel文件导出相应数据统计内容
    随笔4
    随笔3.2
    随笔2
    随笔1
  • 原文地址:https://www.cnblogs.com/kirai/p/6028232.html
Copyright © 2011-2022 走看看