给定一个长度为 len 的字符序列,字符只可能是 'b'、't'、'c'、'f' 中的一种。使用栈来处理这个字符序列,每当遇到特定的字符组合('c' 紧跟 'f' 或者 'b' 紧跟 't')时,就从栈中弹出最后一个字符,否则将新读取的字符加入栈。最后输出栈的最终大小。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
deque<char>st;
signed main()
{
ll len;
cin>>len;
char ch;
while(len--)
{
cin>>ch;
if(st.empty())
{
st.push_back(ch);
continue;
}
if((ch=='c'&&st.back()=='f')||(ch=='b'&&st.back()=='t'))
{
st.pop_back();
}
else{
st.push_back(ch);
}
}
cout<<st.size();
}
using namespace std;
typedef long long ll;
deque<char>st;
signed main()
{
ll len;
cin>>len;
char ch;
while(len--)
{
cin>>ch;
if(st.empty())
{
st.push_back(ch);
continue;
}
if((ch=='c'&&st.back()=='f')||(ch=='b'&&st.back()=='t'))
{
st.pop_back();
}
else{
st.push_back(ch);
}
}
cout<<st.size();
}