Prototypes analyze

描述

ALpha Ceiling Manufacturers (ACM) is analyzing the properties of its new series of Incredibly Collapse-Proof Ceilings (ICPCs).  An ICPC consists of n layers of material, each with a different value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will take the collapse-resistance values of the layers, store them in a binary search tree, and check whether the shape of this tree in any way correlates with the quality of the whole construction. Because, well, why should it not?  To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer to the bottom layer,  and inserts them one-by-one into a tree. The rules for inserting a value v are:

• If the tree is empty, make v the root of the tree.

• If the tree is not empty, compare v with the root of the tree.

• If v is smaller, insert v into the left subtree of the root,

• otherwise insert v into the right subtree.

 

ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take each group of ceiling prototypes that have trees of the same shape and analyze them together. For example , assume ACM is considering five ceiling prototypes with three layers each, as described by Sample Input 1 and shown in Figure C.1. Notice that the first prototype’s top layer has collapseresistance value 2, the middle layer has value 7, and the bottom layer has value 1. The second prototype has layers with collapse-resistance values of 3, 1, and 4 – and yet these two prototypes induce the same tree shape, so ACM will analyze them together. Given a set of prototypes, your task is to determine how many different tree shapes they induce.

 

输入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8).
Each test case specifies :
● Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze,
and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes.
● The next n lines describe the ceiling prototypes. Each of these lines contains k distinct
integers ( between 1 and 1e6, inclusive ) , which are the collapse-resistance values of the
layers in a ceiling prototype, ordered from top to bottom.
输出
For each test case generate a single line containing a single integer that is the number of different tree
shapes.
样例输入
1
5 3
2 7 1
1 5 9
3 1 4
2 6 5
9 7 3
样例输出
4
题意:

给出n行数,每行m个数,每行数按规则建树,最后输出有多少课不同的数(不同是指类型不同,如2 7 1和 3 1 4建的树就相同)

先按规则建树,建完后先序遍历一次,中序遍历一次找出树形,和以前出现的比较看是否有相同树形即可


#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<functional>
#include<vector>
#include<queue>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int a[4000100];//存树
string s,s1;//存先序和中序结果
void ss(int s,int i)//建树
{
    if(a[i]==0)
    {
        a[i]=s;
        return ;
    }
    if(s>a[i])
    {
        ss(s,2*i+1);
    }
    else
    {
        ss(s,2*i);
    }
}
void zx(int i)//中序遍历
{
    if(a[i]==0)
    {
        s+='0';//该节点为空,遍历记为0;
        return;
    }
    else
    {
        zx(2*i);
        s+='1';//该节点不为空,遍历记为1;
        zx(2*i+1);
    }
}
void xx(int i)//先序遍历
{
    if(a[i]==0)
    {
        s1+='0';
        return;
    }
    else
    {
        s1+='1';
        xx(2*i);
        xx(2*i+1);
    }
}

int main()
{
   int T;
   scanf("%d",&T);
   while(T--)
   {

       string b[51],c[51];//存不同的树形
       int t=0;
       int n,m;
       scanf("%d%d",&n,&m);
       while(n--)
       {
           memset(a,0,sizeof(a));
           for(int i=0;i<m;i++)
           {
               int g;
               scanf("%d",&g);
               ss(g,1);
           }
           zx(2);
           s+='2';//根节点记为2
           zx(3);
           s1+='2';
           xx(2);
           xx(3);
           int h=0;
           //cout<<s1<<"  "<<s<<endl;
           for(int i=0;i<t;i++)
           {
               if(s==b[i],s1==c[i])//比较是否有相同树形
               {
                   h=1;
                   break;
               }
           }
           if(h==0)
            b[t]=s,c[t]=s1,t++;
            s.clear();
            s1.clear();

       }
       printf("%d\n",t);
   }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值