1. 程式人生 > 實用技巧 >[CF從零單排#8]96A - Football

[CF從零單排#8]96A - Football

題目來源:http://codeforces.com/problemset/problem/96/A

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".

Examples
input
001001
output
NO
input
1000000001
output
YES

題目大意:

足球比賽時,由兩個隊進行,用0表示第一個隊隊員,用1表示第二個隊隊員。用一串字元(長度不超過1000)來表示兩隊隊員的站位,如果出現某一隊連續7個或7個以上隊員站在一起,就是危險的情況。輸入一串數字,如果危險,輸出“YES”,否則輸出"NO".

題目分析:

模擬題,先讀入資料,用一個變數k來表示某一隊的連續長度,噹噹前隊員和前一個隊員是一致時,k+1;否則k重新開始,k=1.當k>=7時,會出現危險,否則不會。

參考程式碼

#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
	cin >> s;
	int len = s.size();
	int k = 1;    // 第一個字母肯定是1 
	for(int i=1; i<len; i++){
		if(s[i]==s[i-1]){
			k ++;
			if(k>=7){
				break;
			}
		}else
			k = 1;
	}
	if(k>=7)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;
}