Warm up 13 [C] Cellphone Typing
| Cellphone Typing |
| Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB |
| Total submit users: 20, Accepted users: 18 |
| Problem 12750 : Special judge |
| Problem description |
| 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:
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 (1 ≤ N ≤ 105). 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 |
| Problem Source |
| Latin American 2012 |
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define maxn 450005 12 #define ll long long 13 #define INF 0x7fffffff 14 char str[100001]; 15 int n, m; 16 double ans; 17 int ch[maxn][26]; 18 int val[maxn]; 19 int tsize[maxn]; 20 struct Trie{ 21 int sz; 22 Trie(){ sz = 1; memset(ch[0], 0, sizeof ch[0]); } 23 void insert(char *s, int v){ 24 int u = 0, n = strlen(s); 25 for (int i = 0; i<n; i++){ 26 int c = s[i] - 'a'; 27 if (!ch[u][c]){ 28 tsize[u]++; 29 memset(ch[sz], 0, sizeof ch[sz]); 30 val[sz] = 0; 31 ch[u][c] = sz++; 32 } 33 u = ch[u][c]; 34 } 35 val[u] += v; 36 } 37 }; 38 void dfs(int u, double s){ 39 for (int i = 0; i < 26; i++){ 40 if (ch[u][i]){ 41 int v = ch[u][i]; 42 if (tsize[v]>1 && val[v] == 0)s++; 43 if (val[v]){ 44 s += val[v]; ans += s; 45 } 46 dfs(v, s); 47 if(tsize[v]>1&&val[v]==0)s --; 48 if (val[v])s -= val[v]; 49 } 50 } 51 } 52 int main(){ 53 int cas = 1; 54 while (~scanf("%d", &n)){ 55 Trie tree; 56 ans = 0; 57 memset(tsize, 0, sizeof tsize); 58 for (int i = 0; i < n; i++){ 59 scanf("%s", str); 60 tree.insert(str, 1); 61 } 62 dfs(0, 0); 63 printf("%.2lf\n", ans / n); 64 } 65 return 0; 66 }
浙公网安备 33010602011771号