看来一个大牛的code,好厉害,完全不用结构体啥的去建树,就只利用数组的下标关系去记录,赞~
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define N 1005
char str[15];
int tree[(1<<10)+10];//位运算加括号啊!!!否则会不明不白的WA!!!
bool tag;
void gao(char *s){
int i,j;
for(j = 0, i = 2; s[j]; i<<=1,j++){
if(tree[i] == 2){
tag = 1;cout<<str[j]<<endl;
return ;
}
if(s[j] == '0')tree[i] = 1;
else{
i++;
tree[i] = 1;
}
}
if(tree[i] == 1){
tag = 1;
return ;
}
tree[i] = 2;
}
int main(){
int t = 1;
tag = 0;
memset(tree,0,sizeof(tree));
while(scanf("%s",str) != EOF){
if(str[0] == '9'){
printf("Set %d is ", t++);
printf(tag ? "not immediately decodable\n" : "immediately decodable\n");
tag = 0;
memset(tree,0,sizeof(tree));
}else
if(!tag){
gao(str);
}
}
return 0;
}
本文介绍了一种使用位运算而非传统结构体来构建树形数据结构的方法,并通过一个具体实例展示了如何仅利用数组下标关系来记录节点关系。这种方法不仅节省内存,还简化了代码实现。
2696

被折叠的 条评论
为什么被折叠?



