Jishan's Log 13: Codeforces 96A solution

Codeforces 96A: Football

The Problem:

A boy watching football keeps noting down the number of players in both teams with 0 being the players from one team and 1 being the players from another team.

He deduces that if minimum 7 players of the same team stand side by side, the situation would become dangerous.
Ex: 01001. Situation is not dangerous because no 7 players of the same team are standing side by side.
Ex: 1000000001. Situation is dangerous because 7 players of the same team are side by side.
Ex: 1011011111111011. Situation is dangerous because 7 players of the same team are side by side.

The Solution:

 The Algorithm:

1) take a string input
2) start counting the string from 1 until strings length (we start from 1 because we would want to check the chars of current index string with the previous index string) using a for loop.
3) check if current index char matches with last index char.
4) if it does then we increment the counter and check if it's 7 or not. If it is then we return "YES" string
5) else if the chars do not match, it means the 'side-by-side' is not maintained, meaning we need to reset the counter.
6) finally, after the loop ends, we return "NO".

The Code:

NOTE: while checking current index chars with previous or next chars, it is good practice to start from 1 and check current index with previous index. If you start from 0, then you need to check with next index (because there's nothing before 0) and when it reaches to -1 index before string length, it will not work because there's also nothing after the strings length except null char 

#include<bits/stdc++.h>

#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;


auto findmatching(string str)
{
    int counter = 1;
    for(int i = 1i<str.size(); ++i)
    {
        if(str[i] == str[i-1])
        {
            counter++;
            if(counter == 7)
            {
                return "YES\n";
            }
        }
        else
        {
            counter = 1;
        }
    }

    return "NO\n";
}

int main()
{
    fastio;
    string strcin >> str;
    cout << findmatching(str);
}

Comments

Popular posts from this blog

Jishan's Log 14: Codeforces 69A solution

Jishan's Log 12: Codeforces 236A solution