栈的应用
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
string deleteAgain(string s){
stack<char> stk;
for(int i=0;i<s.size();i++){
if(stk.empty()||stk.top()!=s[i]) stk.push(s[i]);
//如果一开始为空或者栈顶与下一个不相等,入栈
else stk.pop();
//否则出栈
}
string temp="";//初始化为空
while(!stk.empty()){
temp+=stk.top();//将栈的元素存到字符串中
stk.pop();
}
reverse(temp.begin(),temp.end());//利用库函数反转字符串
return temp;
}
};
int main(){
string s,temp;
cin>>s;
Solution solution;
temp=solution.deleteAgain(s);
cout<<temp;
return 0;
}
欢迎批评指正!