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


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


 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值