如何解决为什么这个 C++ 程序会为一些未知的测试用例产生错误的输出,我无法调试?
我的输入得到了正确的结果(我已经手动尝试了 30 多个输入并得到了所有正确的输出),但是在练习门户上提交后,一些测试用例导致输出错误,我无法调试!
供参考的问题:Chef 非常喜欢玩纸牌。今天,他在玩三张牌的游戏。每张卡片的顶面都有一个字母,底面上有另一个(可能相同)的字母。厨师可以随意重新排列卡片和/或以他希望的任何方式翻转任何卡片(特别是,他可以保持卡片原样)。他想让卡片顶面的字母从左到右拼出他最喜欢的朋友鲍勃的名字。 确定 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
您可以尝试将卡片分为几类
- 带有
o
和b
的卡片 - 只有
o
的卡片 - 只有
b
的卡片 - 两者都没有的卡片(没有用)
然后计算结果
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。