输入描述:
先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n
输出描述:
根据输入,输出查找到的兄弟单词的个数
示例1
输入
3 abc bca cab abc 1
输出
2 bca
代码如下
//Q26查找兄弟单词
class Dic
{
public:
};
int Q26BrotherWordsFind()
{
int n = 0;
while(cin >> n)
{
vector<string> vecstr;
string str;
for(unsigned int i = 0; i < n; i++)
{
cin>>str;
vecstr.push_back(str);
}
string goal;
cin >> goal;
int k = 0;
cin >> k;
vector<string> brostr;
sort(vecstr.begin(),vecstr.end());
for (unsigned int i = 0; i < vecstr.size(); i++)
{
if(goal.size() != vecstr[i].size())
continue;
if(goal == vecstr[i])
continue;
string sgoal = goal;
string sstr = vecstr[i];
sort(sgoal.begin(),sgoal.end());
sort(sstr.begin(),sstr.end());
if(sstr == sgoal)
brostr.push_back(vecstr[i]);
}
cout << brostr.size();
if(k < brostr.size())
cout << " " << brostr[k];
cout << endl;
}
return 0;
}