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

正解一眼看出来的应该是鸽笼原理。记录每个位置的前缀和,就是dp[i][1]表示前i个数中,1的个数。dp[i][0]同理。

然后计算出每一个位置的dp[i][1] - dp[i][0],如果和前面的出现相同,那么这一段就是贡献。

也可以从化简dp公式来看。

也是一样的都dp[i][0] && dp[i][1]

那么设区间为be, en

如果要相同,则需要dp[en][1] - dp[be - 1][1] == dp[en][0] - dp[be - 1][0]

化简下,就是上面的式子。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int dp[maxn][];
char str[maxn];
vector<int>pos[maxn * ];
void work() {
scanf("%s", str + );
int lenstr = strlen(str + );
int one = , zero = ;
for (int i = ; i <= lenstr; ++i) {
if (str[i] == '') {
zero++;
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
} else {
one++;
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
}
}
for (int i = ; i <= lenstr; ++i) {
pos[dp[i][] - dp[i][] + lenstr].push_back(i);
}
int ans = ;
for (int i = ; i <= * lenstr; ++i) {
if (pos[i].size() <= ) continue;
ans = max(ans, pos[i].back() - pos[i][]);
}
cout << ans << endl;
return;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

1393 0和1相等串 鸽笼原理 || 化简dp公式的更多相关文章

  1. 1393 0和1相等串 51nod

    1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...

  2. 51nod 1393 0和1相等串 思路 : map存前缀和

    题目: 思路:把'0'当成数字-1,'1'当成数字1,求前缀和,用map更新当前前缀和最早出现的位置.(用map而不用数组是因为可能会出现负数) 当前缀和的值之前出现过,比如i = 10时,sum = ...

  3. 51nod 1393:0和1相等串

    1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...

  4. Gym 100851G Generators (vector+鸽笼原理)

    Problem G. Generators Input file: generators.in Output file: generators.outLittle Roman is studying li ...

  5. POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)

    描述 http://poj.org/problem?id=1065 木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次). Wo ...

  6. poj 3370 鸽笼原理知识小结

    中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...

  7. poj 2356鸽笼原理水题

    关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...

  8. UVA 10620 - A Flea on a Chessboard(鸽笼原理)

    UVA 10620 - A Flea on a Chessboard 题目链接 题意:给定一个跳蚤位置和移动方向.如今在一个国际象棋棋盘上,左下角为黑格,一个格子为s*s,推断是否能移动到白格子.问要 ...

  9. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)

    题目链接  Points Inside A Polygon 题意  给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...

随机推荐

  1. Thinkphp5.0 视图view取值

    Thinkphp5.0 视图view取值 <!-- 获取控制器传递的变量 --> <li>{$age}</li> <!-- 获取服务器的信息 --> & ...

  2. hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, ...

  3. C++ fill 和memset

    以下内容来自www.cplusplus.com--------------------------------------------------- FILL: template <class ...

  4. Linux下Apache服务部署静态网站------网站服务程序

    文章链接(我的CSDN博客): Linux下Apache服务部署静态网站------网站服务程序

  5. JDBC的存储过程

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/stored-procedure.html: 正如一个Connection对象创建了Statement和 ...

  6. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  7. 使用Maven对JAVA程序打包-带主类、带依赖

    使用Maven对JAVA程序打包-带主类.带依赖 http://blog.csdn.net/strongyoung88/article/details/54097830

  8. cisco PIX

    来自为知笔记(Wiz)

  9. PHP导入和导出CSV文件

    CREATE TABLE `student` ( `id` ) NOT NULL auto_increment, `name` varchar() NOT NULL, `sex` varchar() ...

  10. regulator_get 调用过程【转】

    本文转载自:http://blog.csdn.net/u012719256/article/details/52083961 Touch panel DTS 分析(MSM8994平台,Atmel 芯片 ...