例题10.1 二叉树遍历(THU)
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
char data;
TreeNode* leftChild;
TreeNode* rightChild;
TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL){ }
};
TreeNode* Build(int& pos,string str){
char c=str[pos++];
if(c=='#'){
return NULL;
}
TreeNode* root=new TreeNode(c);
root->leftChild=Build(pos,str);
root->rightChild=Build(pos,str);
return root;
}
void InOrder(TreeNode* root){
if(root==NULL){
return;
}
InOrder(root->leftChild);
printf("%c ",root->data);
InOrder(root->rightChild);
return;
}
int main(){
string str;
while(cin>>str){
int pos=0;
TreeNode* root=Build(pos,str);
InOrder(root);
cout<<endl;
}
}
例题10.2 二叉树遍历(HUST)
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
char data;
TreeNode* left;
TreeNode* right;
TreeNode(char c):data(c),left(NULL),right(NULL){};
};
TreeNode* Build(string str1,string str2){
if(str1.size()==0){
return NULL;
}
char c=str1[0];
TreeNode* root=new TreeNode(c);
int pos=str2.find(c);
root->left=Build(str1.substr(1,pos),str2.substr(0,pos));
root->right=Build(str1.substr(pos+1),str2.substr(pos+1));
return root;
}
void PostOrder(TreeNode* root){
if(root==NULL){
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%c",root->data);
return;
}
int main(){
string str1,str2;
while(cin>>str1>>str2){
TreeNode* root=Build(str1,str2);
PostOrder(root);
printf("\n");
}
}
习题10.1 二叉搜索树(ZJU)
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int x):data(x),left(NULL),right(NULL){
}
};
TreeNode* Insert(TreeNode* root,int x){
if(root==NULL){
root=new TreeNode(x);
}
else if(x<root->data){
root->left=Insert(root->left,x);
}
else if(x>root->data){
root->right=Insert(root->right,x);
}
return root;
}
bool isEqual(TreeNode *x,TreeNode* y){
if(x==NULL&&y==NULL){
return true;
}
else if(x==NULL&&y!=NULL||x!=NULL&&y==NULL){
return false;
}
if(x->data!=y->data){
return false;
}
return isEqual(x->left,y->left)&&isEqual(x->right,y->right);
}
int main(){
int n;
string s,t;
while(cin>>n){
if(n==0) break;
cin>>s;
TreeNode* root1=NULL;
for(int i=0;i<s.size();i++){
root1=Insert(root1,s[i]);
}
for(int i=0;i<n;i++){
cin>>t;
TreeNode *root2=NULL;
for(int j=0;j<t.size();j++){
root2=Insert(root2,t[j]);
}
if(isEqual(root1,root2)){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
}
}
例题10.5 复数集合(BUPT)
#include<bits/stdc++.h>
using namespace std;
struct Complex{
int real,imag;
bool operator<(const Complex &c) const{
return real*real+imag*imag<c.real*c.real+c.imag*c.imag;
}
};
int main(){
int n;
while(cin>>n){
priority_queue<Complex>p;
while(n--){
string s;
cin>>s;
if(s=="Pop"){
if(p.empty()){
cout<<"empty"<<endl;
}
else{
Complex current=p.top();p.pop();
cout<<current.real<<"+i"<<current.imag<<endl;
cout<<"SIZE = "<<p.size()<<endl;
}
}
else{
int a,b;
scanf("%d+i%d",&a,&b);
p.push({a,b});
cout<<"SIZE = "<<p.size()<<endl;
}
}
}
}
例题10.6 哈夫曼树(BUPT)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin>>n){
priority_queue<int,vector<int>,greater<int> >q;
while(n--){
int x;
cin>>x;
q.push(x);
}
int ans=0;
while(q.size()>1){
int a=q.top();q.pop();
int b=q.top();q.pop();
ans+=a+b;
q.push(a+b);
}
cout<<ans<<endl;
}
}
习题10.2 查找第K小的数(BUPT)
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
while(cin>>n){
set<int> s;
while(n--){
int tmp;cin>>tmp;
s.insert(tmp);
}
int k;cin>>k;
set<int>::iterator it;int i;
for(it=s.begin(),i=0;i<k;i++,it++){
if(i==k-1){
cout<<*it<<endl;
}
}
}
}
习题10.3 搬水果(JLU)
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
while(cin>>n){
if(n==0){
break;
}
priority_queue<int,vector<int>,greater<int> >q;
while(n--){
int x;cin>>x;
q.push(x);
}
int ans=0;
while(q.size()>1){
int a=q.top();q.pop();
int b=q.top();q.pop();
ans+=a+b;
q.push(a+b);
}
cout<<ans<<endl;
}
}
例题10.7 查找学生信息(THU)
#include<bits/stdc++.h>
using namespace std;
map<string,string> mp;
int main(){
int n;
cin>>n;
cin.get();
for(int i=0;i<n;i++){
string str;
getline(cin,str);
int pos=str.find(" ");
string key=str.substr(0,pos);
mp[key]=str;
}
int m;
cin>>m;
for(int i=0;i<m;i++){
string key;
cin>>key;
string ans=mp[key];
if(ans==""){
ans="No Answer!";
}
cout<<ans<<endl;
}
}
例题10.8 魔咒词典(ZJU)
#include<bits/stdc++.h>
using namespace std;
map<string,string> dic;
int main(){
string str;
while(getline(cin,str)){
if(str=="@END@"){
break;
}
int pos=str.find("]");
string key=str.substr(0,pos+1);
string val=str.substr(pos+2);
dic[key]=val;
dic[val]=key;
}
int n;cin>>n;
getchar();
while(n--){
string key;
getline(cin,key);
string ans=dic[key];
if(ans==""){
ans="what?";
}
else if(ans[0]=='['){
ans=ans.substr(1,ans.size()-2);
}
cout<<ans<<endl;
}
}
例题10.9 字串计算(PKU)
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
while(cin>>str){
map<string,int> number;
for(int i=0;i<=str.size();i++){
for(int j=0;j<i;j++){
string key=str.substr(j,i-j);
number[key]++;
}
}
map<string,int>::iterator it;
for(it=number.begin();it!=number.end();it++){
if(it->second>1){
cout<<it->first<<" "<<it->second<<endl;
}
}
}
}
习题10.4 统计同成绩学生人数(ZJU)
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
while(cin>>n){
if(n==0){
break;
}
map<int,int>mp;
for(int i=0;i<n;i++){
int x;cin>>x;
mp[x]++;
}
int y;cin>>y;
cout<<mp[y]<<endl;
}
}
习题10.5 开门人和关门人(ZJU)
#include<bits/stdc++.h>
using namespace std;
int m;
struct Time{
string id;
int h,m,s;
bool operator<(const Time& t) const{
if(h!=t.h){
return h<t.h;
}
if(m!=t.m){
return m<t.m;
}
return s<t.s;
}
};
vector<Time> q1,q2;
int main(){
cin>>m;
while(m--){
string s;cin>>s;
for(int i=0;i<2;i++){
string t1;
cin>>t1;
string h1=t1.substr(0,2);
int ih1=stoi(h1);
string m1=t1.substr(3,2);
int im1=stoi(m1);
string s1=t1.substr(6,2);
int is1=stoi(s1);
if(i==0){
q1.push_back({s,ih1,im1,is1});
}
else{
q2.push_back({s,ih1,im1,is1});
}
}
}
sort(q1.begin(),q1.end());
sort(q2.begin(),q2.end());
cout<<q1[0].id<<" "<<q2[q2.size()-1].id<<endl;
}
习题10.6 谁是你的潜在朋友(PKU)
#include<bits/stdc++.h>
using namespace std;
int n,m;
map<int,int> mp;
int num[210];
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
int t;cin>>t;
mp[i]=t;
num[t]++;
}
for(int i=0;i<n;i++){
if(num[mp[i]]<=1){
cout<<"BeiJu"<<endl;
}
else{
cout<<num[mp[i]]-1<<endl;
}
}
}