1. 程式人生 > >POJ 1451 -- T9

POJ 1451 -- T9

找到 delete size alt output 文章 ++ () bsp

T9
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4131 Accepted: 1481

Sample Input

2
5
hell 3
hello 4
idea 8
next 8
super 3
2
435561
43321
7
another 5
contest 6
follow 3
give 13
integer 6
new 14
program 4
5
77647261
6391
4681
26684371
77771

Sample Output

Scenario #1:
i
id
hel
hell
hello

i
id
ide
idea


Scenario #2:
p
pr
pro
prog
progr
progra
program

n
ne
new

g
in
int

c
co
con
cont
anoth
anothe
another

p
pr
MANUALLY
MANUALLY

題意:

現實生活問題:用手機打字

先給出n個單詞表示常用單詞

然後用戶按手機鍵盤上面的數字鍵.要求用戶每按一個數字鍵,手機彈出可能性最大的單詞

思路:

字典樹的應用,使用字典樹對單詞進行統計

使用深度搜索遍歷字典樹,進行詞義猜測

Tips:

※ 字典樹的典型應用是用於統計,排序和保存大量的字符串(但不僅限於字符串),所以經常被搜索引擎系統用於文本詞頻統計。

※ 【網上摘錄】

串的快速檢索:

  給出N個單詞組成的熟詞表,以及一篇全用小寫英文書寫的文章,請你按最早出現的順序寫出所有不在熟詞表中的生詞。

  在這道題中,我們可以用數組枚舉,用哈希,用字典樹,先把熟詞建一棵樹,然後讀入文章進行比較,這種方法效率是比較高的。

串的排序:

  給定N個互不相同的僅由一個單詞構成的英文名,讓你將他們按字典序從小到大輸出

  用字典樹進行排序,采用數組的方式創建字典樹,這棵樹的每個結點的所有兒子很顯然地按照其字母大小排序。對這棵樹進行先序遍歷即可。

最長公共前綴問題:

  對所有串建立字典樹,對於兩個串的最長公共前綴的長度即他們所在的結點的公共祖先個數,於是,問題就轉化為最近公共祖先問題。

  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 using namespace
std; 5 const int maxl = 110; 6 char word[10][5] = {{"\0"}, 7 {"\0"},{"abc"},{"def"}, 8 {"ghi"},{"jkl"},{"mno"}, 9 {"pqrs"},{"tuv"},{"wxyz"}}; 10 11 char In[maxl];///接受輸入字符串,建立字典樹 12 char inNum[maxl];///接受輸入的數字按鍵 13 char ans[maxl];///輸出數組 14 char tmp[maxl];///輸出數組的臨時數組,比較概率大小 15 int pro; 16 17 class HashTable{ 18 public: 19 HashTable *next[26]; 20 int Count;//概率 21 HashTable()//初始化 22 { 23 Count = 0; 24 memset(next,0,sizeof(next)); 25 } 26 }*root;//根節點 27 28 29 void Insert(int num) 30 { 31 int i = 0; 32 HashTable *temp = root; 33 while(In[i]) 34 { 35 if(!temp->next[In[i]-a])///為空 36 { 37 temp->next[In[i]-a] = new HashTable; 38 } 39 temp = temp->next[In[i]-a]; 40 temp->Count += num; 41 i++; 42 } 43 } 44 45 void find(int End,int pos,HashTable *r) 46 { 47 int len = strlen(word[inNum[pos]-0]); 48 HashTable *temp = r; 49 for(int i=0;i<len;i++) 50 { 51 int pi = word[inNum[pos]-0][i] - a; 52 if(!temp->next[pi]) continue; 53 tmp[pos] = word[inNum[pos]-0][i]; 54 if(pos == End)//檢索結束 55 { 56 if(temp->next[pi]->Count > pro) 57 { 58 strcpy(ans,tmp); 59 pro = temp->next[pi]->Count; 60 } 61 } 62 else 63 find(End,pos+1,temp->next[pi]); 64 } 65 } 66 67 int main() 68 { 69 int turn; 70 int turnCount = 1; 71 while(cin>>turn) 72 { 73 while(turn--) 74 { 75 root = new HashTable; 76 ///輸入字符串和頻數 77 int a; 78 cin>>a; 79 for(int i=1;i<=a;i++) 80 { 81 int num; 82 cin>>In>>num; 83 ///將讀入的字符插入字典 84 Insert(num); 85 } 86 87 cout<<"Scenario #"<<turnCount++<<":"<<endl; 88 89 ///輸入按鍵 90 cin>>a; 91 while(a--) 92 { 93 cin>>inNum; 94 ///將讀入的按鍵進行處理 95 int len = strlen(inNum); 96 for(int i=0;i<len-1;i++)///註意末尾是1 97 { 98 pro = 0; 99 memset(tmp,0,sizeof(tmp)); 100 find(i,0,root); 101 if(pro == 0)//沒有找到 102 cout<<"MANUALLY"<<endl; 103 else{ 104 cout<<ans<<endl; 105 } 106 } 107 cout<<endl; 108 } 109 cout<<endl; 110 delete root; 111 } 112 } 113 return 0; 114 }

技術分享圖片

POJ 1451 -- T9