/*
游戏规则:输入一个只包含英文字母的字符串,
字符串中的俩个字母如果相邻且相同,就可以消除。
在字符串上反复执行消除的动作,
直到无法继续消除为止,
此时游戏结束。
输出最终得到的字符串长度。
输出:原始字符串str只能包含大小写英文字母,字母的大小写敏感,长度不超过100,
输出游戏结束后字符串的长度
备注:输入中包含非大小写英文字母是均为异常输入,直接返回0。
事例:mMbccbc输出为3
*/
#include<iostream>
#include<stdlib.h>
#include<stack>
#include<vector>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
string str;
string res;
getline(cin, str);
stack<char>s1;
for (auto& x : str)
{
if (!isalpha(x))
{
return 0;
}
s1.push(x);
}
stack<char>s2;
while (!s1.empty())
{
///while (s2.empty())
char x=s1.top();
s1.pop();
s2.push(x);
while (!s1.empty()&&!s2.empty())
{
if (s1.top() == s2.top()) {
s1.pop();
s2.pop();
}
else {
char x = s1.top();
s1.pop();
s2.push(x);
}
}
}
cout<< s2.size();
return 0;
}
逻辑思维对应在代码中就是if else while for 的如何摆放问题。要先画图理清思维逻辑,在写循环和判断条件。