zoukankan      html  css  js  c++  java
  • 2019/2/20训练日记+map/multi map浅谈

    Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different
    orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this
    attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
    called ananagrams, an example is QUIZ.
    Obviously such definitions depend on the domain within which we are working; you might think
    that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
    domain would be the entire English language, but this could lead to some problems. One could restrict
    the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
    same domain) but NOTE is not since it can produce TONE.
    Write a program that will read in the dictionary of a restricted domain and determine the relative
    ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
    “rearranged” at all. The dictionary will contain no more than 1000 words.
    Input
    Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
    number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
    across lines. Spaces may appear freely around words, and at least one space separates multiple words
    on the same line. Note that words that contain the same letters but of differing case are considered to
    be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
    consisting of a single ‘#’.
    Output
    Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
    in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
    be at least one relative ananagram.
    Sample Input
    ladder came tape soon leader acme RIDE lone Dreis peat
    ScAlE orb eye Rides dealer NotE derail LaCeS drIed
    noel dire Disk mace Rob dries

    Sample Output
    Disk
    NotE
    derail
    drIed
    eye
    ladder
    soon
    关于昨晚的题,按昨天的思路写出来下面的代码,写一个比较函数,就能出结果,但是这毕竟是STL训练,而且当数据量上去的时候,这样的写法绝对会超时,数组绝对盛不下。

    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    #include<set>
    #include<cstring>
    using namespace std;
    int Count=0;
    struct abc
    {
        string a;
        string b;
    };
    void zh(string m,string &n);
    int main()
    {
        abc x[2000];
        while(cin>>x[Count].a)
        {
            cin>>x[Count].a;
            if(x[Count].a=="#") break;
            zh(x[Count].a,x[Count].b);
            Count++;
        }
    }
    
    void zh(string m,string &n)
    {
        char q[30];
        memset(q,0,sizeof(char)*30);
        for(int i=0;i<m.length();i++)
        {
            q[i]=m[i];
            q[i]=tolower(q[i]);
        }
        sort(q,q+m.size());
        n=q;
    }
    

    所以悬崖勒马,赶紧选用其它容器。
    map/multimap
    map/multimap映射容器的元素数据是由一个Key和一个Value成的,key与映照value之间具有一一映照的关系。
    map/multimap容器的数据结构也采用红黑树来实现的,map插入元素的键值不允许重复,类似multiset,multimap的key可以重复。比较函数只对元素的key进行比较,元素的各项数据只能通过key检索出来。虽然map与set采用的都是红黑树的结构,但跟set的区别主要是set的一个键值和一个映射数据相等,Key=Value。

    map<first,second> a;
    //map,会按照first(键值)排序(查找也是);
    
    map/multimap用法

    头文件

    #include< map >
    

    map成员函数

    begin()				 //返回指向 map 头部的迭代器
    clear()			// 删除所有元素
    count() 			//返回指定元素出现的次数
    empty()				// 如果 map 为空则返回 true
    end() 				//返回指向 map 末尾的迭代器
    erase()				// 删除一个元素
    find()				// 查找一个元素
    insert()			 //插入元素
    key_comp() 			//返回比较元素 key 的函数
    lower_bound() 		//返回键值>=给定元素的第一个位置
    max_size() 			//返回可以容纳的最大元素个数
    rbegin() 			//返回一个指向 map 尾部的逆向迭代器
    rend() 				//返回一个指向 map 头部的逆向迭代器
    size() 				//返回 map 中元素的个数
    swap()				 //交换两个 map
    upper_bound()		 //返回键值>给定元素的第一个位置
    value_comp() 		//返回比较元素 value 的函数
    

    创建map对象

    #include<iostream>
    #include<map>
    using namespace std;
    map<int,char>mp;//定义map容器 
    

    创建结构体map对象

    struct student{
    int birth;
    string name;
    };
    int id;
    typedef map<int,student> Student;//本语句后所有语句Student==map<int,student> 
    

    插入结构体对象
    接上文代码

    Stduent  a;
    cin>>id>>student.birth>>student.name;
    a.insert(make_pair(id,student);
    

    通过map容器使得上述解题方式更简便。

  • 相关阅读:
    A*算法的原理 <转>
    Unity性能优化之 Draw Call原理<转>
    关于XML中:XmlNode和XmlElement的涵义及不同之处
    MySql 数据库连接池
    代码中批量执行Oracle SQL语句
    科密指纹考勤机B329采集
    VB网络编程(webbrowser+Inet+抓包封包+经验)
    Lambda表达式
    网络编程
    多线程
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12799090.html
Copyright © 2011-2022 走看看