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
    评论
k-shortest c是一种图算法,用于寻找图中从一个起点到一个终点的k条最短路径。该算法可以应用于许多实际问题,例如网络路由,行程规划等。 k-shortest c算法的基本原理是在每一次迭代中,从起点到终点寻找一条最短路径,并将其添加到结果集中。然后,根据路径长度对图中的边进行更新,使得之后的迭代可以找到更短的路径。这个过程会重复k次,直到找到k条最短路径为止。 该算法可以通过以下步骤实现: 1. 设置起点和终点,并初始化结果集为空。 2. 在每次迭代中,使用Dijkstra或A*等最短路径算法找到一条最短路径。 3. 将找到的最短路径添加到结果集中。 4. 更新路径中的边的权值,使得之后的迭代可以找到更短的路径。 5. 重复步骤2-4,直到找到k条最短路径为止。 k-shortest c算法的优点是能够在不遍历整个图的情况下找到多条最短路径,从而减少了计算量。它可以提供多个解决方案供选择,使得问题的解决更加灵活。 然而,k-shortest c算法也存在一些问题。首先,随着k值的增加,算法的计算复杂度也会增加,因此在实际应用中需要权衡计算资源和最优解之间的平衡。其次,算法在处理大规模图形时可能会受到内存和计算能力的限制。 在总结中,k-shortest c是一种寻找k条最短路径的图算法,适用于各种实际问题。它通过迭代找到一条条最短路径,并更新路径的边权值,以获得更多的解决方案。尽管算法存在一些限制,但它仍然是解决某些问题的有效工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值