LA4122
哈夫曼树+搜索
抄了抄代码
先开始不知道怎么限制哈夫曼树,然后看了看代码,是用bfs序来限制。因为每个节点的右子树节点肯定不小于左儿子,同一层也是。所以先搞出bfs序,然后搜索,判断每一层右边是否大于左边。
哈夫曼树的每个节点必然会有零个或两个儿子,这也是判断无解或有解的情况。
#include<bits/stdc++.h>
using namespace std;
const int N = ;
string s;
int n, cnt, ans, root, now, kase;
int child[N][], val[N], pos[N];
vector<int> order;
void insert(int x)
{
for(int i = ; i < s.length(); ++i)
{
int t = s[i] - '';
if(!child[x][t])
child[x][t] = ++cnt;
x = child[x][t];
}
}
void dfs(int u)
{
if(pos[u] == order.size() - )
{
++ans;
return;
}
if(child[u][] == )
{
dfs(order[pos[u] + ]);
return;
}
for(int i = ; i * <= val[u]; ++i)
{
val[child[u][]] = i;
val[child[u][]] = val[u] - i;
if(val[order[pos[child[u][]] - ]] && val[order[pos[child[u][]] - ]] < val[child[u][]]) continue;
dfs(order[pos[u] + ]);
}
val[child[u][]] = val[child[u][]] = ;
}
bool build()
{
for(int i = ; i <= cnt; ++i) if(child[i][] * child[i][] == && child[i][] + child[i][])
return ;
queue<int> q;
order.clear();
q.push();
while(!q.empty())
{
int u = q.front();
q.pop();
pos[u] = order.size();
order.push_back(u);
if(child[u][]) q.push(child[u][]);
if(child[u][]) q.push(child[u][]);
}
return true;
}
int main()
{
while(cin >> n)
{
if(!n)
break;
memset(child, , sizeof(child));
ans = ;
cnt = ;
root = ++cnt;
for(int i = ; i <= n; ++i)
{
cin >> s;
insert(root);
}
printf("Case %d: ", ++kase);
if(!build())
{
puts("");
continue;
}
val[] = ;
dfs();
cout << ans << endl;
}
return ;
}
LA4122的更多相关文章
随机推荐
- JavaScript ES 数组系列
正文从这开始- ECMAScript 5.1 中提供的数组方法 其中部分方法,ECMAScript 3 就出现了,但是本文不再细分. ECMA-262/5.1 规范:https://www.ecma- ...
- Git学习总结三(工作区和暂存区、撤销修改)
工作区和暂存区 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库(Repository) 工作区有一个隐藏目录.git, ...
- mt_rand()和rand()两者的区别
在随机读取中使用了mt_rand(),而不适用rand(),他们两者的区别: mt_rand()是更好地随机数生成器,因为它跟rand()相比播下了一个更好地随机数种子:而且性能上比rand()快4倍 ...
- jquery 实现 单选框点击取消
<label for="1" class="z-label"> <input type="radio" class=&qu ...
- c#用控制台程序安装启动停止卸载服务
第一步:新建控制台项目 第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上 第四步 编写代码 双击打开 using System; using Syst ...
- Codeforces Round #468 Div. 2题解
A. Friends Meeting time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Vim 基本操作
Vim 基本操作 vim的模式 命令模式 2. 编辑模式 3. 尾行模式 编辑 i : 插入 光标所在位置 a : 插入 光标所在位置的下一个位置 o : 插入 光标所在位置的下一行插入新行 O : ...
- 第一节:web爬虫之requests
Requests库是用Python编写的,并且Requests是一个优雅而简单的Python HTTP库,在使用Requests库时更加方便,可以节约我们大量的工作,完全满足HTTP测试需求.
- Spring MVC 单元测试Demo
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={" ...
- SSL常用专业缩略语汇总
JKS - Java KeyStore JAVA密钥库 OCSP - Online Certificate Status Protocol证书在线状态协议. SAN - Subject Alterna ...