前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【UVA】11732 – strcmp() Anyone?

【UVA】11732 – strcmp() Anyone?

作者头像
全栈程序员站长
发布2022-07-10 11:27:38
2620
发布2022-07-10 11:27:38
举报

大家好,又见面了,我是全栈君。

一開始不知道这样的一维建树方法。

每次一层用一个链指向下一层最左边的结点,之后每一层用一个链表串联全部的结点,这样就建树成功了。

14328524

11732

Accepted

C++

0.826

2014-10-09 13:13:14

代码语言:javascript
复制
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> pill;
const int nodemaxn = 4000 * 1000 + 100;
struct Trie{
    int head[nodemaxn]; //左儿子链
    int next[nodemaxn]; //右兄弟链
    int   ch[nodemaxn]; //这个结点代表的字符
    LL   tot[nodemaxn]; //有几个单词经过了这个结点
    int   sz;           //结点个数
    LL   ret;           //比較的次数
    void clear(){
        head[0] = 0; next[0] = 0; tot[0] = 0; sz = 1; ret = 0;
    }
    void insert(char *str){
        int L = strlen(str);
        int u = 0,v,found;
        tot[0]++;
        for(int i = 0; i <= L; i ++){
            found = 0;
            for(v = head[u]; v != 0 ; v = next[v]){  //到下一层去找
                if(ch[v] == str[i]){
                    found = 1;
                    break;
                }
            }
            if(!found){  //假设没有找到
                v = sz ++;
                tot[v] = 0;
                ch[v] = str[i];
                next[v] = head[u];
                head[u] = v;
                head[v] = 0;
            }
            u = v;
            tot[u] ++;
        }
    }
    void dfs(int u,int depth){
        if(head[u] == 0){
            ret += tot[u] * (tot[u] - 1) * depth;
        }
        else{
            LL sum = 0;
            for(int v = head[u] ; v != 0; v = next[v])
                sum += tot[v] * (tot[u] - tot[v]);
            ret += sum / 2 * (2 * depth + 1);
            for(int v = head[u] ; v != 0; v = next[v])
                dfs(v,depth + 1);
        }
    }
}trie;
const int maxn = 4000 + 10;
int main(){
    int n,Case = 1;
    while(scanf("%d",&n) && n){
        trie.clear();
        for(int i = 0; i < n; i++){
            char str[maxn];
            scanf("%s",str);
            trie.insert(str);
        }
        trie.dfs(0,0);
        printf("Case %d: %lld\n",Case++,trie.ret);
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115603.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年2月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档