Educational Codeforces 63 (Rated for Div. 2) B. Game with Telephone Numbers(思维博弈)

Game with Telephone Numbers:

题目大意:(文末有原题)

给出一个长度为n(n是奇数)的字符串s,Vasya 和 Petya 轮流从字符串删除一个字符直到字符串的长度变为11,Vasya先走,判断结束时获得的串是否是一个电话号码;

电话号码:长度为11,并且第一个字符是'8';

思路:

要判断最后能否是一个电话号码,实际上是判断最后一次操作后首位是否能为'8';

因为P(Petya)不想让其成为电话号码,所以每次都会删除靠前的'8'直到'8'被删完 或者 长度变为11;

而V(Vasya)想让其变为电话号码,所以每次会删除最外侧的'8'之前的字符;

所以只要判断P删除m个'8'之后的那个'8'之前的元素是否被删干净;

代码:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

const int maxn = 2e5 + 10;
char s[maxn];	//s是初始字符串 
int a[maxn];	//用a来记录每个'8'的位置 

int main() {
	int n, k = 0;
	cin >> n;
	int x = n - 11;	//x是要操作的步数;
	 
	for(int i = 0; i < n; i++) {
		cin >> s[i];
		
		if(s[i] == '8')
			a[k++] = i;	//保存的即是'8'的位置,也是该'8'之前的元素个数; 
	}
	
	if((x / 2) >= k){	// x/2 是P能删除的'8'的个数,如果>=k,则字符串中已经没有'8'了; 
		cout << "NO" << endl;
	}else {
		if(a[x / 2] > x) 	        //因为会删除x/2个'8',所以就要判断第 x/2+1 个'8'能否在第一位,
			cout << "NO" << endl;	//而第 x/2+1 个'8'在a中的下标是x/2,并且a[x/2]保存的是在该'8'之前的元素个数 
		else 		                //与x比较是判断能否删除完,P会删除x/2个8,V会删除x/2个其他元素; 
			cout << "YES" << endl;
	}
	
	return 0;
}

原题:

题目:

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 ss of length nn (nn 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<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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值