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

来源:互联网 发布:知势而后可以加兵的加 编辑:程序博客网 时间:2024/04/30 12:15


           一个单词单词字母交换,可得另一个单词,如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;}