Codeforces Round #700 (Div. 2), problem: (A) Yet Another String Game

A. Yet Another String Game
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Homer has two friends Alice and Bob. Both of them are string fans.

One day, Alice and Bob decide to play a game on a string s=s1s2…sn of length n consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.

In a move, a player must choose an index i (1≤i≤n) that has not been chosen before, and change si to any other lowercase English letter c that c≠si.

When all indices have been chosen, the game ends.

The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
Each test contains multiple test cases. The first line contains t (1≤t≤1000) — the number of test cases. Description of the test cases follows.

The only line of each test case contains a single string s (1≤|s|≤50) consisting of lowercase English letters.

Output
For each test case, print the final string in a single line.

Example
inputCopy
3
a
bbbb
az
outputCopy
b
azaz
by
Note
In the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to ‘b’.

In the second test case: Alice changes the first letter to ‘a’, then Bob changes the second letter to ‘z’, Alice changes the third letter to ‘a’ and then Bob changes the fourth letter to ‘z’.

In the third test case: Alice changes the first letter to ‘b’, and then Bob changes the second letter to ‘y’.
本题的题意:对于给定的字符串,分别对于奇数和偶数位置的字符串进行修改,字符串的位置从0开始,故而偶数位置的字符尽可能小,奇数位置的字符串尽可能大。
题解:对于相应位置的字符进行修改即可,偶数如果不是a,就进行相应的替换a,否则就是替换成b,奇数位置如果不是z,就进行相应的替换z,否则就是替换成y

#include<bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
int main()
{
    
    
    FAST;
    int t;
    cin>>t;
    while(t--)
    {
    
    
        string s;
        cin>>s;
        for(int i=0;i<s.length();++i)
        {
    
    
            if(i&1)
            {
    
    
                if(s[i]!='z')
                  s[i]='z';
                else
                   s[i]='y';
            }
            else
            {
    
    
                if(s[i]!='a')
                   s[i]='a';
                else
                   s[i]='b';
            }
        }
        cout<<s<<endl;
    }
   //system("pause");
}

猜你喜欢

转载自blog.csdn.net/weixin_46006714/article/details/113750333
今日推荐