蓝桥·删除字符(栈+字符串,其他的解法看不下去)

蓝桥·删除字符(栈+字符串,其他的解法看不下去)_第1张图片
tm,我是菜菲,这个题竟然看了这么久。另外,没好的思路的时候,去csdn上找博客,竟然没有一篇我觉得不错的!最后,只想骂自己,骂其他的博客。希望对你有用,本小菲羊的第二篇给爱学习的people看的博客~
核心代码:

void solve()
{
	cin>>s>>n;
	stack<char>st;
	for(int i=0;i<s.size();i++)
	{
		if(n==0||(st.empty())){st.push(s[i]);continue;}
		while(st.empty()==0&&st.top()>s[i])
		{
			st.pop();n--;
		}
		st.push(s[i]);
	}
	string x;
	while(st.empty()==0)
	{
		x+=st.top();st.pop();
	}
	reverse(x.begin(),x.end());
	cout<<x;
}

你可能感兴趣的:(非常愿意分享给大家的,蓝桥杯)