Header Ad

HackerEarth Bob and Bombs problem solution

In this HackerEarth Bob and Bombs problem solution, Bob and Khatu are brave soldiers in World War 3. They have spotted an enemy troop that is planting bombs. They sent messages to the command center containing characters W and B where W represents a wall and B represents a Bomb. They asked the command to tell them how many walls will be destroyed if all bombs explode at once. One bomb can destroy 2 walls on both sides.


HackerEarth Bob and Bombs problem solution


HackerEarth Bob and Bombs problem solution.

#include<bits/stdc++.h>

using namespace std;

#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define all(x) x.begin(),x.end()
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()

int main()
{
int t;
cin >> t;
assert(1 <= t && t <= 10);
while(t--)
{
string s;
cin >> s;
int n = sz(s) , i;
int ans = 0 , pre = -1;
assert(1 <= n && n <= 100000);
for(i=0;i<n;i++)
{
assert(s[i] == 'B' || s[i] == 'W');
}
for(i=0;i<n;i++)
{
if(s[i] == 'B')
{
if(pre == -1)
{
ans += min(2,i);
}
else
{
ans += min(4,i - pre - 1);
}
pre = i;
}
}
if(pre != -1)
ans += min(2,n - pre - 1);
printf("%d\n",ans);
}
return 0;
}

Post a Comment

0 Comments