• 【学习笔记】树形结构基础


    目的

    mark一下紫书上的例题与操作,以备复习。


    内容

    各种套路

    UVa 122

    #include <bits/stdc++.h>
    using namespace std;
    char s[1007];
    struct Node
    {
    	bool have_value; int v;
    	Node *left,*right;
    	Node():have_value(false),left(NULL),right(NULL){}
    };
    bool failed;
    Node* root;
    Node* newnode() { return new Node();}
    void remove_tree(Node* u)
    {
      if(u == NULL) return;
      remove_tree(u->left);
      remove_tree(u->right);
      delete u;
    }
    void addnode(int v,char* s)
    {
    	int n=strlen(s);
    	Node* u=root;		//根节点 
    	for(int i=0;i<n;i++)
    	{
    		if(s[i]=='L')
    		{
    			if(u->left==NULL) u->left=newnode();
    			u=u->left;	//向左走 
    		}
    		else if(s[i]=='R')
    		{
    			if(u->right==NULL) u->right=newnode();
    			u=u->right;
    		}
    	}
    	if(u->have_value) failed=true;
    	u->v =v;
    	u->have_value=true;
    }
    bool read_input()
    {
    	failed=false;
    	root=newnode();
    	for(;;)
    	{
    		if(scanf("%s",s) != 1) return false;
    		if(!strcmp(s,"()")) break;
    		int v;
    		sscanf(&s[1],"%d",&v);
    		addnode(v,strchr(s,',')+1); 
    	}
    	return true;
    }
    bool bfs(vector<int>& ans)
    {
    	queue<Node*> q;
    	ans.clear();
    	q.push(root);
    	while(!q.empty())
    	{
    		Node* u=q.front(); q.pop();
    		if(!u->have_value) return false;
    		ans.push_back(u->v);
    		if(u->left != NULL) q.push(u->left);
    		if(u->right != NULL) q.push(u->right);
    	}
    	return true;
    }
    int main()
    {
    	vector<int> ans;
      	while(read_input())
    	{
    		if(!bfs(ans)) failed = 1;
    		if(failed) printf("not complete
    ");
    		else 
    		{
          		for(int i = 0; i < ans.size(); i++)
    			{
        	    	if(i != 0) printf(" ");
           			printf("%d", ans[i]);
          		}
          		puts("");
        	}
      	}
      	return 0;
    }
    

    UVa 548

    #include <bits/stdc++.h>
    using namespace std;
    const int maxv=10009;
    int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv],n;
    bool read_list(int* a)
    {
    	string line;
    	if(!getline(cin,line)) return false;
    	stringstream ss(line);
    	n=0;
    	int x;
    	while(ss>>x) a[n++]=x;
    	return n>0;
    }
    //把in_order[L1,R1]和post_order[L2,R2]建成一颗二叉树,返回树根 
    int build(int L1,int R1,int L2,int R2)
    {
    	if(L1>R1) return 0;
    	int root=post_order[R2];
    	int p=L1;
    	while(in_order[p] != root) p++; 
    	int cnt=p-L1;
    	lch[root]=build(L1,p-1,L2,L2+cnt-1);
    	rch[root]=build(p+1,R1,L2+cnt,R2-1);
    	return root;
    }
    long long best,best_sum;
    void dfs(int u,long long sum)
    {
    	sum+=u;
    	if(!lch[u] && !rch[u])
    		if(sum<best_sum || (sum==best_sum && u<best))
    		{
    			best=u;best_sum=sum;
    		}
    	if(lch[u]) dfs(lch[u],sum);
    	if(rch[u]) dfs(rch[u],sum);
    }
    int main()
    {
    	while(read_list(in_order))
    	{
    		read_list(post_order);
    		build(0,n-1,0,n-1);
    		best_sum=1000000000000;
    		dfs(post_order[n-1],0);
    		cout<<best<<"
    ";
    	}
    	return 0;
    }
    

    UVa 839

    #include <iostream>
    using namespace std;
    int solve(int& W)
    {
    	int B1=1,B2=1,W1,W2,D1,D2;
    	cin>>W1>>D1>>W2>>D2;
    	if(!W1) B1=solve(W1);
    	if(!W2) B2=solve(W2);
    	W=W1+W2;
    	if(B1&&B2&&W1*D1==W2*D2) return 1;
    	return 0;
    }
    int main()
    {
    	int T,W;
    	cin>>T;
    	while(T--)
    	{
    		if(solve(W)) cout<<"YES
    ";
    		else cout<<"NO
    ";
    		if(T) cout<<"
    ";
    	}
    	return 0;
    }
    

    UVa 10562

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=210;
    int n;
    char buf[maxn][maxn];
    void dfs(int r,int c)
    {
    	printf("%c(",buf[r][c]);
    	if(r+1<n && buf[r+1][c]=='|')
    	{
    		int i=c;
    		while(i-1>=0 && buf[r+2][i-1]=='-') i--;
    		while(buf[r+2][i]=='-' && buf[r+3][i]!='')
    		{
    			if(!isspace(buf[r+3][i])) dfs(r+3,i);
    			i++;
    		}
    	}
    	printf(")");
    }
    void solve()
    {
    	n=0;
    	for(;;)
    	{
    		fgets(buf[n],maxn,stdin);
    		if(buf[n][0]=='#') break;
    		else n++;
    	}
    	printf("(");
    	if(n)
    	{
    		for(int i=0;i<strlen(buf[0]);i++)
    			if(buf[0][i]!=' ')
    			{
    				dfs(0,i); break;
    			}
    	}
    	printf(")
    ");
    }
    int main()
    {
    	int T;
    	fgets(buf[0],maxn,stdin);
    	sscanf(buf[0],"%d",&T);
    	while(T--)
    	{
    		solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    CKA易错题整理:五、配置⽹络策略 NetworkPolicy
    CKA易错题整理:第四题:备份还原 etcd
    2022 暑期复健训练(持续更新)
    【其他】STL 容器初始化后占用空间大小测试
    2步 从百度网盘好友分享文件中导出文件目录
    简单易懂的时序数据压缩算法分析
    判断字符串是否可以转成BigDecimal(可转用小数,负数)
    RocketMq
    【leetCode】三数之和
    【leetCode】盛最多水的容器
  • 原文地址:https://www.cnblogs.com/noblex/p/7978910.html
Copyright © 2020-2023  润新知