Cellphone Typing 字典树
Cellphone Typing
64-bit integer IO format: %lld Java class name: Main
None
Graph Theory
2-SAT
Articulation/Bridge/Biconnected Component
Cycles/Topological Sorting/Strongly Connected Component
Shortest Path
Bellman Ford
Dijkstra/Floyd Warshall
Euler Trail/Circuit
Heavy-Light Decomposition
Minimum Spanning Tree
Stable Marriage Problem
Trees
Directed Minimum Spanning Tree
Flow/Matching
Graph Matching
Bipartite Matching
Hopcroft–Karp Bipartite Matching
Weighted Bipartite Matching/Hungarian Algorithm
Flow
Max Flow/Min Cut
Min Cost Max Flow
DFS-like
Backtracking with Pruning/Branch and Bound
Basic Recursion
IDA* Search
Parsing/Grammar
Breadth First Search/Depth First Search
Advanced Search Techniques
Binary Search/Bisection
Ternary Search
Geometry
Basic Geometry
Computational Geometry
Convex Hull
Pick's Theorem
Game Theory
Green Hackenbush/Colon Principle/Fusion Principle
Nim
Sprague-Grundy Number
Matrix
Gaussian Elimination
Matrix Exponentiation
Data Structures
Basic Data Structures
Binary Indexed Tree
Binary Search Tree
Hashing
Orthogonal Range Search
Range Minimum Query/Lowest Common Ancestor
Segment Tree/Interval Tree
Trie Tree
Sorting
Disjoint Set
String
Aho Corasick
Knuth-Morris-Pratt
Suffix Array/Suffix Tree
Math
Basic Math
Big Integer Arithmetic
Number Theory
Chinese Remainder Theorem
Extended Euclid
Inclusion/Exclusion
Modular Arithmetic
Combinatorics
Group Theory/Burnside's lemma
Counting
Probability/Expected Value
Others
Tricky
Hardest
Unusual
Brute Force
Implementation
Constructive Algorithms
Two Pointer
Bitmask
Beginner
Discrete Logarithm/Shank's Baby-step Giant-step Algorithm
Greedy
Divide and Conquer
Dynamic Programming
Tag it!
A research team is developing a new technology to save time when typing text messages in mobile devices. They are working on a new model that has a complete keyboard, so users can type any single letter by pressing the corresponding key. In this way, a user needs P keystrokes to type a word of length P.
However, this is not fast enough. The team is going to put together a dictionary of the common words that a user may type. The goal is to reduce the average number of keystrokes needed to type words that are in the dictionary. During the typing of a word, whenever the following letter is uniquely determined, the cellphone system will input it automatically, without the need for a keystroke. To be more precise, the behavior of the cellphone system will be determined by the following rules:
- The system never guesses the first letter of a word, so the first letter always has to be input manually by pressing the corresponding key.
- If a non-empty succession of letters c1c2...cn has been input, and there is a letter c such that every word in the dictionary which starts with c1c2...cn also starts with c1c2...cnc, then the system inputs c automatically, without the need of a keystroke. Otherwise, the system waits for the user.
For instance, if the dictionary is composed of the words `hello', `hell', `heaven' and `goodbye', and the user presses `h', the system will input `e' automatically, because every word which starts with `h' also starts with `he'. However, since there are words that start with `hel' and with `hea', the system now needs to wait for the user. If the user then presses `l', obtaining the partial word `hel', the system will input a second `l' automatically. When it has `hell' as input, the system cannot guess, because it is possible that the word is over, or it is also possible that the user may want to press `o' to get `hello'. In this fashion, to type the word `hello' the user needs three keystrokes, `hell' requires two, and `heaven' also requires two, because when the current input is `hea' the system can automatically input the remainder of the word by repeatedly applying the second rule. Similarly, the word `goodbye' needs just one keystroke, because after pressing the initial `g' the system will automatically fill in the entire word. In this example, the average number of keystrokes needed to type a word in the dictionary is then (3 + 2 + 2 + 1)/4 = 2.00.
Your task is, given a dictionary, to calculate the average number of keystrokes needed to type a word in the dictionary with the new cellphone system.
Input
Each test case is described using several lines. The first line contains an integer N representing the number of words in the dictionary ( 1N105). Each of the next N lines contains a non-empty string of at most 80 lowercase letters from the English alphabet, representing a word in the dictionary. Within each test case all words are different, and the sum of the lengths of all words is at most 106.
Output
For each test case output a line with a rational number representing the average number of keystrokes needed to type a word in the dictionary. The result must be output as a rational number with exactly two digits after the decimal point, rounded if necessary.
Sample Input
4
hello
hell
heaven
goodbye
3
hi
he
h
7
structure
structures
ride
riders
stress
solstice
ridiculous
Sample Output
2.00
1.67
2.71
题意:
先给你一些字符串,也就是所谓的字典;
然后先求查询每个单词最少要输入几个单词;
如
4
hello
hell
heaven
goodbye
如果输入g,那只有唯一一个g开头的,所以只要1个就可以。
输入h,由于h开头的都有e,所以e不需要输入,再输入l,相同理由此l不需要输入,再输入o,才能找到hello
解法:
对于每一个单词,至少要找到他,那就要找到他与其他单词不同的地方。hello hell 不同于最后的o。
可以再trie结构体里面多加几个条件,way表示接下去的种数,sum表示该单词使用次数,flag表示是否有单词;
如果该点的way>1,表示有多种路可以走。那此时ans+=p->sum,因为每个单词都要输入下面那个单词,如果现在这个位置有完整的一个字符串了,那ans--,因为他不用再输入了;如果此时way==1,并且这里有完整的单词时,那就要ans+=p->sum-1。(要注意这里的sum表示的意思)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 26
struct trie
{
trie *next[maxn];
int sum;
int way;//记录分支的数量
int flag;//标记
};
char str[];
trie *root;
void init()
{
root=(trie*)malloc(sizeof(trie));
for(int i=;i<maxn;i++)
root->next[i]=NULL;
root->sum=;
root->flag=;
root->way=;
}
void insert(char s[])
{
trie *p=root,*q;
int i,j,len=strlen(s);
for(i=;i<len;i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL)
{
p->way++;
q=(trie*)malloc(sizeof(trie));
for(j=;j<maxn;j++)
q->next[j]=NULL;
q->sum=;
q->flag=;
q->way=;
p->next[id]=q;
}
p=p->next[id];
p->sum++;
if(i==len-)
p->flag=;
}
}
int getans(trie *p)
{
int i,j,ans=;
for(i=;i<;i++)
{
if(p->next[i]!=NULL)
{
ans+=getans(p->next[i]);
}
}
if(p->way>)
{
if(p->flag>)
{
ans--;
}
ans+=p->sum;
}
else if(p->flag>)
{
ans+=p->sum-;//如果为串尾;
}
free(p);
return ans;
}
int main()
{
int i,j,t,m;
while(scanf("%d",&t)!=EOF)
{
init();
m=t;
while(t--)
{
scanf("%s",str);
insert(str);
}
int ans=getans(root)+m;
printf("%.2lf\n",ans*1.0/m);
}
}
Cellphone Typing 字典树的更多相关文章
- BNU 27847——Cellphone Typing——————【字典树】
Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...
- POJ 1451 - T9 - [字典树]
题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...
- HDU1298 字典树+dfs
T9 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- Tire树(字典树)
from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- js计算系统当前日期是星期几的几种方法
方法一: // 计算系统当前是星期几 var str = "今天是星期" + "日一二三四五六".charat(new date().getday()); 方法 ...
- HDU 3081 最大流+并查集
题意:有n个男生和n个女生,玩结婚游戏,由女生选择男生:女生可以选择不会和她吵架的男生以及不会和她闺蜜吵架的男生,闺蜜的闺蜜也是闺蜜.问你最多可以进行多少轮,每一轮每个女生只能选择一个之前她没选过的男 ...
- 安全框架 - Shiro与springMVC整合的注解以及JSP标签
Shiro想必大家都知道了,之前的文章我也有提过,是目前使用率要比spring security都要多的一个权限框架,本身spring自己都在用shiro,之前的文章有兴趣可以去扒一下 最近正好用到s ...
- JavaScript测试工具
大家都知道Javascript的测试比较麻烦,一般是开发使用一些浏览器的插件比如IE develop bar或是firebug来调试,而测试往往需要通过页面展示后的js错误提示来定位.那么还有其他比较 ...
- 【mybatis】1、入门CURD基本操作(环境搭建)
#1.基本环境 环境 版本 jdk 1.7.0_10 ide eclipse-jee-luna-SR2-win32-x86_64 maven 3.3.3 mybatis 3.2.7 mysql 5.1 ...
- HASHKILL
6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区,下划 ...
- mysql-advanced-5.6.23-linux-glibc2.5-x86_64安装
0,二进制安装: mysql-advanced-5.6.23-linux-glibc2.5-x86_64.zip 1,软件包 mysql-advanced-5.6.23-linux-glibc2. ...
- C语言 链表的创建--打印--逆置--新增--删除--排序--释放
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...
- C# log4net 不输出日志
一个新项目,直接用了一些之前的代码,突然跟踪不到日志了.检查发现了原因,特在此记录. log4net的配置文件log4net_config.xml <?xml version="1.0 ...
- NET中MSMQ的使用----附例子
目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子 一. ...