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容器使得上述解題方式更簡便。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章