B. Substring Removal Game(字符串问题) Educational Codeforces Round 93 (Rated for Div. 2)
原题链接:http://codeforces.com/contest/1398/problem/B
样例:
input
5
01111001
0000
111111
101010101
011011110111
output
4
0
6
3
6
题意: 给定一个01字符串,你和对手每回合都可以连续消去相同且连续的字符(至少要消去一个)。你先开始。游戏规则是每消去一个字符1得1分,消完字符串游戏结束,你们都会采取最优策略,求游戏结束后你最多能获得多少分。
解题思路: 既然都会选择最优策略,所以我们至少在未消去字符串中的1时都对0不感兴趣。我们又想消去得到的分最多,所以我们都会选择当前字符串连续且相同字符1的子序列。OK,那事情就好办了,我们就可以统计只包含1的子序列中1的数目,最后再进行排序,对于你来说是先开始,所以你要隔一个再选。即你统计你获得的分数即可。这就是你的最终得分。具体看AC代码.
AC代码:
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t;
string str;
int a[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>str;
int cnt=0;
int sum=0;
int len=str.size();
rep(i,0,len-1){
if(str[i]=='1'){
sum++;
}
else{
a[cnt++]=sum;
sum=0;
}
if(i==len-1&&str[len-1]=='1')
a[cnt++]=sum;
}
sort(a,a+cnt,greater<int>() );
sum=0;
rep(i,0,cnt-1){
if(a[i]==0)break;
sum+=a[i];
i++;
}
cout<<sum<<endl;
}
}
return 0;
}