码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11732 "strcmp()" Anyone? (Trie)

时间:2015-07-14 11:42:09      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

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


题意:模拟strcmp()函数,给n个字符串,两两之间进行比较,问需要进行多少次比较?

解析:字符串很多,数据量大,按题意两两比较显然不现实。如果把所有的单词插入到一棵Trie里面会怎么样呢?考虑than和that对应的结点,than和that的前3个字符是一样的,但第4个字符不同,因此比较次数是7。不仅如此,任何两个在同一结点处分叉的字符串都需要比较7次。



AC代码:

#include <bits/stdc++.h>
using namespace std;

const int maxnode = 4000 * 1000 + 5;
const int sigma_size = 26;

struct Trie{    //由于字符集较大,故用左儿子-右兄弟表示法保存
    int head[maxnode];   //左儿子编号
    int next[maxnode];   //右兄弟编号
    char ch[maxnode];    //结点上的字符
    int tot[maxnode];    //以该结点为根的结点总数
    int sz;
    long long ans;

    void clear(){ sz = 1; tot[0] = head[0] = next[0] = 0; }  //初始只有一个根结点

    void insert(const char *s){
        int u = 0, v, n = strlen(s);
        tot[0] ++;
        for(int i=0; i<=n; i++){
            bool found = false;
            for(v=head[u]; v!=0; v=next[v]){
                if(ch[v] == s[i]){
                    found = true;
                    break;
                }
            }
            if(!found){   //没找到,新建节点
                v = sz++;
                tot[v] = 0;
                ch[v] = s[i];
                next[v] = head[u];
                head[u] = v;   //插到链表首部
                head[v] = 0;
            }
            u = v;
            tot[u] ++;
        }
    }

    // 统计LCP=u的所有单词两两的比较次数之和
    void dfs(int depth, int u) {
        if(head[u] == 0) // 叶结点
          ans += tot[u] * (tot[u] - 1) * depth;
        else {
          int sum = 0;
          for(int v = head[u]; v != 0; v = next[v])
            sum += tot[v] * (tot[u] - tot[v]); // 子树v中选一个串,其他子树中再选一个
          ans += sum / 2 * (2 * depth + 1);    // 每种选法统计了两次,故除2
          for(int v = head[u]; v != 0; v = next[v])
            dfs(depth+1, v);
        }
    }

    // 统计
    long long count() {
        ans = 0;
        dfs(0, 0);
        return ans;
    }

};

Trie T;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int n, t = 0;
    char s[1002];
    while(scanf("%d", &n) != EOF && n){
        T.clear();
        for(int i=0; i<n; i++){
            scanf("%s", s);
            T.insert(s);
        }
        printf("Case %d: %lld\n", ++t, T.count());
    }
    return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA 11732 "strcmp()" Anyone? (Trie)

标签:

原文地址:http://blog.csdn.net/u013446688/article/details/46873539

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!