欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces Round #519 by Botan Investments C. Smallest Word

程序员文章站 2022-05-09 17:38:34
...

On a chessboard with a width of n and a height of n, rows are numbered from bottom to top from 1 to n, columns are numbered from left to right from 1 to n. Therefore, for each cell of the chessboard, you can assign the coordinates (r,c), where r is the number of the row, and c is the number of the column.

The white king has been sitting in a cell with (1,1) coordinates for a thousand years, while the black king has been sitting in a cell with (n,n) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates (x,y)…

Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules:

As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time.

The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates (x,y) first will win.

Let’s recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the (a,b) cell, then in one move he can move from (a,b) to the cells (a+1,b), (a−1,b), (a,b+1), (a,b−1), (a+1,b−1), (a+1,b+1), (a−1,b−1), or (a−1,b+1). Going outside of the field is prohibited.

Determine the color of the king, who will reach the cell with the coordinates (x,y) first, if the white king moves first.


对每个前缀从小到大选择反转或者不反转,问最后能得到的字典序最小的字符串的操作序列。
很有意思得一道题,我们发现类似于abab的串我们可以翻转两次让他变成aabb,稍加讨论,我们可以发现所有的a都可以借由翻转来到最前面。
设下的只要构造这个序列就行了

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pb push_back
using namespace std;
int ans[2001];
int main()
{
    string s;
    cin>>s;
    int n = s.length();
    int sta = (s[0] == 'a');
    for(int i = 1;i<n;i++)
    {
        if(sta)
        {
            if(i != n-1 && s[i] == 'b' && s[i+1] == 'a')
            {
                ans[i] = 1;
                sta ^=1;
            }
        }
        else
        {
            if(i == n-1 || (s[i] == 'a' && s[i+1] == 'b'))
            {
                sta ^= 1;
                ans[i] = 1;
            }
        }
    }
    for(int i = 0;i<n;i++)
    {
        cout<<ans[i]<<' ';

    }
    return 0;
}