zoukankan      html  css  js  c++  java
  • hdu-1247

    思路:

    字典树模板题


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define MAX 500007
    using namespace std;
    
    struct node
    {
        int e;
        struct node* next[26];
    };
    node* root;
    char str[MAX][50];
    
    void Insert(char* s)
    {
        node* ptr = root;
        for(;*s != '';s++)
        {
            int n = *s-'a';
            if(ptr->next[n] == NULL)
                ptr->next[n] = new node();
            ptr = ptr->next[n];
        }
        ptr->e = 1;
    }
    
    bool find2(char* s)
    {
        node* ptr = root;
        for(;*s != '';s++)
        {
            int n = *s-'a';
            ptr = ptr->next[n];
            if(ptr != NULL) {
                if(ptr->e==1 && *(s+1)=='')
                    return true;
            }
            else
                return false; 
        }
        return false;
    }
    
    bool find1(char* s)
    {
        node* ptr = root;
        for(;*s != '';s++)
        {
            int n = *s-'a';
            ptr = ptr->next[n];
            if(ptr->e==1 && find2(s+1))
                return true;
        }
        return false;
    }
    
    int main()
    {
        int i = 0;
        root = new node();
        while(scanf("%s",str[i]) != EOF) {
            Insert(str[i]);
            i++;
        }
        for(int j = 0;j < i;j++)
            if(find1(str[j]))
                cout<<str[j]<<endl;
        return 0;
    }
  • 相关阅读:
    16解释器模式Interpreter
    15适配器模式Adapter
    14桥接模式Bridge
    13组合模式Composite
    12外观模式Facade
    11代理模式Proxy
    10享元模式Flyweight
    09观察者模式ObServer
    08策略模式Strategy
    07装饰模式Decorator
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/5196406.html
Copyright © 2011-2022 走看看