425 字
2 分钟
Yes-or-Yes

题面#

Last Christmas, your friend Fernando gifted you a string ss consisting only of the characters Y\mathtt{Y} and N\mathtt{N}, representing “Yes” and “No”, respectively.

You can repeatedly apply the following operation on ss:

  • Choose any two adjacent characters and replace them with their logical OR.

Formally, in each operation, you can choose an index ii (1is11 \leq i \leq |s|-1), remove the characters sis_i and si+1s_{i+1}, then insert:

  • A single Y\mathtt{Y} if at least one of sis_i or si+1s_{i+1} is Y\mathtt{Y};
  • A single N\mathtt{N} if both sis_i and si+1s_{i+1} are N\mathtt{N}.

Note that after each operation, the length of ss decreases by 11.

Unfortunately, Fernando does not want you to combine “Yes OR Yes”, as he has experienced trauma relating to a certain song.

Determine whether it is possible to reduce ss to a single character by repeatedly applying the operation above, without ever combining two Y\mathtt{Y}‘s.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

The only line of each test case contains the string ss (2s1002\le |s|\le 100). It is guaranteed that si=Ys_i = \mathtt{Y} or N\mathtt{N}.

Output

For each test case, print “YES” if the string can be reduced to a single character by repeatedly applying the described operation, and “NO” otherwise.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.

思路#

观察题目所给的合并条件:

  • 合并 NN,最终变成 N,而 Y 的个数不变
  • 合并 NY/YN,最终变成 YY 的个数不变,因为原串也只有一个 Y
  • 合并 YY,最终变成 Y,此时 Y 的个数改变了,但这个操作是不可行的

因此,字符串里 Y 的个数就是初始 Y 的个数

因此,给出结论,当 Y 的个数小于等于 11 的时候输出 YES,否则就是 NO

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
string s;
cin >> s;
int y = 0;
for (int i = 0; i < s.length(); i ++) {
if (s[i] == 'Y') y ++;
}
if (y <= 1) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
Yes-or-Yes
https://github.com/posts/yes-or-yes/
作者
FZSGBall
发布于
2026-05-20
许可协议
CC BY-NC-SA 4.0