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) ...
随机推荐
- 加载不同的nib文件
只需要实现nibName方法即可 另外还需在vc控制器初始化的时候不指定对应的nib文件名 -(NSString *)nibName { if(UI_USER_INTERFACE_IDIOM() == ...
- Spring MVC配置文件解释
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位
0×00 前言 GNSS是Global Navigation Satellite System的缩写.中文称作:全球卫星导航系统.全球导航卫星系统. GNSS泛指所有的卫星导航系统,包括全球的.区域的 ...
- 黑马程序员——【Java基础】——Java概述
---------- android培训.java培训.期待与您交流! ---------- 一.Java语言概述及三大技术架构 1.Java语言概述 Java是SUN公司于1995年推出的一种面向I ...
- Python 类的一些BIF
issubclass issubclass(cls, class_or_tuple, /) Return whether 'cls' is a derived from another class o ...
- ios openURL的使用(调用系统电话、浏览器、地图、邮件等)
Safari Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is s ...
- 6、C#基础整理(for 语句经典习题--for循环嵌套、穷举)
1.for循环嵌套----最基础题目:求阶乘的和 ; int n = int.Parse(Console.ReadLine()); ; i < n; i++) { ;//定义变量sum1,每次循 ...
- 解决div里插入img下边缝隙问题
<html> <head> <title> new document </title> <meta name="author ...
- Nunit 使用介绍
Nunit是.NET平台单元测试框架,其是从Junit发展而来,它强大之处是支持所有的.NET语言. Nunit的下载地址:http://www.nunit.org 介绍1: 布局: 左面:我们写的每 ...
- MVC 构造新Model实现内容搜索
当前在使用MVC开发一个网站,习惯了使用ASP.NET中控件,转到MVC之后突然对于页面和后台代码的传值感觉无从下手.花了点时间在网上看了写帖子后,想到了一个方法,重新构造一个新Model, 然后利用 ...