题目大意:在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 (前缀树)的更多相关文章

  1. [HIHO1107]Shortest Proper Prefix(trie)

    题目链接:http://hihocoder.com/problemset/problem/1107 题意:求有多少非依赖前缀,使得前缀下标识的字符串不超过5个. 这里非依赖的意思是,假如前缀a,b,a ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  4. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  5. 支持中文的基于词为基本粒度的前缀树(prefix trie)python实现

    Trie树,也叫字典树.前缀树.可用于"predictive text"和"autocompletion".亦可用于统计词频(边插入Trie树边更新或加入词频) ...

  6. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  7. Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

随机推荐

  1. 跨域SSO的实现

    翻译自CodeProject网站ASP.NET9月份最佳文章:Single Sign On (SSO) for cross-domain ASP.NET applications. 翻译不妥之处还望大 ...

  2. C++,1....n中随机等概率的输出m个不重复的数(假设n远大于m)。

    #include <stdlib.h> #include <time.h> knuth(int n, int m) { srand((unsigned )); ; i < ...

  3. IOS UTF8中文字母数字 组合时长度截取

    //计算总共字数和限制字数的Index位置 -(NSMutableArray *) unicodeLengthOfString: (NSString *) text { NSMutableArray ...

  4. poj2777 线段树

    //Accepted 4768 KB 391 ms //线段树,延时标记的应用 //对于每一段,用一个int表示被着色的情况,change标记该段的颜色是否发生整体的改变,即这一段 //用没用被全部涂 ...

  5. 收藏的博客--PHP

    32位Win7下安装与配置PHP环境(一至三)  http://blog.csdn.net/yousuosi/article/details/9448903

  6. C# 展开和折叠代码的快捷键(总结)

    C# 展开和折叠代码的快捷键 VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M +  ...

  7. SQL server 与Oracle开发比较

    ●概念上区别 1.Oracle 是一种对象关系数据库管理系统(ORDBMS),而Sql server 只是关系型数据库管 理系统(RDBMS). 2.Oracle使用Internet文件系统,该系统基 ...

  8. hdu 2050

    PS:真是醉了..之前觉得这题很难..然后在网上找到了大神给的深入解析,跪谢...这里贴大神博客的网址:http://blog.csdn.net/lishuhuakai/article/details ...

  9. ————————————————————————————杭电ACM————————————————X-POWER————————————————————————————————

    _________________________________________我要成大牛!!!___________________________________________________ ...

  10. BZOJ 1042 硬币购物

    先不考虑限制,那么有dp[i]表示i元钱的方案数. 然后考虑限制,发现可以容斥. 其实整个题就是两个容斥原理.感觉出的蛮好的. #include<iostream> #include< ...