2016夏季练习——二叉树
2016-07-20 17:20
274 查看
来源:HDU3791
建树比较:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
int n;
string str;
bool flag=1;
const int MAXN = 5000;
int tra[MAXN];
int trb[MAXN];
bool vis[MAXN];
int da[20],db[20];
void push1(int rt,int data){
// cout<<rt<<" "<<data<<" "<<tra[rt]<<endl;
if(tra[rt]==-1) {
tra[rt]=data;
da[data]=rt;
return ;
}
if(data>tra[rt])push1(rt<<1|1,data);
else push1(rt<<1,data);
}
void push2(int rt,int data){
//cout<<rt<<" "<<data<<" "<<trb[rt]<<endl;
if(trb[rt]==-1){
trb[rt]=data;
db[data]=rt;
return ;
}
if(data>trb[rt]) push2(rt<<1|1,data);
else push2(rt<<1,data);
}
void build(){
//cout<<"build:"<<endl;
memset(tra,-1,sizeof(tra));
int d;
for(int i=0;i<str.length();i++){
d=str[i]-'0';
push1(1,d);
}
}
void fuck(){
memset(trb,-1,sizeof(trb));
int d;
for(int i=0;i<str.length();i++){
d=str[i]-'0';
push2(1,d);
}
}
int main(){
while(scanf("%d",&n)!=EOF&&n){
getchar();
memset(da,0,sizeof(db));
getline(cin,str);
int len=str.length();
//cout<<str<<endl;
build();
while(n--){
flag=1;
getline(cin,str);
// cout<<str<<endl;
if(str.length()!=len){
cout<<"NO"<<endl;
continue;
}
memset(db,0,sizeof(db));
fuck();
for(int i=0;i<10;i++){
if(da[i]!=db[i]){
flag=0;
break;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
建树比较:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
int n;
string str;
bool flag=1;
const int MAXN = 5000;
int tra[MAXN];
int trb[MAXN];
bool vis[MAXN];
int da[20],db[20];
void push1(int rt,int data){
// cout<<rt<<" "<<data<<" "<<tra[rt]<<endl;
if(tra[rt]==-1) {
tra[rt]=data;
da[data]=rt;
return ;
}
if(data>tra[rt])push1(rt<<1|1,data);
else push1(rt<<1,data);
}
void push2(int rt,int data){
//cout<<rt<<" "<<data<<" "<<trb[rt]<<endl;
if(trb[rt]==-1){
trb[rt]=data;
db[data]=rt;
return ;
}
if(data>trb[rt]) push2(rt<<1|1,data);
else push2(rt<<1,data);
}
void build(){
//cout<<"build:"<<endl;
memset(tra,-1,sizeof(tra));
int d;
for(int i=0;i<str.length();i++){
d=str[i]-'0';
push1(1,d);
}
}
void fuck(){
memset(trb,-1,sizeof(trb));
int d;
for(int i=0;i<str.length();i++){
d=str[i]-'0';
push2(1,d);
}
}
int main(){
while(scanf("%d",&n)!=EOF&&n){
getchar();
memset(da,0,sizeof(db));
getline(cin,str);
int len=str.length();
//cout<<str<<endl;
build();
while(n--){
flag=1;
getline(cin,str);
// cout<<str<<endl;
if(str.length()!=len){
cout<<"NO"<<endl;
continue;
}
memset(db,0,sizeof(db));
fuck();
for(int i=0;i<10;i++){
if(da[i]!=db[i]){
flag=0;
break;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}