多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Facebook Hacker Cup 2015 Round 1 Autocomplete (附帶測試數據)

Facebook Hacker Cup 2015 Round 1 Autocomplete (附帶測試數據)

來源:程序員人生   發布時間:2015-01-30 08:35:04 閱讀次數:3174次



題目描寫:


Autocomplete25 points
                                          
  •                   

Since you crave state-of-the-art technology, you've just purchased a phone with a great new feature: autocomplete! Your phone's version of autocomplete has some pros and cons. On the one hand, it's very cautious. It only autocompletes a word when it knows exactly what you're trying to write. On the other hand, you have to teach it every word you want to use.

You have N distinct words that you'd like to send in a text message in order. Before sending each word, you add it to your phone's dictionary. Then, you write the smallest non-empty prefix of the word necessary for your phone to autocomplete the word. This prefix must either be the whole word, or a prefix which is not a prefix of any other word yet in the dictionary.

What's the minimum number of letters you must type to send all N words?

Input

Input begins with an integer T, the number of test cases. For each test case, there is first a line containing the integer N. Then,N lines follow, each containing a word to send in the order you wish to send them.

Output

For the ith test case, print a line containing "Case #i: " followed by the minimum number of characters you need to type in your text message.

Constraints

1 ≤ T ≤ 100 
1 ≤ N ≤ 100,000 

The N words will have a total length of no more than 1,000,000 characters. 
The words are made up of only lower-case alphabetic characters. 
The words are pairwise distinct. 

NOTE: The input file is about 10⑵0MB.

Explanation of Sample

In the first test case, you will write "h", "he", "l", "hil", "hill", for a total of 1 + 2 + 1 + 3 + 4 = 11 characters.

Example input ?        
Example output ?        
5 5 hi hello lol hills hill 5 a aa aaa aaaa aaaaa 5 aaaaa aaaa aaa aa a 6 to be or not two bee 3 having fun yet
Case #1: 11 Case #2: 15 Case #3: 11 Case #4: 9 Case #5: 3
                                





















解題思路:


使用字典樹可以求出該字符串是不是和其他字符串有公共前綴。具體看代碼



題目代碼:

#include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <cctype> #include <algorithm> #define eps 1e⑴0 #define pi acos(⑴.0) #define inf 107374182 #define inf64 1152921504606846976 #define lc l,m,tr<<1 #define rc m + 1,r,tr<<1|1 #define zero(a) fabs(a)<eps #define iabs(x) ((x) > 0 ? (x) : -(x)) #define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A)))) #define clearall(A, X) memset(A, X, sizeof(A)) #define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE)) #define memcopyall(A, X) memcpy(A , X ,sizeof(X)) #define max( x, y ) ( ((x) > (y)) ? (x) : (y) ) #define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; struct node { int p[27]; int cnt; bool alive; } treenode[1000005]; int ans,nodecnt; char s[1000005]; void insertto() { int len=strlen(s),p=0; ans++; if(treenode[p].p[s[0]-'a']==0) { clearall(treenode[nodecnt].p,0); treenode[nodecnt].cnt=0; treenode[nodecnt].alive=false; treenode[p].p[s[0]-'a']=nodecnt++; } p=treenode[p].p[s[0]-'a']; treenode[p].cnt++; for(int i=1; i<len; i++) { if(treenode[p].p[s[i]-'a']==0) { clearall(treenode[nodecnt].p,0); treenode[nodecnt].cnt=0; treenode[nodecnt].alive=false; treenode[p].p[s[i]-'a']=nodecnt++; } if(treenode[p].cnt==1&&treenode[treenode[p].p[s[i]-'a']].cnt==0) { } else ans++; treenode[treenode[p].p[s[i]-'a']].cnt++; p= treenode[p].p[s[i]-'a']; } } int main() { //freopen("data.in","r",stdin); //freopen("data.txt","w",stdout); int t,case1=1; while(scanf("%d",&t)!=EOF) { while(t--) { clearall(treenode[0].p,0); treenode[0].cnt=0; treenode[0].alive=false; nodecnt=1; ans=0; int n; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s",s); insertto(); //printf("%d ",ans); } printf("Case #%d: %d ",case1++,ans); } } return 0; }


題目終究測試數據:


鏈接: http://pan.baidu.com/s/1o6oiwY2 

密碼: 1dgp

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美 日韩 中文字幕 | v片在线观看 | 99精品高清视频一区二区 | 欧美操人视频 | 亚洲精品区一区二区三区四 | 视频久久精品 | 一区视频 | 午夜手机福利 | 欧美激情二区 | 欧美特级特黄a大片免费 | 成人在线视频国产 | 国产在线日韩在线 | www.国产福利 | 免费不卡毛片 | 五月婷婷亚洲 | 午夜视频在线免费观看 | 爱爱一级| 亚洲一区二区三 | 国产一区二区三区免费 | 免费国产成人α片 | 国产xxxxx在线播放 | 亚洲精品一区久久狠狠欧美 | 欧美性久久 | 大美香蕉伊在看欧美 | 武则天一级淫片免费 | 国产农村女人一级毛片了 | 国产乱码精品一区二区三上 | 亚洲国产精品综合久久一线 | 欧美日韩生活片 | 欧美亚洲国产精品久久蜜芽 | 成人精品一区久久久久 | 久久精品免观看国产成人 | 国产在线精品一区二区中文 | 国产一区二区三区国产精品 | 中文字幕在亚洲第一在线 | 一二三四在线观看视频 | 久久久久综合网久久 | 在线视频黄 | 韩国午夜理伦三级网 | 在线亚洲日产一区二区 | 中文字幕欧美亚洲 |