UPC-6474 AtCoDeer and Rock-Paper(模拟)

题目描述
AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of N turns. In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:
(※) After each turn, (the number of times the player has played Paper)≤(the number of times the player has played Rock).
Each player’s score is calculated by (the number of turns where the player wins) − (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.
(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)
With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts. Plan AtCoDeer’s gesture in each turn to maximize AtCoDeer’s score.
The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≤i≤N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≤i≤N) character of s in p, TopCoDeer will play Paper in the i-th turn.

Constraints
1≤N≤105
N=|s|
Each character in s is g or p.
The gestures represented by s satisfy the condition (※).
输入
The input is given from Standard Input in the following format:
s
输出
Print the AtCoDeer’s maximum possible score.
样例输入
gpg
样例输出
0
提示
Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

题意:两个人剪刀石头布,只能出石头和布,赢了得一分,输了扣一分,平手不得分,我们已经预知对手出石头和布的步骤。现在,要求我们每次出一个石头和布时都要保证当前我们出过石头的步骤次数大于等于出布的次数,两人皆要遵守此规则,问最后最多可以得多少分。注意这个出石头和布次数的比较,是每轮都要比较的,也就是说,第一轮,我们什么都没出,因此石头计数为0,布计数为0,为了遵守规则,第一次必出石头,应根据石头的已出步数得知我们是否可以出布。第二轮时,我们出了一个石头,那么我们可以出一次布,之后布和石头次数相等时,就不得再出布,除非垒够了一定数量的石头,符合规则时才能继续使用布。

直接计数模拟即可,可以发现,只要出布,就能挽回一分差值,因此无论在哪里出布,我们尽量可以出就多出布,那么每轮做一次计数和判断,能出则出,否则出石头攒布。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
char str[maxn];
int main()
{
    while(scanf("%s",str)!=EOF)
    {
        int gg=0,pp=0,ans=0;
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            if(str[i]=='g')
            {
                if(pp<gg)ans++,pp++;
                else gg++;
            }
            else
            {
                if(pp<gg)pp++;
                else gg++,ans--;
            }
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/kuronekonano/article/details/80518857