1. 程式人生 > >字典樹的題目 找了個時間刷了一點字典樹的題目

字典樹的題目 找了個時間刷了一點字典樹的題目

sdut 2828 字典樹模板題目 靜態的空間

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;

struct node
{
    int v;
    node *next[26];
    node()
    {
        v = 0;
        for(int i = 0;i < 26;i++){
            next[i] = NULL;
        }
    }
}T[1000000];
int top;
void creattrie(node *&root, char s[])
{
    node *p = root, *q;
    for(int i = 0;s[i];i++){
        int id = s[i]-'a';
        if(p->next[id] == NULL){
            q = &T[top++];
            p->next[id] = q;
        }
        p = p->next[id];
    }
    p->v++;
}
int findtree(node *root, char s[])
{
    node *p = root;
    for(int i = 0;s[i];i++){
        int id = s[i]-'a';
        if(p->next[id]==NULL){
            return 0;
        }
        p = p->next[id];
    }
    if(p->v){
        return 1;
    }else {
        return 0;
    }
}

int main()
{
    int n, m;
    char s[20];
    node *root;
    while(~scanf("%d %d", &n, &m)){
        memset(T,0,sizeof(T));
        top = 0;
        if(n==0&&m==0) break;
        root = &T[top++];
        for(int i = 0;i < n;i++){
            scanf("%s", s);
            creattrie(root,s);
        }
        while(m--){
            scanf("%s", s);
            int t = findtree(root,s);
            if(t){
                printf("Yes\n");
            }else {
                printf("No\n");
            }
        }
    }
    return 0;

}


hdu1251  用new過  不能申請靜態的空間 否則gg

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;

struct node
{
    int v;
    node *next[26];
    node(){
        v = 0;
        for(int i = 0;i < 26;i++){
            next[i] = NULL;
        }
    }
};
int top;
void creat(node *&root, char s[])
{
    node *p = root, *q;
    for(int i = 0;s[i];i++){
        int id = s[i]-'a';
        if(p->next[id]==NULL){
            q = new node;
            q->v++;
            p->next[id] = q;
        }else{
            p->next[id]->v++;
        }
        p = p->next[id];
    }
}
int findtree(node *root, char s[])
{
    node *p = root;
    for(int i = 0;s[i];i++){
        int id = s[i]-'a';
        if(p->next[id]==NULL){
            return 0;
        }
        p = p->next[id];
    }
    return p->v;
}
int main()
{
    node *root = new node;
    char s[15];
    while(gets(s)){
        if(!strcmp(s,"")) break;
        creat(root,s);
    }
    while(scanf("%s", s)!=EOF){
        printf("%d\n", findtree(root,s));
    }

    return 0;
}

sdut 3039 看好題目要求就行跟hdu1251差不多
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <algorithm>
using namespace std;

struct node
{
    int v;
    node *next[10];
    node()
    {
        v = 0;
        for(int i = 0;i < 10;i++){
            next[i] = NULL;
        }
    }
}T[100010];
int top;
void fan(char s[])
{
    char d[10];
    strcpy(d,s);
    int len = strlen(d);
    int i;
    for(i=0;d[i];i++){
        s[i] = d[len-1-i];
    }
    s[i] = '\0';
}
void creat(node *&root, char s[])
{
    node *p = root, *q;
    for(int i = 0;s[i];i++){
        int id = s[i]-'0';
        if(p->next[id]==NULL){
            q = &T[top++];
            q->v++;
            p->next[id] = q;
        }else{
             p->next[id]->v++;
        }
        p = p->next[id];
    }
    p->v--;
}
int findtree(node *root, char s[])
{
    node *p = root;
    for(int i = 0;s[i];i++){
        int id = s[i]-'0';
        if(p->next[id]==NULL){
            return 0;
        }
        p = p->next[id];
    }
    return p->v;
}
int main()
{
    int n;
    char s[10];
    while(~scanf("%d", &n)){
        memset(T,0,sizeof(T));
        top = 0;
        node *root = &T[top++];
        for(int i = 0;i < n;i++){
            scanf("%s", s);
            fan(s);
            creat(root,s);
        }
        int m;scanf("%d", &m);
        for(int i = 0;i < m;i++){
            scanf("%s", s);
            fan(s);
            printf("%d\n", findtree(root,s));
        }
    }
    return 0;
}


hdu 2072一開始的前導的空格沒有處理wa了

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;

struct node
{
    int v;
    node *next[26];
    node(){
        v =0;
        for(int i = 0;i < 26;i++){
            next[i] = NULL;
        }
    }
};
int sum;
void creat(node *&root, char s[])
{
    node *p =root, *q;
    for(int i = 0;s[i];i++){
        int id = s[i] - 'a';
        if(p->next[id]==NULL){
            q  = new node;
            p->next[id]=q;
        }
        p =  p->next[id];
    }
    if(p->v==0){
        sum++;

    }p->v++;
}
int main()
{
    char s[100000];
    char s1[1005];
    while(gets(s)&&s[0]!='#'){
        sum = 0;
        node *root = new node;
        int i = 0;
        int l = strlen(s);
        while(1){
            while(s[i++]==' ');i--;
            if(i==l) break;
            int top = -1;
            int j;
            for(j = i;s[j]!=' '&&s[j]!='\0';j++){
                s1[++top] = s[j];
            }
            s1[++top] = '\0';
            creat(root,s1);
            if(s[j]=='\0') break;
            else {
                while(s[j]==' '){
                    j++;
                }
            }
            i = j;
        }
        printf("%d\n", sum);
    }
    return 0;
}