• hdu1053 Entropy && hdu2527 Safe Or Unsafe


    裸裸的哈弗曼编码,求出哈弗曼编码的路径长度,注意整个字符串为一种字符的情况

    View Code
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<vector>
    using namespace std;
    struct node
    {
    int u,w;
    node(int a=0,int b=0):u(a),w(b){}
    bool friend operator <(const node a,const node b)//重载操作符
    {
    return a.w>b.w;
    }
    };
    int f[150],n,num,d[150],root,ans;
    char str[5000];
    priority_queue<node> Q;
    vector<int> g[500];//用向量保存每一个节点的儿子
    void BTree()//建哈弗曼树
    {
    node p1,p2;
    while(!Q.empty())
    {
    p1=Q.top();Q.pop();
    p2=Q.top();Q.pop();
    g[num].push_back(p1.u);
    g[num].push_back(p2.u);
    if(Q.size()==0)
    break;
    Q.push(node(num,p1.w+p2.w));
    num++;
    }
    }
    void DFS(int v,int deep)
    {
    if(g[v].size()==0)//叶子节点的深度即为编码长度
    {
    ans+=f[v]*deep;
    return ;
    }
    for(int i=0;i<2;i++)
    DFS(g[v][i],deep+1);
    }
    int main()
    {
    while(scanf("%s",str)==1)
    {
    if(strcmp(str,"END")==0)
    break;
    while(!Q.empty())
    Q.pop();
    memset(d,0,sizeof(d));
    for(int i=0;i<strlen(str);i++)
    d[(int)str[i]]++;
    n=0;
    for(int i=0;i<128;i++)
    if(d[i]!=0)
    {
    f[n]=d[i];
    Q.push(node(n++,d[i]));
    }
    if(Q.size()==1)
    {
    printf("%d %d 8.0\n",strlen(str)*8,f[0]);
    continue;
    }
    num=n;ans=0;
    BTree();
    DFS(num,0);
    int tt=strlen(str)*8;
    printf("%d %d %.1f\n",tt,ans,tt*1.0/ans);
    for(int i=0;i<=num;i++)
    g[i].clear();
    }
    return 0;
    }

     hdu2527

    Safe Or Unsafe

    基本一样的题

    View Code
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<vector>
    using namespace std;
    struct node
    {
    int u,w;
    node(int a=0,int b=0):u(a),w(b){}
    bool friend operator <(const node a,const node b)//重载操作符
    {
    return a.w>b.w;
    }
    };
    int f[150],n,num,d[150],root,ans;
    char str[5000];
    priority_queue<node> Q;
    vector<int> g[500];//用向量保存每一个节点的儿子
    void BTree()//建哈弗曼树
    {
    node p1,p2;
    while(!Q.empty())
    {
    p1=Q.top();Q.pop();
    p2=Q.top();Q.pop();
    g[num].push_back(p1.u);
    g[num].push_back(p2.u);
    if(Q.size()==0)
    break;
    Q.push(node(num,p1.w+p2.w));
    num++;
    }
    }
    void DFS(int v,int deep)
    {
    if(g[v].size()==0)//叶子节点的深度即为编码长度
    {
    ans+=f[v]*deep;
    return ;
    }
    for(int i=0;i<2;i++)
    DFS(g[v][i],deep+1);
    }
    int main()
    {
    int T,cmp;
    scanf("%d",&T);
    while(T--)
    {
    scanf("%d",&cmp);
    scanf("%s",str);
    while(!Q.empty())
    Q.pop();
    memset(d,0,sizeof(d));
    for(int i=0;i<strlen(str);i++)
    d[(int)str[i]]++;
    n=0;
    for(int i=0;i<128;i++)
    if(d[i]!=0)
    {
    f[n]=d[i];
    Q.push(node(n++,d[i]));
    }
    num=n;ans=0;
    if(n==1)
    {
    if(strlen(str)<=cmp)
    printf("yes\n");
    else printf("no\n");
    continue;
    }
    BTree();
    DFS(num,0);
    if(ans<=cmp)
    printf("yes\n");
    else printf("no\n");
    for(int i=0;i<=num;i++)
    g[i].clear();
    }
    return 0;
    }

     

  • 相关阅读:
    ironic port bind
    pdb /usr/bin/neutron-server
    networking_generic_switch
    [CodeForces586D]Phillip and Trains
    [CodeForces598D]Igor In the Museum
    [poj3468]A Simple Problem with Integers
    [bzoj1503][NOI2004]郁闷的出纳员
    [bzoj1208][HNOI2004]宠物收养所
    [luogu3384][模板]树链剖分
    [CodeForces869A]The Artful Expedient
  • 原文地址:https://www.cnblogs.com/nanke/p/2353383.html
Copyright © 2020-2023  润新知