#include<iostream>
#include<string>

using namespace std;

//表示next数组的长度,表示26个字母。如果字符串中有其他字符的话,应相应调整。
//如果所有的字符串都是手机号的话,那就是10了
const int MAX_NUM = 26;
struct trieNode{
    int i;//按需使用,本例子中表示从头到本字符组成的前缀出现的次数
    struct trieNode *next[MAX_NUM];
};

trieNode* CreateTrieNode(){
    trieNode *p = new trieNode;
    p->i = 1;
    for(int i = 0; i < MAX_NUM; i++)
        p->next[i] = NULL;
    return p;
}
void DelTrieTree(trieNode *p)
{
    for(int i=0;i < MAX_NUM; i++)
    {
        if(p->next[i] != NULL)
            DelTrieTree(p->next[i]);
    }
    delete p;
}
void InsertTrieTree(trieNode **root, string str){
    trieNode *p;
    if(*root == NULL){
        p = CreateTrieNode();
        *root = p;
    }
    else
        p = *root;
    int len = str.length();
    int k;
    for(int i = 0; i < len; i++){
        k = str.at(i) - 'a';
        if(p->next[k] != NULL)
            p->next[k]->i += 1;
        else
            p->next[k] = CreateTrieNode();
        p = p->next[k];
    }
}

int SearchTrieTree(trieNode **root, string str){
    trieNode *p;
    if(*root == NULL)
        return 0;
    p = *root;
    int k, len = str.length();
    for(int i = 0; i < len; i++){
        k = str.at(i) - 'a';
        if(p->next[k] == NULL)
            return 0;
        p = p->next[k];
    }
    return p->i;
}

int main(){
    trieNode *root = NULL;
    InsertTrieTree(&root, "checking");
    InsertTrieTree(&root, "check");
    InsertTrieTree(&root, "for");
    InsertTrieTree(&root, "checking");
    InsertTrieTree(&root, "program");
    InsertTrieTree(&root, "programmer");
    InsertTrieTree(&root, "cheprogrammer");
    cout << SearchTrieTree(&root, "che") << endl;//输出3
    cout << SearchTrieTree(&root, "prog") << endl;//输出2
}

 

hdu2846该题字典树,在插入是把字符串分解成0到1到len的字符串后在插入。该题编译器需要选择C++,如果选择G++,内存过不了。笔者就是入了编译器的坑,浪费了半天时间在优化内存,结果发现C++编译器后直接就AC了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N (26)

struct Node
{
    int i;
    int k;
    Node* next[N];
    Node(int ik)
    {
        i = 1;
        k = ik;
        for(int j = 0;j<N;++j)
            next[j] = NULL;
    }

};
Node* root = NULL;
void DelNode(Node* p)
{
    if(p==NULL) return;
    for(int i=0;i<N;++i)    DelNode(p->next[i]);
    delete p;
    p = NULL;
}
void InsertNode(const char str[],int index)
{
    Node* p = root;
    for(int i=0;i<strlen(str);++i)
    {
        int k = str[i] - 'a';
        if(p->next[k])
        {
            if(p->next[k]->k != index)
            {
                ++p->next[k]->i;
                p->next[k]->k = index;
            }
        }
        else
        {
            p->next[k] = new Node(index);
        }
        p = p->next[k];
    }
}

int SearchNode(const char str[])
{
    Node *p = root;
    for(int i=0;i<strlen(str);++i)
    {
        int k = str[i] - 'a';
        if(p->next[k])  p = p->next[k];
        else    return 0;
    }
    return p->i;
}

int main()
{
    int p,q;
    while(~scanf("%d",&p))
    {
        root = new Node(0);
        for(int i=0;i<p;++i)
        {
            char cstr[21];
            scanf("%s",cstr);
            int len = strlen(cstr);
            for(int j=0;j<len;++j)
            {
                InsertNode(cstr+j,i);
            }
        }
        int q;
        scanf("%d",&q);
        char cstr2[21];
        for(int i=0;i<q;++i)
        {
            scanf("%s",cstr2);
            printf("%d\n",SearchNode(cstr2));
        }
        DelNode(root);

    }
    return 0;
}
View Code

 hdu1251,该题编译器需要选择C++,不然内存有挂了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define N (26)
struct Node
{
    int i;
    Node* next[N];
    Node()
    {
        i = 1;
        for(int i=0;i<N;++i)
            next[i] = NULL;
    }
};
Node* root = new Node();
void DelNode(Node* p)
{
    if(p==NULL) return;
    for(int i=0;i<N;++i)
        DelNode(p->next[i]);
    delete p;
}
void InsertNode(char* str)
{
    Node *p = root;
    for(int i=0;i<strlen(str);++i)
    {
        int k = str[i]-'a';
        if(p->next[k]) ++p->next[k]->i;
        else    p->next[k] = new Node();
        p = p->next[k];
    }
}

int SearchNode(char* str)
{
    Node *p = root;
    for(int i=0;i<strlen(str);++i)
    {
        int k = str[i]-'a';
        if(p->next[k]) p = p->next[k];
        else return 0;
    }
    return p->i;
}

int main()
{
    char str1[11],str2[11];

    while(gets(str1))
    {
        if(str1[0]==0) break;
        InsertNode(str1);
    }

    while(gets(str2))
    {
        printf("%d\n",SearchNode(str2));
    }

    return 0;
}
View Code

 

hdu1247,该题需要从单纯len=1到len-1遍历,并且需要判断是否是单词

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string.h>
using namespace std;
#define N (26)


struct Node
{
    int i;
    Node* next[N];
    Node()
    {
        i = 1;
        for(int j=0;j<N;++j)
            next[j] = NULL;
    }
    void SetNext(const char c)
    {
        int k = c - 'a';
        if(next[k])     ++next[k]->i;
        else    next[k] = new Node();
    }
    Node* GetNext(const char c)
    {
        return next[c-'a'];
    }
};

Node *root = NULL;

void InsertNode(char* str)
{
    Node *p = root;
    int len = strlen(str);
    for(int i=0;i<len;++i)
    {
        p->SetNext(str[i]);
        p = p->GetNext(str[i]);
    }
}

int SearchNode(char* str)
{
    Node *p = root;
    int len = strlen(str);
    for(int i=0;i<len;++i)
    {
        p = p->GetNext(str[i]);
        if(p==NULL) return 0;
    }
    //处理是否是单词
    int ti = 0;
    for(int i=0;i<N;++i)
        if(p->next[i])
        {
            ti += p->next[i]->i;
            if(ti == p->i) return 0;
        }

    return p->i;
}

void DelNode(Node *p)
{
    if(p == NULL) return;
    for(int i=0;i<N;++i)
        DelNode(p->next[i]);
    delete p;
}
int main()
{
    vector<string> vs;
    root = new Node();
    char str[50];
    while(~scanf("%s",str))
    {
        //if(strcmp(str,"ok")==0) break;
        vs.push_back(str);
        InsertNode(str);
    }
    int nSize = vs.size();
    for(int i=0;i<nSize;++i)
    {
        strcpy(str,vs[i].c_str());
        int len = strlen(str);
        for(int l1=1;l1<len-1;++l1)
        {
            int l2 = len-l1;
            char tc1[50]={'\0'},tc2[50]={'\0'};
            strncpy(tc1,str,l1);
            strncpy(tc2,str+l1,l2);
            if(SearchNode(tc1)&&SearchNode(tc2))
            {
                printf("%s\n",str);
                break;
            }
        }
    }
    vs.clear();
    DelNode(root);
    return 0;
}
/*
a
ahat
hat
hatword
hatshat
hziee
word
ok
*/
View Code