[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;
}

猜你喜欢

转载自www.cnblogs.com/gdgzliu/p/13372520.html
96A
今日推荐