cqupt - 14 - week train B - An Ordinary Game

B - An Ordinary Game

Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

There is a string s of length 3 or greater.No two neighboring characters in s are equal.

Takahashi and Aoki will play a game against each other.The two players alternately performs the following operation, Takahashi going first:

  • Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.

The player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.

Constraints

  • 3|s|105
  • s consists of lowercase English letters.
  • No two neighboring characters in s are equal.

Input

The input is given from Standard Input in the following format:

s

Output

If Takahashi will win, print First. If Aoki will win, print Second.


Sample Input 1

Copy
aba

Sample Output 1

Copy
Second

Takahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring.


Sample Input 2

Copy
abc

Sample Output 2

Copy
First

When Takahashi removes b from s, it becomes ac.Then, Aoki cannot perform the operation, since there is no character in s, excluding both ends.


Sample Input 3

Copy
abcab

Sample Output 3

Copy
First


题意:两个人轮流删除一个字符串中字符,要求:

1.不能删除首尾字符。

2.删除字符后不能有两个相邻的字符相同。

让我们找出谁最后能获得胜利。

题解:开始一直以为是游戏决策找必胜点和必败点找规律的题。想了很久都没想出来。后来问了同学才知道并不是这种规律。如果首尾字符不相同,那么删除到最后一定只剩下首尾字符,这时只要判断字符长度的奇偶性。如果首尾字符相同则判断第二个与倒数第二个字符是否相同,第二个相同则继续判断,直到最后找到不相同的或者到中点,这时的字符就是删除到最后的字符串。于是总结规律:

        一、首尾相同

        1.字符串长度为奇数:先手败。

        2.字符串长度为偶数:先手胜。

        二、首尾不同

        1.字符串长度为奇数:先手胜。

        2.字符串长度为偶数:先手败。


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;
char str[maxn];

int main()
{
    while(scanf("%s",str) != EOF)
    {
        int n = strlen(str);
        if(str[0] == str[n - 1])
        {
            if(n % 2 == 0)
            {
                printf("First\n");
            }
            else
            {
                printf("Second\n");
            }
        }
        else
        {
            if(n % 2 == 0)
            {
                printf("Second\n");
            }
            else
            {
                printf("First\n");
            }
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/eric_chen_song_lin/article/details/80637060
今日推荐