论洛谷IDE的在dev下报错的程序不报错
  • 板块学术版
  • 楼主Lyu_Lintse
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/6/11 07:21
  • 上次更新2023/10/23 13:24:56
查看原帖
论洛谷IDE的在dev下报错的程序不报错
777059
Lyu_Lintse楼主2023/6/11 07:21

题目:P6548

在一开始做的时候,我的想法是用queue。 编写了代码如下

#include <bits/stdc++.h>
using namespace std;
queue<int> q[30];
string str,ms,ss;
int n,l,maxn,mxb;
char c;
int main()
{
	cin>>n>>str;
	for(int i=n-1;i>=0;i--)
	{
		q[(int)(str[i]-'a')].push(i);
	}
	for(int i=0;i<n;i++)
	{
		if(i%2==0)
		{
            maxn=0;
			mxb=0;
            for(int j=0;j<26;j++)
            {
                if(maxn<q[j].front())//后发现在这里错误
                {
                    maxn=q[j].front();
                    mxb=j;
                }
            }
			c=str[maxn];
			ms+=c;
			q[mxb].pop();
		}
		else
		{
			for(int j=0;j<26;j++)
			{
				if(not q[j].empty())
				{
					ss+=str[q[j].front()];
					q[j].pop();
					break;
				}
			}
		}
	}
	if(ss<ms)
	{
		cout<<"DA"<<endl<<ss;
	}
	else
	{
		cout<<"NE"<<endl<<ss;
	}
	return 0;
}

运行时RE了。 但多次调程后依然RE,认为编译器崩溃了,就用了洛谷IDE(~~~~懒得重装~~~~),关键是用lemonline测评(另一台电脑)仍然RE。 在逐行排查后发现错误在22行(注释标记处)。再次尝试发现c++在windows下(洛谷IDE是Linux)队列为空时返回顶端时会返回一个随机数,大概率会超出字符串长度,然后……pong!!!因此要先判断是否为空。 改正后代码如下

#include <bits/stdc++.h>
using namespace std;
queue<int> q[30];
string str,ms,ss;
int n,l,maxn,mxb;
char c;
int main()
{
	cin>>n>>str;
	for(int i=n-1;i>=0;i--)
	{
		q[(int)(str[i]-'a')].push(i);
	}
	for(int i=0;i<n;i++)
	{
		if(i%2==0)
		{
            maxn=0;
			mxb=0;
            for(int j=0;j<26;j++)
            {
                if(maxn<q[j].front() and (not q[j].empty()))
                {
                    maxn=q[j].front();
                    mxb=j;
                }
            }
			c=str[maxn];
			ms+=c;
			q[mxb].pop();
		}
		else
		{
			for(int j=0;j<26;j++)
			{
				if(not q[j].empty())
				{
					ss+=str[q[j].front()];
					q[j].pop();
					break;
				}
			}
		}
	}
	if(ss<ms)
	{
		cout<<"DA"<<endl<<ss;
	}
	else
	{
		cout<<"NE"<<endl<<ss;
	}
	return 0;
}
2023/6/11 07:21
加载中...