⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zp1109_word tree.cpp

📁 一个acm题目系统会自动删除debug和release目录
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
//#define cin fin
//#define cout fout
#include<iostream>
#include<string>
//#include<fstream>
using namespace std;

//ofstream fout("output.txt");

struct Node
{
   string str;
   string dic;
   struct Node* left;
   struct Node* right;
};

void attree(string dc,string st,struct Node* cur)
{
   if(st>(cur->str))
   {
      if(cur->right==NULL)
      {
         cur->right=new(struct Node);
         cur->right->str=st;
         cur->right->dic=dc;
         cur->right->left=NULL;
         cur->right->right=NULL;
      }
      else
        attree(dc,st,cur->right);
   }
   else
   {
      if(cur->left==NULL)
      {
         cur->left=new(struct Node);
         cur->left->str=st;
         cur->left->dic=dc;
         cur->left->left=NULL;
         cur->left->right=NULL;
      }
      else
        attree(dc,st,cur->left);
   }
}
void retree(string st,struct Node* cur)
{
  if (cur==NULL)
    cout<<"eh"<<endl;
  else
  if (st>cur->str)
    retree(st,cur->right);
  else
  if (st<cur->str)
    retree(st,cur->left);
  else
    cout<<cur->dic<<endl;
}

int main(int argc, char* argv[])
{
  //ifstream fin("zp1109.txt");

  string s,sr,sd;
  struct Node* root;

  s="";
  while (s.length()==0) getline(cin,s);

  int i=0;sr="";sd="";
  while (s[i]!=' ') { sr+=s[i++]; } i++;
  while (s[i]) { sd+=s[i++]; }

  root=new(struct Node);
  root->str=sd;
  root->dic=sr;
  root->left=NULL;
  root->right=NULL;

  getline (cin,s);
  while (s.length())
  {
    int i=0;sr="";sd="";
    while (s[i]!=' ') { sr+=s[i++]; } i++;
    while (s[i]) { sd+=s[i++]; }

    attree(sr,sd,root);

    getline (cin,s);
  }

  while (cin>>s)
    retree(s,root);

  return 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -