first:
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
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