[Codeforces 873B]Balanced Substring】的更多相关文章

Description 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…
inputstandard input outputstandard 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 zero…
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则称为平衡子串,求最长的平衡子串. 解题思路:将0换成-1,算出每个点的前缀和,若两个点前缀和相同,从第一个点到第二个点之前的字符串则为平衡串,求出最长的即可. 代码: #include<iostream> #include<cstring> #include<cstdio>…
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. #include<bits…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard 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 t…
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 t…
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 equa…
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\) \(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\) 然后hash一下DP即可. #include <iostream> #include <cstdio…
B. Balanced Substring 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 t…
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串的 第 j 个, 已经放了 k 个左括号, 是否存在所给串的方案数. 因为不匹配的不是从头开始的, 所以暴力求下一个或者直接ac自动机都可以. #include<bits/stdc++.h> #define LL long long #define LD long double #define u…
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input outputstandard: output You are given a graph with nnn nodes and mmm directed edges. One lowercase letter is assigned to each node. We define a path's…
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\((j-1)\)替换为\(j\) \[2*sum[i]-2*sum[j]=i-j\] \[2*sum[i]-i=2*sum[j]-j\] 枚举i,然后求前面和\(2*sum[i]-i\)相同的最小标号 这里可能有负数,所以map就好了 当然,因为\(2*sum[i]-i\)的值是在区间[-n,n]的 所以你可…
题目描述 You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then th…
传送门 很妙的题 首先先考虑一个简化的问题,现在有一行格子让你填 你要么填一格 要么填两格 有的格子不让你填 问你填了 $a$ 个一格和填了 $b$ 个两格有多少种方案 那么显然先只考虑放两格的方案,这个可以简单 $dp$ 得到,设 $f[i][j]$ 表示前 $i$ 个格子放了 $j$ 个两格的方案数 那么如果 $i,i-1$ 都没障碍,那么 $f[i][j]=f[i-1][j]+f[i-2][j-1]$ ,否则 $f[i][j]=f[i-1][j]$ 然后再来考虑填一格的,显然剩下的 $to…
传送门 这一题是真的坑人,时间空间都在鼓励你用 $NTT$ 优化 $dp$...(但是我并不会 $NTT$) 看到题目然后考虑树形 $dp$ ,设 $f[i][0/1]$ 表示 $i$ 个节点的树,根节点为奇数/偶数的方案数 然后发现对于 $f[i][0/1]$ 的所有方案,把节点编号同时加一个偶数后根节点奇偶性不变,把节点编号加一个奇数后根节点的奇偶性变了 那么就可以对每个 $f[i][0/1]$ 枚举左右子树转移了,因为确定总点数所以左子树点数就有一个范围,在那个范围内枚举子树大小 $j$…
传送门 首先显然的,如果一个位置开始播放了两圈还没结束,那么就永远不会结束 先考虑位置 $1$ 开始播放,用一个 $multisetset$ 维护一下当前听的所有歌,直到某一首歌 $r$ 不合法了就停止,此时播放的区间即为位置 $1$ 开始的答案 然后考虑从位置 $2$ 开始播放时和从位置 $1$ 开始播放有什么变化,显然播放的歌曲一定可以到 $r$ (反证法容易证明),并且 $multiset$ 里少了一首位置 $1$ 的歌 那么直接把 $multiset$ 更新一下,然后继续模拟过程直到下一…
传送门 先来考虑一下二维时的情况,那么对于 $x$ 相同的点,我们按 $y$ 排序,然后相邻的一对对消除 最后 $x$ 坐标相同的点最多剩下一个,那么此时所有点的 $x$ 坐标都不一样 再按 $x$ 把 $x$ 相邻的一对对删除即可 扩展到三维,显然也可以同样的思路,先把 $x,y$ 相同的点按 $z$ 一对对消除,然后在把 $x$ 相同的点按 $y,z$ 相邻的一对对消除 最后按 $x,y,z$ 相邻的一对对消除即可 #include<iostream> #include<cstdio…
传送门 这一题有点意思 首先预处理出 $pos[x]$ 表示编号 $x$ 的车是第几个出隧道的 然后按进入隧道的顺序枚举每辆车 $x$ 考虑有哪些车比 $x$ 晚进入隧道却比 $x$ 早出隧道 显然是 $1$ 到 $pos[x]$ 中还没访问过的车,那么暴力做法就是这样枚举然后看看有哪些没标记并打上标记 注意到每辆车只要打上一个标记即可,并且每次打标记都只在一段前缀区间内 那么维护一下 $r$ 表示之前打标记的区间的最右端,每次只要考虑 $[r,pos[x]]$ 即可(注意 $pos[x]$ 本…
传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \mod 3$ 相同的位置的字符和 $i$ 位置的字符是一样的 所有 $j \mod 3 = (i+1) \mod 3$位置的字符也都是一样的并且是可以确定的 所有 $j \mod 3 = (i+2) \mod 3$位置的字符也都是一样的并且是确定的 维护 $cnt[0/1/2][0/1/2]$ 表示…
题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距离什么的,但是仔细看 Hard 下的时间复杂度不允许,然后进行了一波分析... 上图模式串 2 同理. 从上图可以发现,其实就是主串往后移动一位的同时模式串也往后移动一位匹配,同时去掉无用信息即可. 代码 #include<cstdio> #include<climits> #incl…
Content 给定一个长度为 \(n\) 且仅包含字符 a.b 的字符串 \(s\).请找出任意一个使得 a.b 数量相等的 \(s\) 的子串并输出其起始位置和终止位置.如果不存在请输出 -1 -1. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant 1000\),\(1\leqslant n\leqslant 50\). Solution 不难想到一种很朴素的做法:直接暴力提取出所有 \(s\) 的子串,然后一个一个判断这些子串里头是否有 a.b 数量相等的子串…
Educational Codeforces Round 30  A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pragma GCC optimize("Ofast,no-stack-protector") #include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define endl "\n&quo…
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... s…
动态规划1 动态规划问题是面试题中的热门话题,如果要求一个问题的最优解(通常是最大值或者最小值),而且该问题能够分解成若干个子问题,并且小问题之间也存在重叠的子问题,则考虑采用动态规划. 1.LLS 最长上升子序列 2.最大子段和 3.背包 01背包 完全背包 多重背包 混合背包 分组背包 多维背包 输出方案 输出方案种数 最优方案种数 第k优数 A - Tickets Jesus, what a great movie! Thousands of people are rushing to t…
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... s…
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately w…
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;…
http://codeforces.com/contest/1133/problem/Ctime limit per test 2 secondsmemory limit per test 256 megabytesinputstandard inputoutputstandard output You are a coach at your local university. There are $n$ students under your supervision, the programm…
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string is said to be balanced if each of its characters appears n/4 times where…
链接: https://codeforces.com/contest/1220/problem/C 题意: Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string s and a number k (0≤k<|s|). A…