zoukankan      html  css  js  c++  java
  • 编程珠玑--变位词集合

    有一步是必须处理的,就是对字符串进行排序;

    第二步的方法就比较多:(1)采用字符串哈希(求值)(2)排序后转化为24位的位向量(3)直接排序后的字符串作为标识(书上的方法)

     1 #include <iostream>
     2 #include <string>
     3 #include <fstream>
     4 #include <map>
     5 #include <algorithm>
     6 #include <vector>
     7 
     8 using namespace std;
     9 
    10 
    11 int main()
    12 {
    13     ifstream dic("in.txt");
    14     vector<pair<string, string> > words_pair;
    15     string word, word_sign;
    16     while(dic >> word) {
    17         word_sign = word;
    18         sort(word_sign.begin(), word_sign.end());
    19         words_pair.push_back(pair<string, string>(word_sign, word));
    20     }
    21 
    22     sort(words_pair.begin(), words_pair.end());
    23     vector<pair<string, string> >::const_iterator iter;
    24     string pre = "";
    25     for (iter = words_pair.begin(); iter != words_pair.end(); iter++){
    26         if (pre != "" && (*iter).first != pre) {
    27             cout << endl;
    28         }
    29         cout << (*iter).second << " ";
    30         pre = (*iter).first;
    31     }
    32 
    33     cout << endl;
    34     return 0;
    35 }
    haired
    hardier harried
    trihedra
    hardwire
    airshed dashier hardies shadier
    airsheds radishes
    hardiest
    ravished
    dishware rawhides
    hayrides
    airthed
    rawhide
    hayride hydriae

    以上是in.txt的内容

  • 相关阅读:
    Linux基础知识
    c语言依赖倒转
    ios的认识
    ios数据的基本类型和流程控制
    JavaScript 创建 自定义对象
    《大道至简》读后感
    总结
    字符串转换成整型并求和
    《大道之简》第二章
    SQL Server 2008 数据库自动备份
  • 原文地址:https://www.cnblogs.com/cane/p/3779521.html
Copyright © 2011-2022 走看看