UVa 12506 Shortest Names

31 篇文章 0 订阅
2 篇文章 0 订阅

题目:uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3950

Description

In a strange village, people have very long names. For example: aaaaa, bbb and abababab. You see, it’s very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call ‘aaaaa’, you can call ‘aaa’, because no other names start with ‘aaa’. However, you can’t call ‘a’, because two people’s names start with ‘a’. The people in the village are smart enough to always call the shortest possible prefix. It is guaranteed that no name is a prefix of another name (as a result, no two names can be equal). If someone in the village wants to call every person (including himself/herself) in the village exactly once, how many characters will he/she use?

Input

The first line contains T (T ≤ 10), the number of test cases. Each test case begins with a line of one integer n (1 ≤ n ≤ 1000), the number of people in the village. Each of the following n lines contains a string consisting of lowercase letters, representing the name of a person. The sum of lengths of all the names in a test case does not exceed 1,000,000.

Output

For each test case, print the total number of characters needed.

Sample Input

1
3
aaaaa
bbb
abababab

Sample Output

5

题意

每个单词取尽量短的前缀,但只要有若干个单词有相同前缀,它们就要同时再向后取一个字母,直到两两之间没有公共前缀

用字典树记公共前缀,数答案时,每一条链都一直数到有一个结点记录的树值为1为止,因为大于1就说明还有若干单词有公共前缀

遍历的过程ans要一直加上结点记录的数

Source Code

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int n;
    struct node *ch[26];
}leaf,*trie;

void init(trie tree)
{
    int i;
    tree->n=0;
    for(i=0;i<26;i++)
      tree->ch[i]=NULL;
}

trie creat()
{
    trie p=(trie)malloc(sizeof(leaf));
    init(p);
    return p;
}

void insert(trie tree,char *s)
{
    while(*s)
    {
        if(tree->ch[*s-'a']==NULL)
            tree->ch[*s-'a']=creat();
        tree->ch[*s-'a']->n++;
        tree=tree->ch[*s-'a'];
        s++;
    }
}

void count(trie tree,int *ans)
{
    if(!tree) return;
    int i;
    for(i=0;i<26;i++)
        if(tree->ch[i])
        {
            *ans+=tree->ch[i]->n;
            if(tree->ch[i]->n>1)
                count(tree->ch[i],ans);
        }
}

void cut(trie tree)
{
    int i;
    for(i=0;i<26;i++)
      if(tree->ch[i])
        cut(tree->ch[i]);
    free(tree);
}

char name[2002];
int main()
{
    int iTom;
    scanf("%d",&iTom);
    while(iTom--)
    {
        int n,ans=0;
        trie dict=creat();
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",name);
            insert(dict,name);
        }
        count(dict,&ans);
        printf("%d\n",ans);
        cut(dict);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值