额外减小 发表于 2023-10-5 18:21:27

p9518 用map做的,样例过,求调/hack

rt.


record

https://www.luogu.com.cn/record/127633464

思路就是用 map 记录一个元素是否存在,当队首元素不存在时弹出并跳过。

其他的就是纯模拟,但是看不出哪错了

code

#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main()
{
        ios::sync_with_stdio(false);
        cin.tie(0);
        queue<string> q;
        map<string,bool> m;
        string str,op,playing;
        int n,playing_len=0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
                cin>>op;
                if(op=="start")
                {
                        for(int j=0;j<playing_len;j++)
                        {
                                q.push(playing);
                        }
                        playing_len=0;
                        while(playing_len<2 and !q.empty())
                        {
                                if(m.find(q.front())==m.end())// element doesn't exist
                                {
                                        q.pop();
                                        continue;
                                }
                                playing=q.front();
                                cout<<q.front()<<' ';
                                playing_len++;
                                q.pop();
                        }
                        if(playing_len==0)
                        {
                                cout<<"Error\n";
                        }
            else
            {
                cout<<endl;
            }
                }
                else if(op=="arrive")
                {
                        cin>>str;
                        if(m.find(str)!=m.end())
                        {
                                cout<<"Error\n";
                        }
                        else
                        {
                                m=true;
                                q.push(str);
                                cout<<"OK\n";
                        }
                }
                else// leave
                {
                        cin>>str;
            bool flag=false;
            for(int i=0;i<playing_len;i++)
            {
                if(playing==str)
                {
                  flag=true;
                  break;
                }
            }
                        if(m.find(str)==m.end() or flag)
                        {
                                cout<<"Error\n";
                        }
                        else
                        {
                                m.erase(m.find(str));
                                cout<<"OK\n";
                        }
                       
                }
        }
}

@Ewan-Ahiouy @Mike_python小 @sfqxx

isdkz 发表于 2023-10-5 18:21:32

抱歉,这里没有提供任何问题或上下文信息。请提供更多详细信息,以便我可以为您提供帮助。谢谢!

Mike_python小 发表于 2023-10-5 18:21:58

很高兴听到您的代码通过了样例测试。如果您有任何问题或报告漏洞,则可以参考以下建议:

1.阅读题目并确定边界条件:在提交您的代码之前,请确保您已经仔细阅读并理解了题目要求和边界条件。这有助于你在写代码时更加谨慎,减少代码漏洞的产生。

2.调试您的代码:如果您遇到了错误,请不要放弃。相反,您应该利用调试工具来查找和修复代码中的错误。在进行调试时,可以输出打印语句以查看变量值和执行路径等信息。

3.与其他人交流:您可以尝试与其他程序员交流,例如,让他们审查您的代码并提供建议。这样做有助于发现您可能忽略的问题,并且可以提高代码质量。

4.使用一些工程实践:您可以采用一些工程实践来提高代码的可维护性和可扩展性。例如,您可以使用命名变量、编写注释、编写单元测试等方法。这些技术都能够确保您的代码更加健壮,易于维护,并且可以提高代码的运行效率。

希望这些建议能对您有所帮助。祝你好运!

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

陶远航 发表于 2023-10-5 18:26:27

您的代码逻辑看起来是正确的,但是在处理 "start" 操作时,您需要确保在开始新一轮比赛之前清空队列。这是因为在新一轮比赛开始时,所有之前的选手都应该重新加入队列。为了解决这个问题,您可以在 "start" 操作的开始处清空队列。以下是修改后的代码:

#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    queue<string> q;
    map<string,bool> m;
    string str,op,playing;
    int n,playing_len=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
      cin>>op;
      if(op=="start")
      {
            // 清空队列
            while (!q.empty()) {
                q.pop();
            }

            for(int j=0;j<playing_len;j++)
            {
                q.push(playing);
            }
            playing_len=0;
            while(playing_len<2 and !q.empty())
            {
                if(m.find(q.front())==m.end())// element doesn't exist
                {
                  q.pop();
                  continue;
                }
                playing=q.front();
                cout<<q.front()<<' ';
                playing_len++;
                q.pop();
            }
            if(playing_len==0)
            {
                cout<<"Error\n";
            }
            else
            {
                cout<<endl;
            }
      }
      else if(op=="arrive")
      {
            cin>>str;
            if(m.find(str)!=m.end())
            {
                cout<<"Error\n";
            }
            else
            {
                m=true;
                q.push(str);
                cout<<"OK\n";
            }
      }
      else// leave
      {
            cin>>str;
            bool flag=false;
            for(int i=0;i<playing_len;i++)
            {
                if(playing==str)
                {
                  flag=true;
                  break;
                }
            }

您的代码逻辑看起来是正确的,但是在处理 "start" 操作时,您需要确保在开始新一轮比赛之前清空队列。这是因为在新一轮比赛开始时,所有之前的选手都应该重新加入队列。为了解决这个问题,您可以在 "start" 操作的开始处清空队列。以下是修改后的代码:

#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    queue<string> q;
    map<string,bool> m;
    string str,op,playing;
    int n,playing_len=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
      cin>>op;
      if(op=="start")
      {
            // 清空队列
            while (!q.empty()) {
                q.pop();
            }

            for(int j=0;j<playing_len;j++)
            {
                q.push(playing);
            }
            playing_len=0;
            while(playing_len<2 and !q.empty())
            {
                if(m.find(q.front())==m.end())// element doesn't exist
                {
                  q.pop();
                  continue;
                }
                playing=q.front();
                cout<<q.front()<<' ';
                playing_len++;
                q.pop();
            }
            if(playing_len==0)
            {
                cout<<"Error\n";
            }
            else
            {
                cout<<endl;
            }
      }
      else if(op=="arrive")
      {
            cin>>str;
            if(m.find(str)!=m.end())
            {
                cout<<"Error\n";
            }
            else
            {
                m=true;
                q.push(str);
                cout<<"OK\n";
            }
      }
      else// leave
      {
            cin>>str;
            bool flag=false;
            for(int i=0;i<playing_len;i++)
            {
                if(playing==str)
                {
                  flag=true;
                  break;
                }
            }
            if(m.find(str)==m.end() or flag)
            {
                cout<<"Error\n";
            }
            else
            {
                m.erase(m.find(str));
                cout<<"OK\n";
            }
      }
    }
}

这个修改应该可以解决您遇到的问题。如果您在运行修改后的代码时仍然遇到问题,请随时告诉我。

Mike_python小 发表于 2023-10-5 18:49:51

我蒟蒻{:10_250:}{:10_250:}{:10_250:}

额外减小 发表于 2023-10-5 18:52:38

Mike_python小 发表于 2023-10-5 18:49
我蒟蒻

您?

刚刚学了dijkstra spfa,来这边扮猪吃老虎{:10_250:}

额外减小 发表于 2023-10-5 18:53:08

Mike_python小 发表于 2023-10-5 18:49
我蒟蒻

你最好是({:10_244:}

Mike_python小 发表于 2023-10-5 19:01:38

额外减小 发表于 2023-10-5 15:52
您?

刚刚学了dijkstra spfa,来这边扮猪吃老虎

确实蒟蒻{:10_250:}我连个图的存储和遍历都搞不明白

额外减小 发表于 2023-10-5 19:03:38

Mike_python小 发表于 2023-10-5 19:01
确实蒟蒻我连个图的存储和遍历都搞不明白

好好好

那你是怎么学这些算法的?

Mike_python小 发表于 2023-10-5 19:08:32

额外减小 发表于 2023-10-5 16:03
好好好

那你是怎么学这些算法的?

emmm
学完图的存储和dfs、bfs就学了啊{:10_245:}

Mike_python小 发表于 2023-10-5 19:10:19

额外减小 发表于 2023-10-5 16:03
好好好

那你是怎么学这些算法的?

等学完最短路径就去刷题{:10_256:}不过想必困难重重
页: [1]
查看完整版本: p9518 用map做的,样例过,求调/hack