zucc-project3 huffman codes 兩種解答方式詳解(build tree and imitate tree)(advanced data structure)

first:

#include<bits/stdc++.h>
using namespace std;
const int	N = 100;
int		n;
char		ch[N];
map<char, int>	Time;

struct node
{
	char	c[N];
	int	len;
} code[N];

typedef struct tree
{
	int		sum;
	struct tree	* left;
	struct tree	* right;
	char		c;
}*Tree;

struct cmp//
{
	bool operator()( const Tree x, const Tree y )
	{
		return(x->sum > y->sum);
	}
};

int BuildHuffman()
{
	int					sum = 0; /* 記錄花費 */
	priority_queue<Tree, vector<Tree>, cmp> que;

	for ( int i = 1; i <= n; i++ )
	{
		Tree treenode = (Tree) malloc( sizeof(struct tree) );
		treenode->c	= ch[i];
		treenode->left	= NULL;
		treenode->right = NULL;
		treenode->sum	= Time[ch[i]];
		que.push( treenode );
	}
	while ( que.size() > 1 )
	{
		Tree lchild = que.top();
		que.pop();
		Tree rchild = que.top();
		que.pop();
		Tree father = (Tree) malloc( sizeof(struct tree) );
		father->left	= lchild;
		father->right	= rchild;
		father->sum	= lchild->sum + rchild->sum;
		que.push( father );
	}

	Tree head = que.top();
	typedef pair<Tree, int> P;
	queue<P> q;
	q.push( P( head, 0 ) );
	while ( !q.empty() ) /* 樹的層序遍歷 */
	{
		P p = q.front();
		q.pop();
		if ( p.first->left )
			q.push( P( p.first->left, p.second + 1 ) );
		if ( p.first->right )
			q.push( P( p.first->right, p.second + 1 ) );
		if ( !p.first->left && !p.first->right )
			sum += Time[p.first->c] * p.second;
	}

	return(sum);
}


int cmp1( const struct node x, const struct node y )
{
	return(x.len < y.len);
}


bool Only()
{
	map<string, int> mp;
	sort( code + 1, code + 1 + n, cmp1 );
	string s;
	for ( int i = 1; i <= n; i++ )
	{
		s.clear();
		for ( int j = 0; j < code[i].len; j++ )
		{
			s += code[i].c[j];
			if ( mp[s] )
				return(0);
		}
		mp[s] = 1;
	}
	return(1);
}


int main()
{
	char c;
	c=getchar();
	while(c!='0'){
	n=c-'0';
	for ( int i = 1; i <= n; i++ )
	{
		scanf( " %c", &ch[i] );         /* ch保存字符,便於遍歷所有字符 */
		scanf( "%d", &Time[ch[i]] );    /* 用MAP容器記錄字符使用次數(方便調用) */
	}

	int Minmum = BuildHuffman();            /* 記錄最優花費 */

	int m;
	cin>>m;
	while ( m-- )
	{
		int	flag = 0;
		char	c;
		int	SumCost = 0;
		for ( int i = 1; i <= n; i++ )
		{
			cin>>c>>code[i].c;
			code[i].len	= strlen( code[i].c );
			SumCost		+= Time[c] * code[i].len; /* 算花費 */
		}

		if ( SumCost > Minmum )
			cout<<"No"<<endl;
		else if ( !Only() )
			cout<<"No"<<endl;
		else
			cout<<"Yes"<<endl;
	}
	getchar();
	c=getchar();
	}

	return(0);
}



second

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
    string s;
    int count;
};
node p[80];
map<char,int> ha;
priority_queue<int,vector<int>,greater<int> > q;//從小到大排
bool check(node *p,int n){
    int i,j;
    for(i=0;i<n;i++){
        string temp=p[i].s.substr(0,p[i].s.length());
        for(j=0;j<n;j++){
            if(i==j){
                continue;
            }
            if(temp==p[j].s.substr(0,p[i].s.length())){//前綴檢查
                break;
            }
        }
        if(j<n){//不滿足要求
            return false;
        }
    }
    return true;
}
int main(){
    //freopen("D:\\INPUT.txt","r",stdin);
    int n,i;
    scanf("%d",&n);
    char c;
    int wpl=0;
    for(i=0;i<n;i++){
        cin>>c;
        scanf("%d",&ha[c]);
        q.push(ha[c]);
    }
    int cur,next;
    while(!q.empty()){
        cur=q.top();
        q.pop();
        if(q.empty()){//最後一次不用做加法
            break;
        }
        cur+=q.top();
        q.pop();
        wpl+=cur;

        //cout<<cur<<endl;

        q.push(cur);
    }
    //cout<<wpl<<endl;
    int num;
    scanf("%d",&num);
    while(num--){
        int sum=0;
        for(i=0;i<n;i++){
            cin>>c;
            p[i].count=ha[c];
            cin>>p[i].s;
            sum+=p[i].count*p[i].s.length();
        }

//        cout<<sum<<endl;

        if(sum==wpl&&check(p,n)){
            printf("Yes\n");
        }
        else{
            printf("No\n");
        }
    }
    return 0;
}

參考鏈接:

https://www.cnblogs.com/Deribs4/p/4801656.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章