hihoCoder#1107 : Shortest Proper Prefix (前缀树)
题目大意:在n个单词中,如果以s作为前缀的单词个数不超过5个,那么称s为proper prefix。如果s为proper prefix并且s的任何一个前缀(不包括s)都不是proper prefix,那么称s为shortest proper prefix,找出词典中shortest proper prefix的个数。
题目分析:建立一棵trie,在trie上深搜即可。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long const int N=1005;
const int INF=1000000000;
const LL oo=0x7fffffffffffffff;
const double eps=1e-10; struct Node
{
int u,l;
Node(){}
Node(int _u,int _l):u(_u),l(_l){}
};
int cnt,n;
int size[N*N<<1];
int tr[N*N<<1][26]; void init()
{
cnt=1;
memset(tr,0,sizeof(tr));
memset(size,0,sizeof(size));
} void insert(string p)
{
int len=p.size();
int u=0;
for(int i=0;i<len;++i){
int c=p[i]-'a';
if(!tr[u][c]) tr[u][c]=cnt++;
u=tr[u][c];
++size[u];
}
} void dfs(int u,int &ans)
{
if(u>0&&size[u]<=5){
++ans;
}else{
for(int i=0;i<26;++i)
if(tr[u][i]) dfs(tr[u][i],ans);
}
} int solve()
{
int ans=0;
dfs(0,ans);
return ans;
} int main()
{
string p;
while(~scanf("%d",&n))
{
init();
while(n--)
{
cin>>p;
insert(p);
}
printf("%d\n",solve());
}
return 0;
}
hihoCoder#1107 : Shortest Proper Prefix (前缀树)的更多相关文章
- [HIHO1107]Shortest Proper Prefix(trie)
题目链接:http://hihocoder.com/problemset/problem/1107 题意:求有多少非依赖前缀,使得前缀下标识的字符串不超过5个. 这里非依赖的意思是,假如前缀a,b,a ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- 支持中文的基于词为基本粒度的前缀树(prefix trie)python实现
Trie树,也叫字典树.前缀树.可用于"predictive text"和"autocompletion".亦可用于统计词频(边插入Trie树边更新或加入词频) ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
随机推荐
- 火狐和IE的window.event对象详解(转载)
FF的FIREBUG,不仅能测试JS还能检查CSS错误,是一般常用的. 但它主要检查FF方面的错误,对IE就无能为力了. 要测试IE,就用ieTester,它可以测试IE几乎所有版本(1.0恐怕也用不 ...
- 解决uploadify多图片上传部分图片丢失,且不提示任何错误的问题
这两天用到uploadify的flash版本进行批量图片上传并生成缩略图的功能,之前用uploadify用的好好的,这次突然出现了一个奇怪的问题. 问题描述如下:当我选择单个图片上传的时候,图片上传都 ...
- app store 上架流程
前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...
- 记录一些容易忘记的属性 -- UIButton
//设置按钮文字字体(这个只在自定义button时有效) btn1.titleLabel.font = [UIFont systemFontOfSize:30]; showsTouchWhenH ...
- SharePoint 跨域还原网站一则
博客地址:http://blog.csdn.net/foxdave 源端:执行PowerShell命令备份网站集 Backup-SPSite http://server_name/sites/site ...
- sql server日志不能shrink或truncate
Backup log [dbxxx] with truncate_only sql server 2008之后不支持此操作,需要改为: BACKUP LOG dbxxx TO DISK='NUL:' ...
- 在MyEclipse上搭建web服务器
a) Servlet是什么? Servlet(服务器端小程序)是使用Java语言编写的服务器端程序[o1] ,可以使用脚本语言,生成动态的web页. b) Servlet的架构? ...
- 13年省赛-B题-连通分量
题意:求从1到N是否存在一条路,可以遍历每个节点. 思路:求任意两点之间是否通畅即可: 疑惑:完全暴力,bfs但是TLE,问题在于求连通分量(PS:不会)贴别人代码,先保存着. #include &l ...
- 解决Xcode7.1插件安装的办法
现象一. 运行安装后,没有出现在菜单上. 1. 到githup上下载Alcatraz project https://github.com/supermarin/Alcatraz2. 打开终端 3. ...
- 转:label标签的特殊用法
容易被忽略的label标签 原始作用 label标签是HTML原生的标签,其原始的作用参考这里 label 标签为 input 元素定义标注(标记). label 元素不会向用户呈现任何特殊效果.不过 ...