UVA 11732 strcmp() Anyone? (Trie)

7 篇文章 0 订阅
2 篇文章 0 订阅

strcmp() Anyone?

题意:输入n个字符串,两两调用一次strcmp(),问字符比较的总次数是多少?
考虑strcmp()的实现如下:
int strcmp (char *s, char * t) {
	int i;
	for (i = 0; s[i] == t[i]; i++)
		if (s[i] == '\0')
			return 0;
	return s[i] - t[i];
}

思路:首先单词两两比较一定有n*(n-1)/2。然后建立Trie树,每插入一个单词,如果当前字母在树上还没有节点存在,则不比较,如果已经存在,则比较次数为该结点当前的访问次数*2。要注意如果两个单词相同,还需要最后比较一次。这里用普通的Trie树是不行的,必须将Trie树用前向星的方式存储。

代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug puts("===============")
typedef long long ll;
using namespace std;
const int maxnode = 4001000;
struct Trie {
    int head[maxnode], next[maxnode], tot[maxnode], ed[maxnode];
    char ch[maxnode];
    int sz;
    ll sum;
    void init() {
        sz = 1;
        sum = 0;
        head[0] = next[0] = tot[0] = 0;
    }
    void insert(char *s) {
        int u = 0, v, n = strlen(s);
        for (int i = 0; i < n; i++) {
            bool ok = 0;
            for (v = head[u]; v; v = next[v]) {
                if (ch[v] == s[i]) {
                    ok = 1;
                    break;
                }
            }
            if (!ok) {
                v = sz++;
                tot[v] = ed[v] = 0;
                ch[v] = s[i];
                next[v] = head[u];
                head[u] = v;
                head[v] = 0;
            }
            u = v;
            sum += tot[v] * 2;
            tot[v]++;
        }
        sum += ed[u];
        ed[u]++;
    }
}T;
int main () {
    int cnt = 1, n;
    char str[1100];
    while(~scanf("%d", &n), n) {
        T.init();
        rep(i, n) {
            scanf("%s", str);
            T.insert(str);
        }
        printf("Case %d: %lld\n", cnt++, T.sum + (ll)n * (n - 1) / 2);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值