Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions String_Algo/substring_removal_game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*.......Competitive prograamming...*/
/*Alice and Bob play a game. They have a binary string s (a string such that each character in it is either 0 or 1).
Alice moves first, then Bob, then Alice again, and so on.
During their move, the player can choose any number
(not less than one) of consecutive equal characters in s and delete
them.The game ends when the string becomes empty, and the score of each player is the number of 1-characters deleted by them.
Each player wants to maximize their score. Calculate the resulting
score of Alice...*/

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;cin>>t;
int count=0;
while(t--){
string s;
cin>>s;
s.push_back('0');
vector<int>a;
int cc=0;
for(int i=0;i<(int)s.size();i++){
if(s[i]=='1')
cc++;
else{
a.push_back(cc);
cc=0;
}
}
sort(a.rbegin(),a.rend());
int total=0;
for(int i=0;i<(int)a.size();i++){
if(i%2==0)
total+=a[i];
}
cout<<total<<"\n";
}
return 0;
}