pat 1145

1145 hash
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

  1. TSize is the maximum size of the hash table
  2. Quadratic probing with positive increments only is used to solve the collisions
  3. table size is better to be prime
  4. maximum size given by the user is not prime
  5. you must redefine the table size to be the smallest prime number whcih is larger than
    the size given by the user

//那就是要先写一个判断是否是素数的函数洛

 bool is_Prime(int x){
    if(x<=1){
    return false;
    }
    if(x==2){
       return true;
    }
   int up = sqrt(x);
   for(int i = 2;i<=up;i++){
         if(x%i == 0){
             return false;
}
return true;
   }
}


然后读一下输入规范
Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10
4
1.Each input file contains one test case
2. MSsize are the user-defined tbale size

if(prime(Msize()) == true){
           
 }else{
     int t = Msize;
     while(true){
       t++;
      if: prime(t)
       break;
    
     }
 }
 

. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10
5

int n;int m;cin>>n;cin>>m;
int hash[];
bool visit[];
for(int i  =0;i<tsize;i++){

}
for(int i  =0;i<n;i++){
   if(visit[i] == true){
       for(int k  = 1;k<tsize();k++){
          if[(temp[j] % tsize + k*k) == false]{
               hash[
          }
       }
   }else{
        hash[i];
        hash[i] = temp[j];
        j++;
   }
}

我平方探测探测到一定

写一下把 思路我也说不清楚

#include <iostream>
#include <vector>
#include<math.h>

using namespace std;
bool is_prime(int x){
    if(x<=1){
        return false;
    }
    if(x<=2){
        return true;
    }
    int up = sqrt(x);
    for(int i  = 2;i<=up;i++){
        if(x%i == 0){
            return false;
        }
    }
    return true;
}
double len = 0;

int main(){
    int tsize; int n;int m;
    cin>>tsize;cin>>n;cin>>m;

    while(true){
        if(is_prime(tsize)){
            break;
        }
        tsize++;
    }
        int table[tsize];
        bool visit[tsize];
    //insert
    for(int i = 0;i<tsize;i++){
        visit[i] = false;
        table[i] = 0;
    }
    for(int i = 0;i<n;i++){
        int key;
        cin>>key;
        int k = 0;
        for(k = 0;k<tsize;k++){
            if(visit[(key+k*k)%tsize] == false){
                table[(key+k*k)%tsize] = key;
                visit[(key+k*k)%tsize] = true;
                break;
            }
        }
        if(k == tsize){
            cout<<key<<" cannot be inserted."<<endl;
        }
    }
//     for(int i = 0;i<tsize;i++){
//         cout<<table[i]<<" ";
//     }
   
    for(int i = 0;i<m;i++){
        int key;
        cin>>key;
        int k  =0 ;
        
        for( k = 0;k<tsize+1;k++){
                len++;
            if(table[(key+k*k)%tsize] == key||table[(key+k*k)%tsize] == 0) {
          
                break;
            }
        }
      
//         cout<<key<<" "<<len<<endl;
    }
//             cout<<len;
        printf("%.1f",(len)/m);
    cout<<endl;
    
}












主要还是查找次数的上限为多少让人异或
休息一下干拓扑排序
排序

数论
排序
dfs+数论
建立树
大约四点

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.

1.This is a problem givenin the Graduate Entrance Exam in 2018 which of the following
is Not a topological order obtained from the given directed graph Now you are supposed to
write a program to tet each of the options.

  1. 思路就是建图 删去边就可以了
  2. 建立一个图,然后删去一条边
  3. 似乎都不需要见图
  4. 时间复杂度卡的很严
  5. 所以我们需要做那件事对用c++

先在文本编辑器写一些伪代码

读一下输入格式,不过我感觉也不太需要读
ach input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

  1. input file contians one test case. For each case, the first line gives two positive integers
  2. the number of vertices in the graph, and M the number of directed edges
  3. then M lines follow each gives the start and the end vertices of an edge.
  4. The vertices are numbreed from 1 to N
  5. after the graph there is another positive integer Then K lines of query folo
  6. each give a permutation of all the vertices
  7. All the numbers in a line are separated by a space
建立图  没怎么用C++建国图 链式前向星  还还是算了

vector<int>[] graph;
int n;int m;cin>>n;cin>>m;
int indree[n];
for(int i = 0;i<m;i++){
   int u;int v;cin>>u;cin>>v;
   graph[u].push_back(v);
   indree[v]++;     
}//双向边
int q;cin>>q;
bool help = true;
for(int i = 0;i<q;i++){
    int v ;cin>>v;
    help = true;
    for(int j = 0;j<v;j++){
    int u;cin>>u;
    if(indree[u] == 0){
        for(int k = 0;k<graph[u].size();k++){
            indree[graph[u][k]]--;
        }
    }else{
       help = false;
       break;
    }
    
    }
    
    if(help == false){
       continue;
    }else{
      if(i!= n-1){
       cout<<i<<" ";
       }else{
         cout<<i;
       }
    }
    
}


整理得:

#include<iostream>
#include<vector>
using namespace std;
int first = 1;
int main(){

int n;int m;cin>>n;cin>>m;
int indree[n+1];
vector<int>graph [n+1];
    for(int i = 1;i<n+1;i++){
    indree[i] = 0;
}
for(int i = 0;i<m;i++){
   int u;int v;cin>>u;cin>>v;
   graph[u].push_back(v);
   indree[v]++;     
}//双向边

int q;cin>>q;
bool help = true;
int indree2[n+1];
for(int i = 0;i<q;i++){
 
    help = true;
    for(int i = 1;i<n+1;i++){
        indree2[i] = indree[i];
        // cout<<indree2[i];
    }
    for(int j = 0;j<n;j++){
        int u;cin>>u;
    if(indree2[u] == 0){
        for(int k = 0;k<graph[u].size();k++){
            indree2[graph[u][k]]--;
        }
    }else{
       help = false;
    }
    
    }
    
    if(help == false){
       if(first == 1){
           cout<<i;
           first = 0;
       }else{
           
           cout<<" "<<i;
       }
       continue;
    }else{
  continue;
    }
    
}
}

1136 A Delayed Palindrome

从定义上,肯定是一道水题
Consider a positive integer N written in standard notation with k+1 digits a
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

Given any positive integer, you are supposed to find its paired palindromic number.

97152

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

  1. Non-palindromic number can be paired with palindromic ones via a seris of opertaions.1
  2. the non-palinromic number is reversed and the result ids added to the original number
  3. if the result is not a pailindfnbumber this is repeated until it give a palinromic number
  4. Such number is called a delayed palindrome

Given any positive integer, you are supposed to find its paired palindromic number.

  string s;
  cin>>s;
  string s2 = reverse(s);
  
for(int i = 0;i<10;i++){
  //进行10次操作
  // 如果得到了就退出10次没得到就输出不行
  if(s == reverse(s.begin(),s.end()){
  cout<<s;
  return 0;}
 stringadd(s,reverse(s.begin(),s.end());
   
}
cout<<"no ind"
returno;

string stringadd(string a ,string b){
  //双指针
  int i = 0;
  int j = 0;
  int carry = 0;
  //由题意两个字符串长度一样,所以不需要考虑长度不一样怎么办
  //模拟相加  1 2 3    3 2 1 
  //          3 2 1   1 2 3 
  //       4 4 4 
  // 4 4 4 
  //可以就写针对题目所需要的加法而不需要通用的
   string ret = "";
   while(i<a.length&&j<b.length){
      int value = (a[i] - '0')+(b[i]-'0')+carry;
      if(ret[i] > 10){
         carry =1;
       value-=10;
      }else{
         carry = 0;
      }
      ret[i] = value+'0';
}
  revers(ret.begin(),ret.end());
}
string reverse(string s){
    string ret = "";
   int len = s.length();
   for(int i = len-1;i>=0;i--){
       ret[i] = s[i];
}
return ret;
}

放自己的调试器整理一下
dei:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string reverse1(string s){
   string ret ="";
   int len = s.length();
   for(int i = len-1;i>=0;i--){
       ret+=s[i];
}
    // cout<<ret;
return ret;
}
string stringadd(string a ,string b){
  //双指针
  int i = 0;
  int j = 0;
  int carry = 0;
  string ret = "";
    
   while(i<a.length()&&j<b.length()){
      int value = (a[i] - '0')+(b[j]-'0')+carry;
      if(value >=10){
         carry =1;
         value-=10;
      }else{
         carry = 0;
      }
      ret += value+'0';
       i++;
       j++;
}
    if(carry == 1){
        ret+='1';
    }
  ret = reverse1(ret);
     // cout<<ret<<endl;
    return ret;
}
int main(){
   string s;
   cin>>s;
   string temp;
for(int i = 0;i<10;i++){
  if(s == reverse1(s)){
     cout<<s<<" is a palindromic number.";
      return 0;
   }else{
      temp = stringadd(s,reverse1(s));
      cout<<s<<" + "<<reverse1(s)<<" = "<<temp<<endl;
      s = temp;
   }
}
   
cout<<"Not found in 10 iterations.";
return 0;
           }


For a student taking the online course “Data Structures” on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. The final grade is calculated by G=(G
mid−term
The problem is that different exams have different grading sheets. Your job is to write a program to merge all the grading sheets into one.

1137 Final Grading
水题,但是我 的c++语言能力及其一般,估计还是要搞一会儿的
说实话 把第二页搞定有点小难
现在时间不多了
好想睡觉
累死了

时间卡的很紧又用不了java
For a student taking the online course “Data Structures” on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. The final grade is calculated by G=(G
mid−term

×40%+G
final

×60%) if G
mid−term

G
final

, or G
final

will be taken as the final grade G. Here G
mid−term

and G
final

are the student’s scores of the mid-term and the final exams, respectively.

The problem is that different exams have different grading sheets. Your job is to write a program to merge all the grading sheets into one.

  1. 只要是真的看不懂题目再说什么
  2. 不是看不懂英文,真的
  3. 是不清楚它在表达上么
  4. 我就随便乱写把

状态: 像一年级的小朋友被妈妈叫去刷牙,睡觉一样

在这里插入代码片
#include<iostream>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
   struct Student{
       string name;
        int Gp = -1;
        double Gf = -1;
        double Gm= -1;
        double G = -1;
    };

bool cmp(Student s1,Student s2){
    if(s1.G != s2.G){
   return  s1.G > s2.G;
    }else{
      return  s1.name < s2.name;
    }
}
    map<string,int> map2;
    int main(){
       int a;int b;int c;
       cin>>a;cin>>b;cin>>c;
       int sum = a+b+c;
       Student sts[sum+1];
        //
        int index = 1;
        for(int i = 0;i<a;i++){
            string name;int grade;
         
            cin>>name;cin>>grade;
            if(grade<0){
                continue;
            }
            if(map2[name] == 0){
                map2[name] =  index;
                index++;
            }
            sts[map2[name]].name = name;
            sts[map2[name]].Gp = grade;
        }
        for(int i = 0;i<b;i++){
            string name;int grade;
            cin>>name;cin>>grade;
            if(grade<0){
                continue;
            }
            if(map2[name] == 0){
                map2[name] =  index;
                index++;
            }
             sts[map2[name]].name = name;
            sts[map2[name]].Gm = grade;
        }
        for(int i = 0;i<c;i++){
            string name;int grade;
            cin>>name;cin>>grade;
           if(grade<0){
               continue;
           }
            if(map2[name] == 0){
                map2[name] =  index;
                index++;
            }
             sts[map2[name]].name = name;
            sts[map2[name]].Gf = grade;
        }
        for(int i = 0;i<index;i++){
            if(sts[i].Gm <= sts[i].Gf){
             sts[i].G  = sts[i].Gf;   
            }else{
             
            sts[i].G =(int)(((double)sts[i].Gm*0.4+(double)(sts[i].Gf*0.6))+0.5);
            
            }
        }
        sort(sts,sts+index+1,cmp);
        for(int i = 0;i<index;i++){
            if(sts[i].Gp >= 200&&sts[i].Gp <= 900&&sts[i].G >= 60 && sts[i].G<=100&&sts[i].Gm <=100 &&sts[i].Gf<=100){
    cout<<sts[i].name<<" "<<sts[i].Gp<<" "<<sts[i].Gm<<" "<<sts[i].Gf<<" "<<sts[i].G<<endl;
            }
        }
        
       }
    
    




我也不能特别轻松的做出来就不解释了

下一题
像跑了500米一样

1114 Family Property
并查集

his time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

翻译:
你应该帮助我们收集家庭财产
给每个家庭成员和房地产在他的名下
你应该要知道家庭的的大小 和平均面
比较经典的并查集
和深度优先

10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100

1115 Counting Nodes in a Binary Search Tree
一眼深度优先:
考察二叉搜索树

  1. A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

1、 A Binary Search Tree(BST) is recursively defined as a binary tree which has the following
The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than the nodes’s key.
Both the left and right subtrees must also be binary search trees.
Insert a sequence of number s into an intially empty binary search tree Then you are supposed to count the total nnumber of nodes in the lowest 2 levels of the resulting tree.

The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.

把一脸串数据放到树中,
计算最后l两层

深度两层
计算深度吗需要
有序数组

import java.util.*;
import java.io.*;
public class Main{
    static Scanner scan =new Scanner(System.in);
    static class Node{
        Node left;
        Node right;
        int data;
        int depth;
        public Node(){
           int depth = 1;
           int data = 0;
            left = null;
            right = null;
        }
    }
   static  int low1 =0;
   static  int low2 = 0;
//     public static void dfs(Node root){
//         if(root == null){
//             return;
//         }
//         if(root.depth == 2){
//             low2++;
//             System.out.println(root.data);
//         }
//         if(root.depth == 1){
//             low1++;
//             System.out.println(root.data);
//         }
//         dfs(root.left);
//         dfs(root.right);
        
//     }
        
        
  static  int  getDepth(Node root){
        if(root == null){
            return 0;
        }
        return root.depth;
    }
    static Node insert(Node root,int val){
        if(root == null){
            Node node = new Node();
            node.data = val;
            return node;
        }
        if(val > root.data){
         root.right = insert(root.right,val);
   
         
        }else{
         root.left =    insert(root.left,val);
           
        }
         int right  = getDepth(root.right);
        int left = getDepth(root.left);
        root.depth = Math.max(right,left)+1;
        return root;
    }
    public static void main(String [] args){
        int n = scan.nextInt();
        Node root = null;
        for(int i = 0;i<n;i++){
            int val = scan.nextInt();
           root = insert(root,val);
        }

        int len = root.depth;
//         System.out.println(len);
       Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            if(len == 1){
                low2 = queue.size();
            }
            if(len == 0){
                low1 = queue.size();
            }
            int  t = queue.size();
        for(int i = 0;i<t;i++){
            Node q = queue.poll();
//             System.out.print(q.data+" ");
            if(q.left != null){    
                queue.add(q.left);
            }
            if(q.right != null){
                queue.add(q.right);
            }
        }
            len--;
        }
        System.out.print(low1+" + "+ low2+" = "+(low1+low2));
    }
}

深度计算的好想还有问题
过了就算了

再整理一下

import java.util.*;
import java.io.*;
public class Main{
    static Scanner scan =new Scanner(System.in);
    static class Node{
        Node left;
        Node right;
        int data;
        int depth;
        public Node(){
           int depth = 1;
           int data = 0;
            left = null;
            right = null;
        }
    }
   static  int low1 =0;
   static  int low2 = 0;
//     public static void dfs(Node root){
//         if(root == null){
//             return;
//         }
//         if(root.depth == 2){
//             low2++;
//             System.out.println(root.data);
//         }
//         if(root.depth == 1){
//             low1++;
//             System.out.println(root.data);
//         }
//         dfs(root.left);
//         dfs(root.right);
        
//     }
        
        
  static  int  getDepth(Node root){
        if(root == null){
            return 0;
        }
        return root.depth;
    }
    static Node insert(Node root,int val){
        if(root == null){
            Node node = new Node();
            node.data = val;
            node.depth = 1;
            return node;
        }
        if(val > root.data){
         root.right = insert(root.right,val);
            
         
        }else{
         root.left =    insert(root.left,val);
            
        }
        int right  = getDepth(root.right);
        int left = getDepth(root.left);
        root.depth = Math.max(right,left)+1;
        return root;
    }
    public static void main(String [] args){
        int n = scan.nextInt();
        Node root = null;
        for(int i = 0;i<n;i++){
            int val = scan.nextInt();
           root = insert(root,val);
        }

        int len = root.depth;
//         System.out.println(len);
       Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            if(len == 2){
                low2 = queue.size();
            }
            if(len == 1){
                low1 = queue.size();
            }
            int  t = queue.size();
        for(int i = 0;i<t;i++){
            Node q = queue.poll();
//             System.out.print(q.data+" ");
            if(q.left != null){    
                queue.add(q.left);
            }
            if(q.right != null){
                queue.add(q.right);
            }
        }
            len--;
        }
        System.out.print(low1+" + "+ low2+" = "+(low1+low2));
    }
}

复盘也是经典的套路题
没什么说的
我没有更高的理解所以不解释了‘

This time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.
1.
合并的顺序以及合并过程维护根的性质

在这里插入代码片
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
 int fa[10100];
 int sets[10100];
 int people[10100];
 int areas[10100];
 bool visit[10100];
int findf(int x){
   if(x != fa[x]){
      fa[x]  = findf(fa[x]); 
   }
    return fa[x];
}
void Union(int a,int b){
    int aRoot = findf(a);
    int bRoot = findf(b);
    if(aRoot == bRoot){
        return;
    }
    if(aRoot < bRoot){
        fa[bRoot] = aRoot;
    }else{
        fa[aRoot] = bRoot;
    }
}

bool cmp(int r1,int r2){
  double avarea1 = (double)areas[r1]/people[r1];
  double avarea2 = (double)areas[r2]/people[r2];
   if(avarea1 != avarea2){
       return avarea1>avarea2;
   }else{
       return r1 < r2;
   }
}
int main(){
    int n;
    cin>>n;
    for(int i = 0;i<10100;i++){
    fa[i] = i;
    areas[i] = 0;
    sets[i] =  0;
    people[i] = 1;
   visit[i] = 0;
}
    for(int i = 0;i<n;i++){
     int id;int fa; int ma; int k;
        cin>>id;cin>>fa;cin>>ma;cin>>k;
        visit[id] = true;
        if(fa != -1){
            Union(id,fa);
            visit[fa] = 1;
        }
        if(ma != -1){
            Union(id,ma);
            visit[ma] = 1;
        }
       
        for(int j = 0;j<k;j++){
             int child; cin>>child; 
            visit[child] = true;
            Union(id,child);
        }
        
        int m; int area;cin>>m;cin>>area;
        sets[id] = m;
        areas[id] = area;
    }
    vector<int> vec;
    set<int> set1;
    for(int i = 0;i<10100;i++){
        if(visit[i] == 1){
            int root = findf(i);
            if(i!=root){
             sets[root] += sets[i];
             areas[root] += areas[i];
             people[root] += people[i];
             if(set1.find(root) == set1.end()){
                 vec.push_back(root);
             }
              set1.insert(root);
           
            }else{
             if(set1.find(root) == set1.end()){
                 vec.push_back(root);
             }
              set1.insert(root);
            }
        }
    }
    cout<<vec.size()<<endl;
    sort(vec.begin(),vec.end(),cmp);
    for(int i =0;i<vec.size();i++){
        printf("%04d",vec[i]);
        cout<<" "<<people[vec[i]]<<" ";
        printf("%.3f",(double)sets[vec[i]]/people[vec[i]]);
        cout<<" ";
        printf("%.3f",(double)areas[vec[i]]/people[vec[i]]);
        cout<<endl;
    }
    
}


才输学前
还有八题不会要熬夜把
窝巢
不要啊
那就硬搞把
欧拉图

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous Seven Bridges of Konigsberg problem in 1736. It has been proven that connected graphs with all vertices of even degree have an Eulerian circuit, and such graphs are called Eulerian. If there are exactly two vertices of odd degree, all Eulerian paths start at one of them and end at the other. A graph that has an Eulerian path but not an Eulerian circuit is called semi-Eulerian. (Cited from https://en.wikipedia.org/wiki/Eulerian_path)

Given an undirected graph, you are supposed to tell if it is Eulerian, semi-Eulerian, or non-Eulerian.

uppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

A registration card number of PAT consists of 4 parts:

the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
the 2nd - 4th digits are the test site number, ranged from 101 to 999;
the 5th - 10th digits give the test date, in the form of yymmdd;
finally the 11th - 13th digits are the testee’s number, ranged from 000 to 999.
Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in “zigzagging order” – that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.6666666666666666

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

#include<iostream>
#include<map>
using namespace std;
struct Node{
    int data;
    Node* left;
    Node* right;
    
};
int n;
//传统建立树就可以了
int postorder[100];
map<int,int>premap;
map<int,int>postmap;
bool ret = true;
Node* dfs(int preorder[],int s1,int e1,int postorder[],int s2,int e2){
    Node* node = new Node();
    if(s1>e1|| s2>e2){
        return nullptr;
    }
    node->data = preorder[s1];
    if(s1 == e1 ){
        return node;
    }
    
    // 计算儿子有多少
    int size =  postmap[preorder[s1]] - s2;
    // 计算左孩子儿子有多少 划分一个区间建立一棵左树
    if(size == 1){
        ret = false;
    }
    int lsize = postmap[preorder[s1+1]] - s2+1;
    // 两个相见计算右孩子有多少, 划分区间建立一颗右树
    int rsize = size - lsize;
    //
    node->left = dfs(preorder,s1+1,s1+lsize,postorder,s2,s2+lsize-1);
node->right = dfs(preorder,s1+lsize+1,e1,postorder,s2+lsize,s2+rsize+lsize-1);
    return node;
   
}
int first = 1;
void dfs2(Node* root){
    if(root == nullptr){
        return;
    }
    dfs2(root->left);
    if(first == 1){
        first = 0;
        cout<<root->data;
    }else{
        cout<<" "<<root->data;
    }
    dfs2(root->right);
    
}
int main(){
cin>>n;
int preorder[n];


for(int i = 0;i<n;i++){
   cin>> preorder[i];
    premap[preorder[i]] = i;
}
for(int i = 0;i<n;i++){
    cin>> postorder[i];
    postmap[postorder[i]] = i;
}
   Node* root =  dfs(preorder,0,n-1,postorder,0,n-1);
   if(ret == true){
       cout<<"Yes"<<endl;
   }else{
       cout<<"No"<<endl;
   }
   dfs2(root);
    cout<<endl;
}

熟悉指针

A registration card number of PAT consists of 4 parts:

the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
the 2nd - 4th digits are the test site number, ranged from 101 to 999;
the 5th - 10th digits give the test date, in the form of yymmdd;
finally the 11th - 13th digits are the testee’s number, ranged from 000 to 999.
Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

定义题目需要的结构体
我曹这题还是有点难度的
有点意识 主要没什么时间了 看一下思路了
下一题

1126 Eulerian Path
分数 25

入度数组考察欧拉性质

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值