zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 93 (Rated for Div. 2) C. Good Subarrays

    C. Good Subarrays

    原题链接:https://codeforces.ml/contest/1398/problem/C

    题目大意:

    给定一个序列,序列中的数为$0,1...9$,求有多少个连续子序列的和等于它的长度。

    解题思路:

    首先将所有的数都减去1,就可以转化为求有多少个区间和为0,可以维护一个前缀和,当前$i$个数的前缀和等于前$j$个数,则说明$(i,j]$这个区间和为0,所以记录每个前缀和的个数,然后遍历序列即可。要注意的是初始化时$dp_0 = 1$。

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 typedef pair<ll,ll> pi;
     5 typedef complex <double> cp;
     6 #define debug(a) cout<<#a<<":"<<a<<endl;
     7 #define fr freopen("in.txt","r",stdin);
     8 #define Fill(x,a) memset(x,a,sizeof(x))
     9 #define cpy(a,b) memcpy(a,b,sizeof(a))
    10 const double PI = acos(-1);
    11 const ll INF=0x3f3f3f3f;
    12 const ll N=1e6+7;
    13 const ll mod=1e9+7;
    14 ll maxn,minn;
    15 ll T,n,m,q;
    16 string s;
    17 ll arr[N];
    18 map<int,int>mp;
    19 
    20 
    21 int main(){
    22     ll ans,res;
    23     cin>>T;
    24     while(T--){
    25         cin>>n>>s;
    26         ans=res=0;
    27         mp.clear();
    28         for(ll i=0;i<n;i++){
    29             arr[i+1]=s[i]-'0'-1;    
    30         }
    31         mp[0]=1;
    32         for(ll i=1;i<=n;i++){
    33             res+=arr[i];
    34             ans=ans+mp[res];
    35             mp[res]++;
    36         }
    37         cout<<ans<<endl;
    38         
    39     }
    40 
    41 
    42     return 0;
    43 }
    44  

  • 相关阅读:
    不同用户表的导入导出
    视图合并和谓词推入
    pgsql 的函数
    pgsql_pg的数据类型
    个人最终总结
    结对编程黄金点游戏
    阅读代码
    Visual Studio2015安装过程以及单元测试
    软件工程(2018)第五次团队作业
    软件工程(2018)第二次团队作业
  • 原文地址:https://www.cnblogs.com/meanttobe/p/13514695.html
Copyright © 2011-2022 走看看