求助玄学问题
  • 板块P1106 删数问题
  • 楼主BearBig
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/11/15 21:55
  • 上次更新2023/10/27 02:49:54
查看原帖
求助玄学问题
668599
BearBig楼主2022/11/15 21:55

rt,遇到了玄学问题,求大佬指教
WA+RE+AC 代码:

#include<bits/stdc++.h>
using namespace std;
stack<char>s;
string st;int k;
int main()
{
	cin>>st>>k;
	for(int i=0;i<st.length();++i)
	{
		if(!k)
		{
			s.push(st[i]);
			continue;
		}
		if(s.empty())s.push(st[i]);
		else
		{
			if(s.top()>st[i])
			{
				s.pop();--k;
			}
			else s.push(st[i]);
		}
	}
	while(k>0){--k;s.pop();}
	string num("");
	while(s.size())
	{
		num=s.top()+num;
		s.pop();continue;
	}
	string res("");
	for(int i=0;i<num.length();++i)
		if(!res.length() and num[i]=='0')
			continue;else res+=num[i];
	if(res.empty())cout<<"0";
        else cout<<res;
}

AC 代码:

#include<bits/stdc++.h>
using namespace std;
stack<char>s;
string st;int k;
int main()
{
	cin>>st>>k;
	for(int i=0;i<st.length();)
	{
		if(!k)
		{
			s.push(st[i++]);
			continue;
		}
		if(s.empty())s.push(st[i++]);
		else
		{
			if(s.top()>st[i])
			{
				s.pop();--k;
			}
			else s.push(st[i++]);
		}
	}
	while(k>0){--k;s.pop();}
	string num("");
	while(s.size())
	{
		num=s.top()+num;
		s.pop();continue;
	}
	string res("");
	for(int i=0;i<num.length();)
		if(!res.length() and num[i]=='0')
			{++i;continue;}
		else res+=num[i++];
	if(res.empty())cout<<"0";
        else cout<<res;
}
2022/11/15 21:55
加载中...