AcWing 3678. 模拟出入栈游戏
原题链接
简单
作者:
〇壹
,
2025-03-13 21:40:37
·广东
,
所有人可见
,
阅读 1
C++ 代码
#include <iostream>
#include <stack>
using std::cout, std::cin, std::endl;
using std::stack, std::string;
int main()
{
string str;
while (cin >> str)
{
stack<char> Stack;
int i = 0;
char max = 'a'; //用于标记该字母前的所有字母已经入栈,['a', max)已入栈
while (str[i])
{ //遍历字符串
while (str[i] >= max) //如果遍历的字符还未入栈,则将其与其前面所有字符入栈
Stack.push(max++);
if (str[i++] != Stack.top()) break; //匹配遍历字符与栈顶字符是否一致
Stack.pop();
}
if (Stack.empty())
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}