416 字
2 分钟
Specialty-String

题面#

AksLolCoding is playing a game on a string ss of length nn. Initially, ss contains only lowercase Latin characters.

In one turn, AksLolCoding can choose any pair of integers (i,j)(i,j) such that:

  • 1i<jn1 \leq i \lt j \leq n;
  • si=sjs_i = s_j \neq *; and
  • sk=s_k = * for all i<k<ji \lt k \lt j.

If no such i,ji,j exists, then the game ends. Otherwise, AksLolCoding sets si:=s_i:=* and sj:=s_j:=*.

Once the game ends, AksLolCoding wins if and only if every character in ss is equal to *. Determine if it is possible for AksLolCoding to win.

Note: * represents ASCII character 42.

Input

The first line contains an integer tt (1t1001 \leq t \leq 100), the number of test cases.

The first line of each test case contains an integer nn (1n50001 \leq n \leq 5000), the length of the string.

The second line of each test case contains a string ss consisting of lowercase Latin characters.

The sum of nn over all test cases does not exceed 50005000.

Output

Output the answer to each test case on its own line. If AksLolCoding can win, output “YES” (without quotes). Else, output “NO” (without quotes).

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.

思路#

分析题意:将 * 视为删除字符,条件 si=sjs_i=s_j(i,j)(i,j) 间全为 * 等价于当前未删除序列中出现相邻相等字符
一次操作即删除一对相邻相等字符,问能否通过反复删除将字符串清空

我们可以观察到:

用栈维护当前消除后的序列,从左到右扫描字符 cc
若栈非空且栈顶为 cc,则形成相邻对可消除,弹栈
否则将 cc 入栈
扫描结束栈空则输出 YES,否则输出 NO

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n;
cin >> n;
string s;
cin >> s;
stack<char> stk;
for (int i = 0; i < n; i ++) {
if (!stk.empty() && stk.top() == s[i]) stk.pop();
else stk.push(s[i]);
}
if (stk.empty()) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
Specialty-String
https://github.com/posts/specialty-string/
作者
FZSGBall
发布于
2026-05-13
许可协议
CC BY-NC-SA 4.0