算法中的博弈论:电话游戏

原题链接
题目描述
A telephone number is a sequence of exactly 11 digits such that its first digit is 8.
Vasya and Petya are playing a game. Initially they have a string s of length n (n is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player’s move it may be 112, 111 or 121. The game ends when the length of string ss becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.
You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).

输入

The first line contains one integer n ( 13 ≤ n < 1 0 5 13≤n<10^5 13n<105, n is odd) —
the length of string s.

The second line contains the string s (|s|=n) consisting only of
decimal digits.

输出

If Vasya has a strategy that guarantees him victory, print YES.
Otherwise print NO.

示例

Input

13
8380011223344

Output

YES

Input

15
807345619350641

Output

NO

Note

In the first example Vasya needs to erase the second character. Then
Petya cannot erase a character from the remaining string 880011223344
so that it does not become a telephone number.
In the second example after Vasya’s turn Petya can erase one character
character 8. The resulting string can’t be a telephone number, because
there is no digit 8 at all.

题解思路
首先还是要抓住题目的关键点:
1、将一串长度为11且以数字‘8’开头的数字定义为电话号码的格式要求
2、给定一串长度为n的数字串s,其中n>11且n为奇数,这说明双方操作的次数相等
3、双方轮流从这串数字中剔除数字,玩家A先开始,每次只能剔除一个数字,直至最终数字串的长度为11
4、如果玩家A必胜(即无论对手如何操作,最后剩下的数字串肯定是一串电话号码),则输出YES,否则输出NO

其实代码实现很简单,只要找到制胜的关键点,这道题就解出来了(废话),我们来分析第一个输入样例:8380011223344

这串长为13的数字为何能够使玩家A必胜呢?

根据题意知,无论给定的字符串有多长,最终肯定满足电话号码的长度(11),因为这是游戏的终止条件,由此我们可以知道制胜点与字符串的长度关系不大

这时我们应该把关注点放在另一个条件上,即怎样才能使这串数字串最终以数字‘8’开头呢?

我们观察8380011223344这串数字,整个数字串只有两个数字‘8’,两个玩家操作的总次数是2,各自操作了一次

对于玩家A,肯定会想尽办法保留更多的数字‘8’,对于玩家B,为了不让玩家A获胜,肯定会把前端的数字‘8’去掉

我们将这串数字从右到左给每个数字一个序号,即第一位为4,第二位为4,第三位为3…

如果玩家B能把第10位之后(不包括第10位)的数字‘8’全部去掉,那么玩家A是必输无疑!

相反,如果玩家B无论怎么操作,都不能把第10位之后的数字‘8’全部去掉,那么游戏终止时肯定会以数字‘8’开头,即玩家A必胜!

由上面的分析可得,只要第10位之后数字‘8’的个数比玩家B的操作次数多,那么无论如何玩家B都不可能将第10位之后的所有‘8’去掉,而玩家A不会自己去把‘8’剔除掉(自取灭亡),这样玩家A就必胜无疑了

代码实现如下:

#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	string str;
	cin >> n >> str;
	int sum = 0;//记录前端8 的个数
	int count = (n - 11) / 2;//记录单方操作次数(n总为奇数)
	for (int i = 0; i < (n - 10); i++)
	{
		if (str[i] == '8')
			sum++;
	}
	if (sum > count)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
	return 0;
}

更多精彩内容可以关注微信公众号:艺千秋录
算法中的博弈论——石头游戏
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艺千秋录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值