CodeForces - 1398C

挺简单的题目,但是没有想到还是整理一下

方法1

把每个元素都减1,那么满足题意的就是一段和的值是0,然后维护前缀和,如果发现这个前缀和之前出现过,就说明有满足题意的部分。就 ans++

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int maxn=4e5+10;
int arr[maxn]={0};
map<int,int>mp;
signed main(){
int N,n;
char c;
cin>>N;
while(N--){
cin>>n;
mp.clear();
for(int a=1;a<=n;a++){
cin>>c;
arr[a]=c-'1';
}
int ans=0;int sum=0;
mp[0]=1;
for(int a=1;a<=n;a++){
sum+=arr[a];
ans+=mp[sum];
mp[sum]++;
}
cout<<ans<<endl;
}
}

方法2

洛谷题解看到的:

直接移项就能知道要求得的式子了

需要注意初始的时候是p[0]=1

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[100005];
int sum[100005];
map<int,int> p;
signed main(){
int time;
cin>>time;
while(time--){
int ans=0;
p.clear();
int len;
cin>>len;
string s;
cin>>s;
p[0]=1;
for(int i=0;i<len;++i){
int x=s[i]-'0';
a[i+1]=x;
sum[i+1]=sum[i]+x;
p[sum[i+1]-i-1]++;
if(p[sum[i+1]-i-1]==1)
continue;
else{
ans+=(p[sum[i+1]-i-1]-1);
}
}
cout<<ans<<endl;
}
return 0;
}

CodeForces - 1398C Good Subarrays的更多相关文章

  1. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  2. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  3. Codeforces 655E Beautiful Subarrays【01trie树】

    题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...

  4. codeforces 665E Beautiful Subarrays

    题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...

  5. codeforces Gym100589H Count Subarrays 树状数组/线段树+离散化

    题意:给你一个数组,问你有多少子数组中的逆元数不小于K个,N<105 还在研究中

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 字典树

    E. Beautiful Subarrays 题目连接: http://www.codeforces.com/contest/665/problem/E Description One day, ZS ...

  7. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  8. Codeforces Round #323 (Div. 2) E - Superior Periodic Subarrays

    E - Superior Periodic Subarrays 好难的一题啊... 这个博客讲的很好,搬运一下. https://blog.csdn.net/thy_asdf/article/deta ...

  9. 【CodeForces】582 C. Superior Periodic Subarrays

    [题目]C. Superior Periodic Subarrays [题意]给定循环节长度为n的无限循环数列,定义(l,s)表示起点为l的长度为s的子串,(l,s)合法要求将子串从该起点开始以s为循 ...

  10. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. 一文剖析TCP三次握手、四次挥手

    TCP三次握手四次挥手 问题 TCP建立连接为什么是三次握手,而不是两次或四次? TCP,名为传输控制协议,是一种可靠的传输层协议,IP协议号为6. 顺便说一句,原则上任何数据传输都无法确保绝对可靠, ...

  2. 1055 - Expression #9 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'xxx.xxx.xxx' which is not functionally ...;this is incompatible with sql_mode=only_full_group_by

    MySQL 8 的默认 sql_mode 包含了only_full_group_by,如果想要sql不按照这模式做检查,可以设置当前session的sql_mode值不包含oly_full_group ...

  3. 【YashanDB知识库】yasql登录报错:YAS-00413

    [问题分类]错误码处理 [关键字]yasql,00413 [问题描述]使用工具设置不同并发迁移数据的过程中,导致yasql登录报错:YAS-00413 [问题原因分析]工具使用与数据库使用资源超过了操 ...

  4. 【合合TextIn】智能文档处理系列—电子文档解析技术全格式解析

    一.引言 在当今的数字化时代,电子文档已成为信息存储和交流的基石.从简单的文本文件到复杂的演示文档,各种格式的电子文档承载着丰富的知识与信息,支撑着教育.科研.商业和日常生活的各个方面.随着信息量的爆 ...

  5. 常回家看看之house_of_emma

    house_of_emma 前言: 相比较于house_of_kiwi(house_of_kiwi),house_of_emma的手法更加***钻,而且威力更大,条件比较宽松,只需要lagebin_a ...

  6. EF Core – Library use EF

    前言 写 Library 有时候会用到 database, 会想用 EF 来维护. 比如 Identity, IdentityServer, OpenIddict, 这些 Library 都有使用到 ...

  7. Nuxt Kit 自动导入功能:高效管理你的模块和组合式函数

    title: Nuxt Kit 自动导入功能:高效管理你的模块和组合式函数 date: 2024/9/14 updated: 2024/9/14 author: cmdragon excerpt: 通 ...

  8. Vue——前端框架

    Vue    Vue 快速入门    <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  9. [Tkey] 与非

    解法原理1 首先我们需要明白 \(\operatorname{nand}\) 的运算: \[\operatorname{not}(a\operatorname{nand}b)=a\operatorna ...

  10. 全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用

    全网最适合入门的面向对象编程教程:56 Python 字符串与序列化-正则表达式和 re 模块应用 摘要: Python 的 re 模块提供了强大的正则表达式操作功能,用于在字符串中搜索.匹配.替换等 ...