第四章 树(二)

      在这里主要讲解标准库中的set和map的一些用法。

1、set

      set 是一个排序后的容器,该容器不允许重复。set特有的操作是高效的插入。删除和执行基本查找。


2、map

      map用来存储排序后的由键和值组成的项的集合。键必须唯一,但是多个键可以对应同一个值。因此,值不需要唯一。在map中的键保持逻辑排序后的顺序。


3、set和map的实现

   C++需要set和map支持在最坏情况下对基本的操作insert、erase和find仅消耗对数时间。相应的,底层实现是平衡二叉查找树。典型的用法不是使用AVL树,而是常常使用自顶向下红黑树。


4、set的应用

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <set>
using  namespace std;

template < class T>
class RuntimeCmp
{
public:
     enum cmp_mode {normal, reverse};
private:
    cmp_mode mode;
public:
    RuntimeCmp(cmp_mode m = normal): mode(m) {}

     bool  operator()( const T &t1,  const T &t2)
    {
         return mode == normal ? t1 < t2 : t2 < t1;
    }

     bool  operator==( const RuntimeCmp &rc)
    {
         return mode == rc.mode;
    }
};

typedef set< int, RuntimeCmp< int> > IntSet;

void fill(IntSet &set);

int main()
{
    IntSet set1;
    fill(set1);
    PRINT_ELEMENTS(set1,  "set1:");

    RuntimeCmp< int> reverse_order(RuntimeCmp< int>::reverse);

    IntSet set2(reverse_order);
    fill(set2);
    PRINT_ELEMENTS(set2,  "set2:");

    set1 = set2; //assignment:OK
    set1.insert( 3);
    PRINT_ELEMENTS(set1,  "set1:");

     if(set1.value_comp() == set2.value_comp())
        cout <<  "set1 and set2 have the same sorting criterion" << endl;
     else
        cout <<  "set1 and set2 have the different sorting criterion" << endl;
}

void fill(IntSet &set)
{
    set.insert( 4);
    set.insert( 7);
    set.insert( 5);
    set.insert( 1);
    set.insert( 6);
    set.insert( 2);
    set.insert( 5);
}

5、map 的应用

我们想写一个程序,来找到所有的可以通过替换其中一个字符的得到至少15个其他单词。

程序如下:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<set>
#include<map>
#include<iostream>
#include<vector>
#include<string>

using  namespace std;
//打印结果
void printHighChageables( const map<string, vector<string> > &adjwords,  int minwords =  15)
{
    map<string, vector<string> >::const_iterator itr;
     for(itr = adjwords.begin(); itr != adjwords.end(); ++itr)  //遍历map
    {
         const pair<string, vector<string> > &entry = *itr;  //获取一个键值对
         const vector<string> &words = entry.second;  //获取存储内容

         if(words.size() >= minwords)  //如果大于15 则把结果输出。
        {
            cout << entry.first <<  "(" << words.size() <<  "):";
             for( int i =  0; i < words.size(); i++)
            {
                cout <<  " " << words[i];
            }
            cout << endl;
        }
    }
}
//检测两个单词是否仅有一个字母不同的例程。

bool oneCharOff( const string &word1,  const string &word2)
{
     if(word1.length() != word2.length())
         return  false;
     int diffs =  0;
     for( int i =  0; i < word1.length(); i++)
    {
         if(word1[i] != word2[i])
             if(++diffs >  1)
                 return  false;
    }
     return diffs ==  1;
}

//下面的方法采用暴力解决,时间复杂度为O(N2)
map<string, vector<string> > computeAdjacentWords1( const vector<string> &words)
{
    map<string, vector<string> > adjwords;
     for( int i =  0; i < words.size(); i++)
         for( int j =  0; j < words.size(); j++)
             if(oneCharOff(words[i], words[j]))
            {
                adjwords[words[i] ].push_back(words[j]);
                adjwords[words[j] ].push_back(words[i]);
            }
     return adjwords;
}

//下面采用另外一种解法,就是增加一个map对单词按长度进行存储。
map<string, vector<string> >computeAdjacentWord2( const vector<string> &words)
{
    map<string, vector<string> >adjwords;
    map< int, vector<string> >adjLength;
     for( int i =  0; i < words.size(); i++)
    {
        adjLength[words[i].length()].push_back(words[i]);
    }

     //下面还是用暴力解决。
    map< int, vector<string> >::iterator itr;
     for(itr = adjLength.begin(); itr != adjLength.end(); ++itr)  //根据长度分组暴力解决O(M*M)
    {
         //获取当前值。
        vector<string> mwords = (*itr).second;
         for( int i =  0; i < mwords.size(); i++)
             for( int j =  0; j < mwords.size(); j++)
            {
                 if(oneCharOff(mwords[i], mwords[j]))
                {
                    adjwords[mwords[i] ].push_back(mwords[j]);
                    adjwords[mwords[j] ].push_back(mwords[i]);
                }
            }
    }
     return adjwords;

}
//下面还有一种更加高速的算法
/*
第三种算法更加复杂一些,使用了附加的map,和上面一样,将单词按长度分组,然后对每个分组分别操作。
假设现在运行在长度为4的单词组上。首先,我们想要找到形如wine和nine的只有一个字符不同的单词对。
实现的一个方法有:对每一个长度为4的单词,删除第一个字母,保留剩下三个字母的样本,生成map,其中健就是这个样本。
其值为有这样样本的单词的vector。例如含有ine对应的是dine,fine,wine,nine,mine等等。

每次变化一个字符,可以快速找出这些集合出来。实现算法如下:
*/


map<string, vector<string> >computeAdjacentWord2( const vector<string> &words)
{
    map<string, vector<string> > adjwords;
    map< int, vector<string> >adjLength;

     for( int i =  0; i < words.size(); i++)
    {
        adjLength[words[i].size()].push_back(words[i]);
    }

    map< int, vector<string> >::iterator itr;
     for(itr = adjLength.begin(); itr != adjLength.end(); ++itr)
    {
         int wordlength = itr->first;
         const vector<string> &mwords = itr->second;

         for( int i =  0; i < mwords.size(); i++)
        {
            map<string, vector<string> >repwords;
             for( int j =  0; j < mwords.size(); j++)  //遍历同组的单词,然后删除其中一个位。
            {
                string rep = mwords[j];
                rep.erase(i,  1);
                repwords[rep].push_back(mwords[j]);
            }

            map<string, vector<string> >::const_iterator itr2;
             for(itr2 = repwords.begin(); itr2 != repwords.end(); ++itr2)
            {
                 const vector<string> &clique = itr2->second;
                 if(clique.size() >=  2)
                {
                     for( int p =  0; p < clique.size(); p++)
                    {
                         for( int q = p +  1; q < clique.size(); q++)
                        {
                            adjwords[clique[p] ].push_back(clique[q]);
                            adjwords[clique[q] ].push_back(clique[p]);
                        }
                    }
                }
            }


        }
    }
     return adjwords;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值