P164 习题10.1
http://t.cn/Ai9PUJtK
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f909e3cc1ad405f127232c9c3c930dd1.png)
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string str;
struct TreeNode{
int data;
TreeNode* leftchild;
TreeNode* rightchild;
TreeNode(int x):data(x),leftchild(NULL),rightchild(NULL){}
};
TreeNode* Insert(TreeNode* root,int x){
if (root == NULL){
root = new TreeNode(x);
}
else if (x<root->data){
root->leftchild = Insert(root->leftchild,x);
}
else if (x>root->data){
root->rightchild = Insert(root->rightchild,x);
}
return root;
}
void preorder(TreeNode* root){
if (root == NULL){return;}
str += root->data-'0';
preorder(root->leftchild);
preorder(root->rightchild);
}
void inorder(TreeNode* root){
if (root == NULL){return;}
inorder(root->leftchild);
str += root->data-'0';
inorder(root->rightchild);
}
int main(){
int n;
cin>>n;
string father;
cin>>father;
TreeNode* rootfather = NULL;
for (int i=0;i<father.size();i++){
rootfather = Insert(rootfather,father[i]-'0');
}
str = "";
preorder(rootfather);
string pre = str;
str = "";
inorder(rootfather);
string in = str;
while(n--){
string son;
cin>>son;
TreeNode* rootson = NULL;
for (int i=0;i<son.size();i++){
rootson = Insert(rootson,son[i]-'0');
}
str = "";
preorder(rootson);
string prestr = str;
str = "";
inorder(rootson);
string inpre = str;
if (prestr == pre && inpre == in){cout<<"YES"<<endl;}
else{cout<<"NO"<<endl;}
}
cin>>n;
}j
P169 习题10.2
http://t.cn/AiCu5hcK
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ad911918a9e95d719eb21d70fcadcac.png)
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int main(){
int n;
cin>>n;
int temp;
priority_queue <int,vector <int>,greater <int> > prique;
while(n--){
cin>>temp;
prique.push(temp);
}
int k;
cin>>k;
int counter = 0;
int p = 1001;
while(!prique.empty()){
if (p!=prique.top()){
counter++;
}
p = prique.top();
prique.pop();
if (counter == k){cout<<p;break;}
}
}
P170 习题10.3
http://t.cn/AiCu5nsQ
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7eced3f2ea1121b951930cdd1249ff30.png)
P178 习题10.4
http://t.cn/AiCuM7nj
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2b7656a80dcc2b1ebafb50f4a589e598.png)
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <string>
using namespace std;
int main(){
map <int,int> m;
int n;
while(cin>>n){
if (n == 0){break;}
while(n--){
int score;
cin>>score;
if (m.find(score)!=m.end()){m[score]++;}
else{m[score] = 1;}
}
int destination;
cin>>destination;
if (m.find(destination)!=m.end()){cout<<m[destination]<<endl;continue;}
cout<<'0'<<endl;
}
}
P178 习题10.5
http://t.cn/AiCuM09f
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9c22f9d99de39313251a085569278cda.png)
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <string>
using namespace std;
struct personarrive{
string arrive;
string left;
bool operator <(const personarrive& x) const{
if(arrive<x.arrive){
return true;
}
return false;
}
};
struct personleft{
string arrive;
string left;
bool operator <(const personleft& x) const{
if(left>x.left){
return true;
}
return false;
}
};
bool comparearrive(){return true;}
int main(){
map <personarrive,string> marrive;
map <personleft,string> mleft;
int casenum;
cin>>casenum;
while(casenum--){
personarrive p;
personleft q;
string id;
cin>>id;
cin>>p.arrive;
cin>>p.left;
q.arrive = p.arrive;
q.left = p.left;
marrive[p] = id;
mleft[q] = id;
}
map <personarrive,string>::iterator itarrive;
map <personleft,string>::iterator itleft;
itarrive = marrive.begin();
itleft = mleft.begin();
cout<<itarrive->second<<' ';
cout<<itleft->second;
}
P178 习题10.6
http://t.cn/AiCux4f7
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/663d3cca974c2d77b93807b81da3c0b3.png)
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
freopen("D://case.txt","r",stdin);
int n,m;
while(cin>>n>>m){
vector<int> vec;
map<int,int> ma;
int temp;
while (n--){
cin>>temp;
ma[temp]++;
vec.push_back(temp);
}
for(int i=0;i<vec.size();i++){
int t = ma[vec[i]];
if(t==1)
cout<<"BeiJu"<<endl;
else
cout<<t-1<<endl;
}
}
return 0;
}