查找兄弟单词 牛客输出多输出一个数字 ? 求大佬看看
牛客上运行输出一个string对象,为什么会在string对象结尾多输出一个2呢 《求帮助》
输出一个string对象,为什么会多输出一个2呢,在自己本地输出没有任何问题,测例通过50%,
i为兄弟个数,res为查找的兄弟单词,我直接让res=输入的目标string对象,为什么会这样啊
std::cout << i << endl << res;// //if(res=="adbcb") cout << "wcw ";//debug,证明res此时没有2结尾,的确没有2
源代码如下
/*
*牛客 查找兄弟单词
*
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n=0;//debug n=0
while (cin>>n)
{
//debug with n>0 ?
if (n<=0)
return -1;
vector<string> vstr;
string str;
for (int i = 0; i < n; i++)
{
cin >> str;
vstr.push_back(str);
}
sort(vstr.begin(), vstr.end());
string word;
int pos;
int i = 0; //many of brother
string res="";
cin >> word;
cin >> pos;
string backword = word; //backup for target word which will be changed
sort(word.begin(), word.end());
for (auto tmp : vstr)
{
string backtmp = tmp;
sort((tmp).begin(), (tmp).end());
if (word == tmp)
{ //find brotther&nbs***bsp;himself
if (backword == backtmp)
{
continue;
}
else
{
i++;
}
if (i == pos)
{ /*少了一个等号,答案输出变成 1 和dacbb*/ //find the tarword;
res = backtmp;
}
}
//do nothing for next word;
}
std::cout << i << endl
<< res;//
//if(res=="adbcb") cout << "wcw ";//测例3,证明res此时没有2结尾,的确没有2
n = 0; // debug
}
return 0;
}
查看8道真题和解析