【Codeforces】1220C – Substring Game in the Lesson

正文索引 [隐藏]

Problem Description:

Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string s and a number k (0≤k<|s|).
At the beginning of the game, players are given a substring of ss with left border l and right border r, both equal to k (i.e. initially l=r=k). Then players start to make moves one by one, according to the following rules:

  • A player chooses l′ and r′ so that l′≤l, r′≥r and s[l′,r′] is lexicographically less than s[l,r]. Then the player changes l and r in this way: l:=l′, r:=r′.
  • Ann moves first.
  • The player, that can’t make a move loses.

Recall that a substring s[l,r] (l≤r) of a string s is a continuous segment of letters from s that starts at position l and ends at position r. For example, “ehn” is a substring (s[3,5]) of “aaaehnsvz” and “ahz” is not.
Mike and Ann were playing so enthusiastically that they did not notice the teacher approached them. Surprisingly, the teacher didn’t scold them, instead of that he said, that he can figure out the winner of the game before it starts, even if he knows only s and k.
Unfortunately, Mike and Ann are not so keen in the game theory, so they ask you to write a program, that takes s and determines the winner for all possible k.

Input Specification:

The first line of the input contains a single string s (1≤|s|≤5⋅10^{5}) consisting of lowercase English letters.

Output Specification:

Print |s| lines.
In the line i write the name of the winner (print Mike or Ann) in the game with string s and k=i, if both play optimally.

Sample Input1:

abba

Sample Output1:

Mike
Ann
Ann
Mike

Sample Input2:

cba

Sample Output2:

Mike
Mike
Mike

解题思路:

水题,若遍历字符串,当前字符大于最小字符则输出Ann,否则更新最小字符并输出Mike。

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    string str;
    getline(cin,str);
    char min = 'z';
    for(auto it : str)
    {
        if(min < it)
        {
            cout << "Ann" << endl;
        }
        else
        {
            min = it;
            cout << "Mike" << endl;
        }
    }
    return 0;
}