给定一个长度为 n 的字符串 s,字符串由小写字母 a..z 和大写字母 A..Z 组成。
小明要对这个字符串进行操作,他会从头到尾检查这个字符串,如果发现:
s[i] 是小写字母,s[i+1] 是 s[i] 对应的大写字母 (1≤i<n);
s[i] 是大写字母,s[i+1] 是 s[i] 对应的小写字母 (1≤i<n);
就会把 s[i],s[i+1] 这两个字符都删掉。
小明会重复这个操作,直到字符串不再改变。
你需要给出处理完成的字符串。
输入格式
第一行一个整数 n。
接下来一行一个长度为 n 的字符串 s。
输出格式
输出最后处理完成的字符串,有可能是空串。
样例输入1
5
aAcCa
样例输出1
a
样例输入2
8
AaAAbcCB
样例输出2
AA
数据规模
对于所有数据,保证 1≤n≤100000。
answer
#include <bits/stdc++.h>
using namespace std;
int n, s[100001], top = 0;
char str[100011];
bool check(char s, char t) {
if (s >= 'a' && s <= 'z')
return t == s - 'a' + 'A';
else
return t == s - 'A' + 'a';
}
int main () {
scanf("%d%s", &n, str + 1);
for (int i = 1; i <= n; i++) {
if (top && check(s[top], str[i]))
--top;
else
s[++top] = str[i];
}
for (int i = 1; i <= top; i++) {
printf("%c", s[i]);
}
}