01串
来源:互联网 发布:mysql 查库表空间大小 编辑:程序博客网 时间:2024/05/03 12:27
1086: 01串
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 6 Solved: 2
[Submit][Status][Web Board]
Description
01串是指仅由0和1组成的字符串。一个完美的01串,是指任意相邻字符都不相等的01串。也就是说,完美的01串应该拥有这样的形式:“010101...”或者“10101...”。
一个01串的完美子串,是指这个字符串中连续的一段,且这段字符串是完美的01串。例如对于字符串"0110"来说,"1","0","01"和"10"都是他的完美子串,而"010"则不是。
给定一个仅包含0,1和?的字符串,每次操作你可以将一个?替换成0或1。在所有替换完成之后,这个字符串中包含的最长的01完美子串的长度将作为你的得分。你的任务是,在给定的字符串下,最多能拿到多少分?
Input
输入数据的第一行是测试数据的组数T(T≤20)。
每组测试数据仅有一行,是一个串长不超过105的01串。数据保证不会给定空串。
Output
对于每组测试数据输出一行,即为能拿到的最大分数。
Sample Input
20??10110
Sample Output
42
HINT
Source
CPC23 2014-2
这道题目是哈理工比赛时候的一道题目,由适牛出的题目。。。这道题目需要用动态规划来写,首先数组dp[i][2]记录i位置的时候如果选择0最大串适多少。如果选择1最大串适多少?
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int Max = 100005+10;int dp[Max][2];int main(){ #ifdef xxz freopen("in","r",stdin); #endif // xxz int T; cin>>T; while(T--) { memset(dp,0,sizeof(dp)); string str; cin>>str; int len = str.length(); if(str[0] == '?') { dp[0][1] = dp[0][0] = 1; } else { int temp = str[0] -'0'; dp[0][temp] = 1; } for(int i = 1; i < len; i++) { if(str[i] == '?' || str[i] == '1') { dp[i][1] = dp[i-1][0] + 1; } if(str[i] =='?' || str[i] == '0') { dp[i][0] = dp[i-1][1] + 1; } } int ans = 0; for(int i = 0; i < len;i ++) { ans = max(ans,max(dp[i][0],dp[i][1])); } cout<<ans<<endl; } return 0;}
0 0
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- 01串
- Mac使用
- 关于正则表达式 g,m 参数的总结,为了回答“正则表达式(/[^0-9]/g,'')中的"/g"是什么意思?”
- 碎碎念
- 昂秦大撒比
- Linux centos添加源+网易163源+搜狐源
- 01串
- 小游戏——猜猜你的生日
- Excel使用总结
- 使用XAMPP搭建本地JSP+PHP环境
- 《宇航动力学学报》征稿简则
- VR眼镜来袭,VIRGlass玩起虚拟现实
- BroadCom Wi-Fi related Source Code
- Win7快捷键
- Selenium入门1 安装selenium IDE: