Cellphone Typing 字典树

Cellphone Typing

Time Limit: 5000ms

Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 12526
64-bit integer IO format: %lld      Java class name: Main

Prev Submit Status Statistics Discuss Next

Font Size: + -

Type:  
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!

[PDF Link]

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:

  1. 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.
  2. 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[1000010];
trie *root;
void init()
{
    root=(trie*)malloc(sizeof(trie));
    for(int i=0;i<maxn;i++)
        root->next[i]=NULL;
    root->sum=0;
    root->flag=0;
    root->way=0;
}
void insert(char s[])
{
    trie *p=root,*q;
    int i,j,len=strlen(s);
    for(i=0;i<len;i++)
    {
        int id=s[i]-‘a‘;
        if(p->next[id]==NULL)
        {
            p->way++;
            q=(trie*)malloc(sizeof(trie));
            for(j=0;j<maxn;j++)
                q->next[j]=NULL;
            q->sum=0;
            q->flag=0;
            q->way=0;
            p->next[id]=q;
        }
        p=p->next[id];
        p->sum++;
        if(i==len-1)
            p->flag=1;
    }
}
int getans(trie *p)
{
    int i,j,ans=0;
    for(i=0;i<26;i++)
    {
        if(p->next[i]!=NULL)
        {
            ans+=getans(p->next[i]);
        }
    }
    if(p->way>1)
    {
        if(p->flag>0)
        {
            ans--;
        }
        ans+=p->sum;
    }
    else if(p->flag>0)
    {
        ans+=p->sum-1;//如果为串尾;
    }
    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);
    }
}
时间: 2024-12-21 13:35:05

Cellphone Typing 字典树的相关文章

BNU 27847——Cellphone Typing——————【字典树】

Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1252664-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT A

HDU 1298 字典树

T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1720    Accepted Submission(s): 678 Problem Description A while ago it was quite cumbersome to create a message for the Short Message Service (

hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)

Problem Description A while ago it was quite cumbersome to create a message for the Short Message Service (SMS) on a mobile phone. This was because you only have nine keys and the alphabet has more than nine letters, so most characters could only be

HDU 1298 T9【字典树】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3917    Accepted Submission(s): 1415 Problem Description A while ago it was quite cumbersome

Tire树(字典树)

from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交叉. 定义 在计算机科学中,trie,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有

hdu 1251 统计难题(字典树)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

51nod round3# 序列分解(折半枚举+字典树)

小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n的整数序列.小刀试着把这个序列分解成两个长度为n/2的子序列. 这两个子序列必须满足以下两个条件: 1.他们不能相互重叠. 2.他们要完全一样. 如果小刀可以分解成功,大刀会给小刀一些糖果. 然而这个问题对于小刀来说太难了.他想请你来帮忙. Input 第一行给出一个T,表示T组数据.(1<=T<=5) 接下来每一组数据,输入共2行. 第一行包含一个整数n (2<=n<=40且为偶数). 第二行给出n

白话算法与数据结构之【字典树】

1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高.      Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子

[算法系列之二十]字典树(Trie)

一 概述 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 二 优点 利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希表高. 三 性质 (1)根节点不包含字符,除根节点外每一个节点都只包含一个字符: (2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串: (3)每个节点的所有子节点包含的字符都不相同. 单词列表为"apps&