UVa11732 - strcmp() Anyone?

本文详细介绍了如何使用C++实现Trie数据结构,并通过案例展示了其应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Trie

#include <bits/stdc++.h>
#define sigma_size 100
#define maxnode 4000010
typedef long long ll;
using namespace std;
ll ans;
struct Trie {
    struct Node {
        int val;//记录结点字母
        int cnt;//记录节点字母出现次数
        int fir, nxt;
        void init() {
            cnt = 0;
            fir = nxt = val = -1;
        }
    };
    Node node[maxnode];
    int used, root;
    int P(char ch) {
        if ('0' <= ch && ch <= '9') return ch - '0';
        if ('a' <= ch && ch <= 'z') return 10 + ch - 'a';
        if ('A' <= ch && ch <= 'Z') return 36 + ch - 'A';
        return 62;
    }
    int newNode() {
        node[used].init();
        return used ++;
    }
    void init() {
        used = 0;
        root = newNode();
    }
    int Find(int x, int p) {
        for (int i = node[x].fir; ~i; i = node[i].nxt) {
            if (node[i].val == p) return i;
        }
        return -1;
    }
    void Add(char *s) {
        int x = root;
        for (int i = 0; ; ++ i) {
            int p = P(s[i]);
            int ix = Find(x, p);
            if (ix == -1) {
                ix = newNode();
                node[ix].val = p;
                node[ix].nxt = node[x].fir;
                node[x].fir = ix;
            }
            ans += node[x].cnt - node[ix].cnt;
            ans += node[ix].cnt << 1;
            ++node[x].cnt;
            x = ix;
            if (s[i] == '\0') break;
        }
        ++ node[x].cnt;
    }
}tx;
char tmp[1010];
int main() {
    #ifdef LOCAL
        freopen("data.in", "r", stdin);
    #endif // LOCAL
    int n, now = 0;
    while (scanf("%d", &n) != EOF) {
        tx.init();
        if (n == 0) break;
        ans = 0;
        for (int i = 0; i < n; ++ i) {
            scanf("%s", tmp);
            tx.Add(tmp);
        }
        printf("Case %d: ", ++ now);
        cout << ans << endl;
    }
}


是的,C++中确实有`strcmp`函数。`strcmp`是一个字符串比较函数,用于比较两个字符串是否相等。它是在C++标准库中的`<cstring>`(在C++11及以后的版本中,这个库通常被重命名为`<string>`)中定义的。 函数的原型通常如下: ```cpp int strcmp(const char *str1, const char *str2); ``` 这个函数会返回一个整数,如果`str1`和`str2`完全相同,它会返回0。如果`str1`在字典序上小于`str2`,它会返回一个负数。如果`str1`在字典序上大于`str2`,它会返回一个正数。 使用这个函数时,需要注意以下几点: * 字符串必须以空字符('\0')结尾,否则可能会产生未定义的行为。 * 字符串必须以空字符结尾的相同方式进行比较。否则可能会得到错误的结果。 * 两个字符串的指针参数不能是NULL,否则会抛出运行时错误。 使用这个函数的一个例子可能是这样的: ```cpp #include <cstring> #include <iostream> int main() { std::string str1 = "Hello"; std::string str2 = "Hello"; int result = strcmp(str1.c_str(), str2.c_str()); if (result == 0) { std::cout << "Strings are equal." << std::endl; } else if (result < 0) { std::cout << "String1 is less than String2." << std::endl; } else { std::cout << "String1 is greater than String2." << std::endl; } return 0; } ``` 这个程序将打印出 "Strings are equal.",因为两个字符串是相同的。请注意,为了安全起见,最好始终使用字符串的`.c_str()`方法来获取C风格的字符串,以便在需要使用`strcmp`或类似函数的情况下使用它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值