1. 程式人生 > 實用技巧 >資料結構 散列表7.12 三角形遊戲

資料結構 散列表7.12 三角形遊戲

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

typedef struct TNode *Position;
typedef Position BinTree;
typedef long long int ElementType;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
    int Height; 
    int times;
};


Position FindMin( BinTree BST ){
    
if(!BST || !BST->Left)return BST; return FindMin(BST->Left); } Position FindMax( BinTree BST ){ if(!BST || !BST->Right)return BST; return FindMax(BST->Right); } BinTree Delete(BinTree BST,ElementType X) { Position Tmp; if(!BST) printf("要刪除的元素未找到"); else
{ if(X < BST->Data) BST->Left = Delete(BST->Left,X);//左子樹遞迴刪除 else if(X > BST->Data) BST->Right = Delete(BST->Right,X);//右子樹遞迴刪除 else{//BST就是要刪除的點 //如果被刪除的結點有左右兩個子結點 if(BST->Left&&BST->Right) { Tmp
= FindMin(BST->Right); BST->Data = Tmp->Data; BST->Right = Delete(BST->Right,BST->Data); } else{//被刪除的結點只有一個或無子節點 Tmp = BST; if(!BST->Left) BST = BST->Right; else BST = BST->Left; free(Tmp); } } } return BST; } int Max( int a,int b) { return a > b ? a : b; } int Height(BinTree t) { int hl,hr; if(!t)return -1; hl=Height(t->Left); hr=Height(t->Right); if(hl>hr) return ++hl; else return ++hr; } int GetHeight(BinTree T){ if(!T)return 0; return T->Height; } BinTree SingleLeftRotation(BinTree A) { //注意 A必須有一個左子結點B //將A和B做左單旋,更新A和B的高度,返回新的結點B BinTree B = A->Left; A->Left = B->Right ; B->Right = A; A->Height = Max( GetHeight(A->Left),GetHeight(A->Right))+1; B->Height = Max( GetHeight(B->Left),A->Height)+1; return B; } BinTree SingleRightRotation(BinTree A) { BinTree B = A->Right ; A->Right = B->Left; B->Left = A; A->Height = Max( GetHeight(A->Left),GetHeight(A->Right))+1; B->Height = Max( GetHeight(B->Right),A->Height)+1; return B; } BinTree DoubleLeftRightRotation(BinTree A) { /* 注意:A必須有一個左子結點B,且B必須有一個右子結點C */ /* 將A、B與C做兩次單旋,返回新的根結點C */ /* 將B與C做右單旋,C被返回 */ A->Left = SingleRightRotation(A->Left); /* 將A與C做左單旋,C被返回 */ return SingleLeftRotation(A); } BinTree DoubleRightLeftRotation(BinTree A) { A->Right = SingleLeftRotation(A->Right); return SingleRightRotation(A); } BinTree Insert(BinTree BST,ElementType X) { if(!BST) {//若原樹為空,生成並返回一個結點的二叉搜尋樹 BST = (BinTree)malloc(sizeof(struct TNode)); BST->Data = X; BST->Left = BST->Right =NULL; BST->times = 1; } else //開始查詢要插入的的元素的位置 { if( X < BST->Data ) { BST->Left = Insert(BST->Left,X);//遞迴插入左子樹 if(GetHeight(BST->Left) - GetHeight(BST->Right) == 2) { if( X < BST->Left->Data ) BST = SingleLeftRotation(BST); else BST = DoubleLeftRightRotation(BST); } } else if(X > BST->Data) { BST->Right = Insert(BST->Right,X);//遞迴插入右子樹 if(GetHeight(BST->Left) - GetHeight(BST->Right)== -2) { if(X > BST->Right->Data ) BST = SingleRightRotation(BST); else BST = DoubleRightLeftRotation(BST); } } //else X已經存在,什麼都不做 else { BST->times++; } } BST->Height = Max(GetHeight(BST->Left),GetHeight(BST->Right)) + 1; return BST; } BinTree MakeTree(int n){ BinTree bt = NULL; int bian[3]; for(int i=0;i<n;i++) { for(int j=0;j<3;j++) { scanf("%d",&bian[j]); } sort(bian,bian+3); // printf("%lld\n",((((long long int)bian[0] * 1000000+bian[1])*1000000)+bian[2])); bt = Insert(bt, ((((long long int)bian[0] * 1000000+bian[1])*1000000)+bian[2])); } return bt; } Position Find( BinTree BST, ElementType X ){ if(!BST||BST->Data==X)return BST; return Find((X>BST->Data?BST->Right:BST->Left),X); } void traveinorder(BinTree bt) { if(bt){ printf("%lld %d\n",bt->Data,bt->times); traveinorder(bt->Left); traveinorder(bt->Right); } } void Search(int m,BinTree bt){ int bian[3]; for(int i=0;i<m;i++) { for(int j=0;j<3;j++) { scanf("%d",&bian[j]); } sort(bian,bian+3); // printf("%lld\n",((((long long int)bian[0] * 1000000+bian[1])*1000000)+bian[2])); BinTree temp = Find(bt, ((((long long int)bian[0] * 1000000+bian[1])*1000000)+bian[2])); if(temp == NULL)printf("0\n"); else printf("%d\n",temp->times); } } int main() { int n,m; scanf("%d",&n); BinTree bt = NULL; bt = MakeTree(n); scanf("%d",&m); // traveinorder(bt); Search(m,bt); }

資料結構 散列表7.12 三角形遊戲

★實驗任務

給定n個三角形,用a,b,c表示三角形的三條邊(三角形可能有重複)。之後有m次詢問,每次詢問一個三角形在給定的n個三角形中出現的次數。

★資料輸入

第一行為n,之後n行,每行有a,b,c三個數字表示三角形的三條邊;接下來一行為m,之後有m行詢問,每行有a,b,c三個數字,表示要詢問的三角形的三邊。 資料保證a,b,c為正整數且可以構成一個三角形,且a,b,c不一定有序 對於40%的資料,n<=100,m<=100,a,b,c<=100 對於70%的資料,n<=1000,m<=1000,a,b,c<=1000 對於100%的資料,n<=5,000,m<=100,000,a,b,c<=999999

★資料輸出

對於每次詢問,輸出此三角形在之前給定的n個三角形中出現的次數

輸入示例
3
2 2 3
3 3 4
2 3 2
3
2 3 2
3 3 4
1 1 1

輸出示例
2
1
0