Facebook Hacker Cup 2015 Round 1 Autocomplete (附带测试数据)




题目描述:


Autocomplete 25 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-20MB.

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-10
#define pi acos(-1.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\n",ans);
            }
            printf("Case #%d: %d\n",case1++,ans);
        }
    }
    return 0;
}


题目最终测试数据:


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

密码: 1dgp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Wireshark是一种开源的网络数据包分析工具,用于捕获和分析网络中的数据流量。它可以帮助用户检测和解决网络问题,同时还可以用于网络安全方面的调试和攻击分析。据引用所述,Wireshark可以用来查看和分析hack.pcapng数据包文件。 对于hacker1.pcapng数据包文件的查看和分析,可以使用Wireshark打开该文件。通过对数据包的分析,可以了解到网络中传输的各种信息,例如网络通信协议、源IP地址、目标IP地址、数据内容等。 根据引用和引用的描述,如果在hacker1.pcapng数据包文件中存在黑客获取到的系统的账号内容或者黑客爆破数据库名的sql语句,可以根据相关的信息提取出账号的用户名、密码或数据表的完整名字作为FLAG进行提交。 因此,使用Wireshark查看并分析hacker1.pcapng数据包文件,可以帮助我们获取有关黑客攻击和数据泄露的相关信息,并进一步做出相应的安全措施。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [wirehark数据分析与取证hack.pcapng](https://blog.csdn.net/Aluxian_/article/details/127046619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值