Shortest Proper Prefix
来源:互联网 发布:淘宝详情页编辑 编辑:程序博客网 时间:2024/06/08 16:09
Shortest Proper Prefix
题目
Query auto-completion(QAC) is widely used in many search applications. The basic idea is that when you type some string s in the search box several high-frequency queries which have s as a prefix are suggested. We say string s1 has string s2 as a prefix if and only if the first |s2| characters of s1 are the same as s2 where |s2| is the length of s2.
These days Little Hi has been working on a way to improve the QAC performance. He collected N high-frequency queries. We say a string s is a proper prefix if there are no more than 5 collected queries have s as a prefix. A string s is a shortest proper prefix if s is a proper prefix and all the prefixes of s(except for s itself) are not proper prefixes. Little Hi wants to know the number of shortest proper prefixes given N collected queries.
Hint: the 4 shortest proper prefixes for Sample Input are “ab”, “bb”, “bc” and “be”. Empty string “” is not counted as a proper prefix even if N <= 5.
输入
The first line contains N(N <= 10000), the number of collected queries.
The following N lines each contain a query.
Each query contains only lowercase letters ‘a’-‘z’.
The total length of all queries are no more than 2000000.
Input may contain identical queries. Count them separately when you calculate the number of queries that have some string as a prefix.
输出
Output the number of shortest proper prefixes.
样例输入
12aababcabcdeabcdeabcbabcdbcdebcbbdbcacbeebbb
样例输出
4
思路:
使用字典树的方法来实现。先构造带访问次数的字典树,然后在遍历查找有多少个访问次数小于5的前缀。
代码:
import java.util.*;class TreeNode{ int val = 0; char ch; TreeNode[] child; TreeNode(){ child = new TreeNode[26]; } public void insert(TreeNode root, String str){ TreeNode p = root; p.val++; char[] cstr = str.toCharArray(); for(int i = 0; i < str.length(); i++){ int idx = cstr[i] - 'a'; if(p.child[idx] == null) p.child[idx] = new TreeNode(); p = p.child[idx]; p.ch = cstr[i]; p.val++; } }}public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String ns = sc.nextLine(); int n = Integer.parseInt(ns); String[] sin = new String[n]; TreeNode root = new TreeNode(); for(int i = 0; i < n; i++){ sin[i] = sc.nextLine(); root.insert(root, sin[i]); } find(root); System.out.println(result); } sc.close(); } public static int result = 0; public static void find(TreeNode p){ if(p.val <= 5){ result++; return; } for(int i = 0; i < 26; i++){ if(p.child[i] != null && p.child[i].val > 5) find(p.child[i]); if(p.child[i] != null && p.child[i].val <= 5) result++; } }}
- #1107 : Shortest Proper Prefix
- Shortest Proper Prefix
- hihocoder #1107 : Shortest Proper Prefix
- hihocoder 1107 Shortest Proper Prefix
- Shortest Proper Prefix---Trie树
- hihocoder 1107 : Shortest Proper Prefix
- hihocoder 1107 : Shortest Proper Prefix
- 微软笔试题-Shortest Proper Prefix
- hihoCoder1107 Shortest Proper Prefix Tire树
- hihoCoder 1107 Shortest Proper Prefix (字典树的遍历)
- hihoCoder 1107 Shortest Proper Prefix 微软苏州校招笔试 1月17日
- poj 2001 shortest prefix
- POJ 2001 Shortest Prefix
- Shortest unique prefix
- Poj 2001 Shortest Prefix(字典树模板)
- proper fraction
- Proper Nutrition
- prefix
- 多态
- Android的性能模式:救援技巧
- STL空间配置器(一)
- 润乾报表使用记
- Selenium启动Chrome时,加载用户配置文件
- Shortest Proper Prefix
- Http响应码及其含义
- 如何使用 Google Web Fonts?
- JavaWeb——文件上传和下载
- 关于input标签的一些常识
- CCF 窗口
- 移动前端开发之viewport的深入理解
- maven加载oracle驱动问题
- 自定义滑杆遇到的两侧圆角的问题