为什么这个 C++ 程序会为一些未知的测试用例产生错误的输出,我无法调试?

开发者05

我的输入得到了正确的结果(我已经手动尝试了 30 多个输入并得到了所有正确的输出),但是在练习门户上提交后,一些测试用例导致输出错误,我无法调试!

参考问题:大厨很喜欢玩纸牌。今天,他在玩三张牌的游戏。每张卡片的顶面都有一个字母,底面上有另一个(可能相同)的字母。厨师可以随意重新排列卡片和/或以他希望的任何方式翻转任何卡片(特别是,他可以保持卡片原样)。他想让卡片顶面的字母从左到右拼出他最喜欢的朋友鲍勃的名字。确定 Chef 是否可以用这些卡片拼写“bob”。

我的方法:我认为在以下情况下可以实现正确的输出:

  • 两个 'b' & 'o' 在第一个字符串或第二个字符串中。

  • 如果在第一个和第二个字符串中没有直接找到两个 'b' 和 'o',那么单个卡片上必须存在两个 'b' 和 'o',以便我们可以相应地重新排列或翻转。

    #include <iostream>
    #include <cstring>
    
    int main()
     {
      int t;
      std::cin >> t;
    
      while (t--)
      {
     std::string s1, s2, s;
     std::cin >> s1 >> s2;
    
     /* In String1 indiviually */
     int index1 = s1.find('b', 0);
     int index2 = s1.find('b', index1 + 1);
     int index3 = s1.find('o', 0);
    
     if (index1 != -1 && index2 != -1 && index3 != -1)
     {
         std::cout << "yes" << '\n';
         continue;
     }
    
     /* In String2 indiviually */
     int pos1 = s2.find('b', 0);
     int pos2 = s2.find('b', pos1 + 1);
     int pos3 = s2.find('o', 0);
    
     if (pos1 != -1 && pos2 != -1 && pos3 != -1)
     {
         std::cout << "yes" << '\n';
         continue;
     }
    
     /* Now checking whole string1 and string2 or on the top of card as well as foot of card */
     s = s1 + s2;
     int count = s.length();
     int sum = 0;
    
     int key1 = s.find('b', 0);
     if (key1 != -1)
     {
         sum++;
         int xflag1 = key1 + 3; /* if found on top then change its footer to 'z' and vice- 
                                   versa.
                                   this is because lets assume if I found 'b' on top of a card 
                                   and also there exist 'b' or 'o'
                                   on its footer then its create wrong output for next 
                                   std::string.find() */
         int xflag2 = key1 - 3;
    
         if (xflag1 < count)
         {
             s[xflag1] = 'z';
         }
    
         if (xflag2 >= 0)
         {
             s[xflag2] = 'z';
         }
     }
    
     int key2 = s.find('b', key1 + 1);
     if (key2 != -1)
     {
         sum++;
         int xflag1 = key2 + 3;
         int xflag2 = key2 - 3;
    
         if (xflag1 < count)
         {
             s[xflag1] = 'z';
         }
    
         if (xflag2 >= 0)
         {
             s[xflag2] = 'z';
         }
     }
    
     int key3 = s.find('o', 0);
     if (key3 != -1)
     {
         sum++;
         int xflag1 = key3 + 3;
         int xflag2 = key3 - 3;
    
         if (xflag1 < count)
         {
             s[xflag1] = 'z';
         }
    
         if (xflag2 >= 0)
         {
             s[xflag2] = 'z';
         }
     }
    
     if (sum == 3)
     {
         std::cout << "yes" << '\n';
     }
     else
         std::cout << "no" << '\n';
     }
    
    return (0);
    }
    
苹果 苹果

bbboxx 会失败,因为你首先用 b


您可以尝试将卡片分为几类

  1. ob
  2. 卡只有 o
  3. 卡只有 b
  4. 两者都没有的卡(没有用)

然后计算结果

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么这个C ++函数会产生如此多的分支错误预测?

为什么这个C ++程序如此之快?

为什么这个缩进是错误的?

为什么这个C程序的输出是这样的?

为什么这个C程序不能让我访问内存?

为什么这个C程序错误地输出2的幂?

为什么这个C ++程序反向单词不起作用

为什么这个Ansi C程序没有给出结果?

我无法确定为什么这个C程序给我这个答案

为什么这个Objective-C ++程序不能编译?

为什么这个简短的C程序的结果为“ 3 2”?

为什么这个C ++函数会产生混乱的输出?

为什么 catch 不处理这个错误 C++

为什么这个 C 结构体初始化代码会产生总线错误?

为什么这个交换程序不能在 C 中运行?

为什么这个 C 代码会产生无限循环?

为什么这个带有“abort()”的 C 程序不会崩溃?

为什么这个查询是错误的?

为什么这个'C'代码的输出是这样的?

这个 C 程序可以工作,但编译会产生错误。为什么?

为什么这个 C 程序显示这个结果?

为什么 Codeblocks 运行这个 C 程序没有错误,但 Codeforces 显示编译错误?

为什么我们在这个程序中需要虚拟(Turbo C++)?

为什么在这个方法中声明这个变量会覆盖我的类成员(C++)?

这个 C 指针错误是什么意思?为什么会这样

为什么这个 C 程序会崩溃?

为什么这个程序在 C 中给出无效的内存访问错误?

为什么这个 C 程序在 Ubuntu 中什么都不做?

我正在学习 C++ lambda 函数。为什么会有这个输出?