Codeforces 96A Football Solution & Explanation

Difficulty : 900

Problem Description

Petya loves football very much. One day, as he was watching a football match, he was writing the players’ current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.

Input

The first input line contains a non-empty string consisting of characters “0” and “1”, which represents players. The length of the string does not exceed 100 characters. There’s at least one player from each team present on the field.

Output

Print “YES” if the situation is dangerous. Otherwise, print “NO”.

Eamples

Input001001
OutputNO
Input1000000001
OutputYES

Solution

C# Solution

Solution1

string word = Console.ReadLine();
char now = word[0];
int cnt = 1;
bool dangerous = false;

for (int i = 1; i < word.Length; i++)
{
    char c = word[i];
    if(c == now){
        cnt+=1;
        if(cnt==7){
            Console.WriteLine("YES");
            dangerous = true;
            break;
        }
    }
    else{
        now = c;
        cnt=1;
    }
}

if(!dangerous){
    Console.WriteLine("NO");
}

With a simple iteration, a few variables, and an optimized use of break, this code efficiently checks for seven consecutive occurrences of the same character in the given input string.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.nextLine();
        char now = word.charAt(0);
        int cnt = 1;
        boolean dangerous = false; 
        
        
        for (char c : word.toCharArray()) {
           if(c == now){
                cnt+=1;
                if(cnt==7){
                    System.out.print("YES");
                    dangerous = true;
                    break;
                }
            }
            else{
                now = c;
                cnt=1;
            } 
        }
        
        if(!dangerous){
            System.out.print("NO");
        }
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 96A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *