1. When using Mp[key] it will go to the key value of the item, if not, he will automatically add a key item, and then the value is assigned to the corresponding initial value (value is an int is assigned to the value of 0,string, the assignment is null). So if you can insert it, you can use find if you are looking for it, so you can save overhead. The time complexity of the lookup is O (logn)
2.
Code:
#include <iostream> #include <string> #include <map>using namespace std;string word[50005];map< String,int> mp;//is implemented with a balanced binary tree, by key from small to large row int main () { int i=0; printf ("%d\n", K.max_size ()); Use the container's capacity while (cin>>word[i++])//Ctrl + Z and enter to end the loop { mp[word[ I-1]]=1;//mp[] [] has been overloaded, note is i-1 AH AH!!!!!! } Map<string,int>::iterator it; For (It=mp.begin (); It!=mp.end (); it++) { string w=it->first; for (I=1; I<w.length ()-1; i++) { string W1 (w,0,i);//copy [0,i] string W2 (W,i);//start copying if from the I position of W ( Mp.find (W1)!=mp.end () &&mp.find (W2)!=mp.end ()) { cout<<w<<endl; Break ; }}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 1247 Hat's Words (find Hat's word from the given word and output in dictionary order)