我的输入得到了正确的结果(我已经手动尝试了 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
您可以尝试将卡片分为几类
o
和b
o
b
然后计算结果
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句