Codeforces Round #700 (Div. 2)-A. Yet Another String Game
傳送門
Time Limit: 2 seconds
Memory Limit: 512 megabytes
Problem Description
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 = s 1 s 2 … s n s = s_1 s_2 \dots s_n s=s1?s2?…sn? of length n n 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 i i ( 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n) that has not been chosen before, and change s i s_i si? to any other lowercase English letter c c c that c ≠ s i c \neq s_i 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 a a is lexicographically smaller than a string b b b if and only if one of the following holds:
Input
Each test contains multiple test cases. The first line contains t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 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 s s ( 1 ≤ ∣ s ∣ ≤ 50 1 \leq |s| \leq 50 1≤∣s∣≤50) consisting of lowercase English letters.
Output
For each test case, print the final string in a single line.
Sample Input
3
a
bbbb
az
Sample Onput
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’.
題目大意
Alice和Bob玩字串游戲,Alice想讓字串字典序盡可能小,Bob想讓盡可能大,他們都能修改字串中未修改過的字符,Alice先手,問最終字串長什么樣,
解題思路
一個字符只能修改一次,想要字符排序盡可能靠前,就盡可能修改最前面的字符
a*****
b*****
則第一個字串必定小于第二個
所以Alice就在第一個未修改過的字符中,盡可能修改為a
但是如果這個字符恰好是a,那么就把它修改為b
AC代碼
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
while (N--)
{
string s;
cin >> s;
for (int i = 0; i < s.size(); i++)
{
if (i % 2 == 0) //Alice
putchar((s[i] == 'a') ? 'b' : 'a'); //s[i]等于'a'嗎? 如果是,putchar('b') 否則,putchar('a')
else //Bob
putchar((s[i] == 'z') ? 'y' : 'z'); //s[i]等于'z'嗎? 如果是,putchar('y') 否則,putchar('z')
}
puts(""); //輸出換行
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258094.html
標籤:其他
