标签:
题目1009:二叉搜索树
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:5733
解决:2538
如果序列相同则输出YES,否则输出NO
2 567432 543267 576342 0
YES NO
1 #include <cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<string> 5 #include<cstring> 6 #include<vector> 7 using namespace std; 8 char tree[1500],t[1500]; 9 int main(){ 10 //freopen("D://INPUT.txt","r",stdin); 11 int n; 12 //cout<<n<<endl; 13 string s; 14 while(cin>>n&&n){ 15 cin>>s; 16 //build(tree,s); 17 memset(tree,‘-‘,sizeof(tree)); 18 int i=0; 19 int p; 20 while(i<s.length()){ 21 p=1; 22 while(tree[p]!=‘-‘){ 23 if(tree[p]>s[i]){ 24 p=p*2; 25 } 26 else{ 27 if(tree[p]<s[i]){ 28 p=p*2+1; 29 } 30 } 31 } 32 tree[p]=s[i++]; 33 } 34 while(n--){ 35 cin>>s; 36 memset(t,‘-‘,sizeof(t)); 37 i=0; 38 while(i<s.length()){ 39 p=1; 40 while(t[p]!=‘-‘){ 41 if(t[p]>s[i]){ 42 p=p*2; 43 } 44 else{ 45 if(t[p]<s[i]){ 46 p=p*2+1; 47 } 48 } 49 } 50 t[p]=s[i++]; 51 } 52 if(!strcmp(t,tree)){ 53 cout<<"YES"<<endl; 54 } 55 else{ 56 cout<<"NO"<<endl; 57 } 58 } 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4397276.html