1201题目:
http://ac.jobdu.com/problem.php?pid=1201
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
using namespace std;
class treepoint{
public:
int value;
treepoint* leftchild;
treepoint* rightchild;
};
class theRoot{
public:
treepoint *theRootPoint;
theRoot(){
theRootPoint=NULL;
}
~theRoot(){
theRootPoint=NULL;
}
void createTree(int value);
void pre();//前序遍历
void pre_play(treepoint *point);
void med();//中序遍历
void med_play(treepoint *point);
void las();//后序遍历
void las_play(treepoint *point);
void inputs(int n);
};
void theRoot::createTree(int value){//
if(theRootPoint==NULL)
{
theRootPoint=new treepoint;
theRootPoint->leftchild=NULL;
theRootPoint->rightchild=NULL;
theRootPoint->value=value;
}
else{
treepoint *temp=theRootPoint;
while(1){
if(temp->value==value)
return ;
if(temp->value>value){
if(temp->leftchild==NULL){
temp->leftchild=new treepoint();
temp->leftchild->value=value;
temp->leftchild->leftchild=NULL;
temp->leftchild->rightchild=NULL;
return ;
}
temp=temp->leftchild;
}
else{
if(temp->rightchild==NULL){
temp->rightchild=new treepoint();
temp->rightchild->value=value;
temp->rightchild->leftchild=NULL;
temp->rightchild->rightchild=NULL;
return ;
}
temp=temp->rightchild;
}
}
}
}
void theRoot::pre(){//
pre_play(theRootPoint);
cout<<endl;
}
void theRoot::pre_play(treepoint* point){//
if(point==NULL)
return ;
treepoint* temp=point;
cout<<point->value<<' ';
pre_play(temp->leftchild);
pre_play(temp->rightchild);
}
void theRoot::med(){
med_play(theRootPoint);
cout<<endl;
}
void theRoot::med_play(treepoint* point){
if(point==NULL)
return ;
treepoint* temp=point;
med_play(temp->leftchild);
cout<<temp->value<<' ';
med_play(temp->rightchild);
}
void theRoot::las(){
las_play(theRootPoint);
cout<<endl;
}
void theRoot::las_play(treepoint* point){
if(point==NULL)
return ;
treepoint* temp=point;
las_play(temp->leftchild);
las_play(temp->rightchild);
cout<<temp->value<<' ';
}
void theRoot::inputs(int n){
int temp;
for(int i=0;i<n;i++)
{
cin>>temp;
createTree(temp);
}
}
int main(){
int n;
theRoot* T;
while(scanf("%d",&n)!=EOF){
T= new theRoot();
T->inputs(n);
T->pre();
T->med();
T->las();
}
}
/**************************************************************
Problem: 1201
User: zhouyudut
Language: C++
Result: Accepted
Time:100 ms
Memory:2972 kb
****************************************************************/
1009题目:
http://ac.jobdu.com/problem.php?pid=1009
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
using namespace std;
class treepoint{
public:
int value;
treepoint* leftchild;
treepoint* rightchild;
};
class theRoot{
public:
treepoint *theRootPoint;
int lenth;
int prearr[20];
int medarr[20];
theRoot(){
theRootPoint=NULL;
}
~theRoot(){
theRootPoint=NULL;
}
void createTree(int value);
int getlenth(){
return lenth;
}
void pre();//前序遍历
void pre_play(treepoint *point);
void med();//中序遍历
void med_play(treepoint *point);
void las();//后序遍历
void las_play(treepoint *point);
void inputs(char s[]);
};
void theRoot::createTree(int value){//
if(theRootPoint==NULL)
{
theRootPoint=new treepoint;
theRootPoint->leftchild=NULL;
theRootPoint->rightchild=NULL;
theRootPoint->value=value;
}
else{
treepoint *temp=theRootPoint;
while(1){
if(temp->value==value)
return ;
if(temp->value>value){
if(temp->leftchild==NULL){
temp->leftchild=new treepoint();
temp->leftchild->value=value;
temp->leftchild->leftchild=NULL;
temp->leftchild->rightchild=NULL;
return ;
}
temp=temp->leftchild;
}
else{
if(temp->rightchild==NULL){
temp->rightchild=new treepoint();
temp->rightchild->value=value;
temp->rightchild->leftchild=NULL;
temp->rightchild->rightchild=NULL;
return ;
}
temp=temp->rightchild;
}
}
}
}
void theRoot::pre(){//
prearr[0]=0;//这个记录数组长度;
pre_play(theRootPoint);
// cout<<endl;
}
void theRoot::pre_play(treepoint* point){//
if(point==NULL)
return ;
treepoint* temp=point;
// cout<<point->value<<' ';
int i=++prearr[0];
prearr[i]=point->value;
pre_play(temp->leftchild);
pre_play(temp->rightchild);
}
void theRoot::med(){
medarr[0]=0;
med_play(theRootPoint);
// cout<<endl;
}
void theRoot::med_play(treepoint* point){
if(point==NULL)
return ;
treepoint* temp=point;
med_play(temp->leftchild);
// cout<<temp->value<<' ';
int i=++medarr[0];
medarr[i]=temp->value;
med_play(temp->rightchild);
}
void theRoot::las(){
las_play(theRootPoint);
cout<<endl;
}
void theRoot::las_play(treepoint* point){
if(point==NULL)
return ;
treepoint* temp=point;
las_play(temp->leftchild);
las_play(temp->rightchild);
cout<<temp->value<<' ';
}
void theRoot::inputs(char s[]){
lenth=strlen(s);
for(int i=0;i<lenth;i++)
{
createTree(s[i]-'0');
}
pre();
med();
}
bool judge(int pre1[],int med1[],int pre2[],int med2[]){
if(pre1[0]!=pre2[0])//判断长度是否相等
return false;
int len=pre1[0];
for(int i=1;i<=len;i++)
{
// cout<<"pre1[i]="<<pre1[i]<<" pre2[i]"<<pre2[i]<<" med1[i]"<<med1[i]<<" med2[i]"<<med2[i]<<endl;
if(pre1[i]!=pre2[i])
return false;
if(med1[i]!=med2[i])
return false;
}
return true;
}
int main(){
int n;
theRoot* T;
theRoot* test;
char s1[20];
char s2[20];
while(1){
scanf("%d",&n);
if(n==0)
return 0;
T= new theRoot();
cin>>s1;
T->inputs(s1);
for(int i=0;i<n;i++){
cin>>s2;
test=new theRoot();
test->inputs(s2);
if(judge(T->prearr,T->medarr,test->prearr,test->medarr))
cout<<"YES"<<endl;
else{
cout<<"NO"<<endl;
}
}
}
}
/**************************************************************
Problem: 1009
User: zhouyudut
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/