您的位置:首页 > 其它

2012 百度实习笔试题-兄弟单词

2012-09-19 10:11 288 查看



一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程。

思路:利用STL函数next_permutation()自动生成含有该单词所有字母的所有字符串序列,然后逐个在字典中查找。同时,查找时,通过预先判断待匹配单词的长度和该单词长度,加速查找过程。

上伪代码:

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
struct {
Word* word;
unsigned int len_Volca;//字典里单词数
}Volca;//已经存储好的字典
struct {
string  word_string;
}Word;
Word pWord;
int main()
{
string str; // 用str来存储待查找单词
cin>>str;
unsigned int str_len = str.len();
Word operator_word;
operator_word.word_string = str;
sort(str.begin(),str.begin());
cout<<str<<endl;
while(next_permutation(str.begin(),str.end()))
{
for(int i = 0 ; i < len_Volca;i++)
{
if(pWord.word_string[i].len()==str_len)
{
cout<<str<<endl;
if(pWord.word_string[i] == str)
{
int count  = 1;
printf("The %drd brother word is %s !",count,str);
count++:
}
}
}
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: