zoukankan      html  css  js  c++  java
  • 面试金典--11.2

    题目描述:给定一个字符串数组,将变位词排在相邻的位置

    思路:

    map即可

    这里发现一个问题,string用sort排序不能使引用,回头看看源码。

     1 #include <iostream>
     2 #include <queue>
     3 #include <climits>
     4 #include <algorithm>
     5 #include <memory.h>
     6 #include <stdio.h>
     7 #include <map>
     8 using namespace std;
     9 
    10 class strCmp
    11 {
    12 public:
    13     bool operator()(string s1,string s2)
    14     {
    15         sort(s1.begin(),s1.end(),less<char>());
    16         sort(s2.begin(),s2.end(),less<char>());
    17         return (s1 == s2);
    18     }
    19 };
    20 
    21 
    22 int main()
    23 {
    24     vector<string> input;
    25     input.push_back("abc");
    26     input.push_back("cba");
    27     input.push_back("aaa");
    28 
    29     strCmp sc;
    30     sort(input.begin(),input.end(),sc);
    31 
    32     map<string,vector<string> > res;
    33     vector<string>::iterator ite = input.begin();
    34     while(ite != input.end())
    35     {
    36         string stmp = *ite;
    37         sort(stmp.begin(),stmp.end());
    38         if(res.find(stmp) != res.end())
    39         {
    40             res[stmp].push_back(*ite);
    41         }
    42         else
    43         {
    44             vector<string> tmp;
    45             tmp.push_back(*ite);
    46             res.insert(make_pair(stmp,tmp));
    47         }
    48         ++ite;
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    pyzabbix 接口使用
    lvs使用进阶
    lvs基础
    linux服务基础之nginx配置详解
    linux服务基础之编译安装nginx
    iptables (二) nat & tcp_wrapper
    iptables (一) 主机防火墙和网络防火墙
    rsyslog及loganalyzer
    linux基础之Mini Linux制作
    linux基础之磁盘管理与文件系统
  • 原文地址:https://www.cnblogs.com/cane/p/3809947.html
Copyright © 2011-2022 走看看