(HDU)2527 Safe Or&nbsp…

来源:互联网 发布:mac能用硕鼠吗 编辑:程序博客网 时间:2024/04/27 14:50
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2527
//ps:算法跟hdu 1053类似
#include
#include
#include
#include
using namespace std;
struct node
{
    intkey;
    int f;
    intlson,rson;
    booloperator < (const nodex)const        
                                                
       returnx.key<key;
    }
};
node father;
node g[3100];
int sum=0;
void fuc(node x,int cnt);
int main()
{
    intn,temp;
   cin>>n;
   while(n--)
    {
       charstr[1100];
      cin>>temp;
      scanf("%s",str);
       sum=0;
      priority_queue p;
       intlen=strlen(str);
       intii=0;
       for(inti=0;i
       {
          intcnt=1;
          charc;
         if(str[i]!='#')
          {
            c=str[i];
            str[i]='#';
             for(intj=i+1;j
             {
               if(str[j]==c)
                {
                   cnt++;
                  str[j]='#';
                }
             }
            g[ii].f=ii;
            g[ii].lson=g[ii].rson=-1;
            g[ii].key=cnt;
            p.push(g[ii]);
             ii++;
          }
       }
      
      if(ii==1)
       {
         if(len<=temp)
            cout<<"yes"<<endl;
          else
            cout<<"no"<<endl;
       }
       else
       {
         while(p.size()!=1)
          {
             nodetmp1,tmp2;
            tmp1=p.top();
            p.pop();
            tmp2=p.top();
            p.pop();
            g[ii].f=ii;
            g[ii].lson=tmp1.f;
            g[ii].rson=tmp2.f;
            tmp1.f=ii;
            tmp2.f=ii;
            g[ii].key=tmp1.key+tmp2.key;
            p.push(g[ii]);
             ii++;
          }
         father=p.top();
         p.pop();
         
         fuc(father,0);
         if(sum<=temp)
            cout<<"yes"<<endl;
          else
            cout<<"no"<<endl;
       }
    }
    return0;
}

void fuc(node x,int cnt)
{
   if(x.lson==-1&&x.rson==-1)
    {
      sum+=x.key*cnt;
       return;
    }
   fuc(g[x.lson],cnt+1);
   fuc(g[x.rson],cnt+1);
}
原创粉丝点击