题面
Last Christmas, your friend Fernando gifted you a string consisting only of the characters and , representing “Yes” and “No”, respectively.
You can repeatedly apply the following operation on :
- Choose any two adjacent characters and replace them with their logical OR.
Formally, in each operation, you can choose an index (), remove the characters and , then insert:
- A single if at least one of or is ;
- A single if both and are .
Note that after each operation, the length of decreases by .
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 to a single character by repeatedly applying the operation above, without ever combining two ‘s.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The only line of each test case contains the string (). It is guaranteed that or .
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,最终变成Y,Y的个数不变,因为原串也只有一个Y - 合并
YY,最终变成Y,此时Y的个数改变了,但这个操作是不可行的
因此,字符串里 Y 的个数就是初始 Y 的个数
因此,给出结论,当 Y 的个数小于等于 的时候输出 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; }}