萌新求解
查看原帖
萌新求解
285328
Linnyx楼主2022/2/25 18:48

if (p->children[sum] == nullptr) 一直报错 找不到错

#include <bits/stdc++.h>

using namespace std;
int T, len, ans, cnt;
string s;
int re(char c)
{
    if (c >= '0' && c <= '9')
        return int(c - '0') ;
    else if (c >= 'A' && c <= 'Z')
        return int(c - 'A' + 10 + 1);
    else if (c >= 'a' && c <= 'z')
        return int(c - 'a' + 10 + 26 + 2);
    return 0;
}
class TrieNode
{
public:
    TrieNode *children[100];
    bool isWord;
    TrieNode()
    {
        memset(children, 0, sizeof(children));
        isWord = false;
    }
};
TrieNode *root;
void insert(string word)
{
    TrieNode *p = root;
    for (char w : word)
    {
        int sum=re(w);
        if (p->children[sum] == nullptr)
            p->children[sum] = new TrieNode;
        p = p->children[sum];
    }
    if(!p->isWord)ans++;
    p->isWord = true;
}
int main()
{
    scanf("%lld", &T);
    for (int i = 1; i <= T; i++)
    {
        cin>>s;
        insert(s);
    }
    printf("%d", ans);
    system("pause");
    return 0;
}
2022/2/25 18:48
加载中...