zoukankan      html  css  js  c++  java
  • 动态字典树_拆分查找(HDU_1247)

    #include <stdio.h>
    #include <string.h>
    
    #define M 26
    #define WordSize 32
    #define WordCount 50000
    
    struct node{
        int end;
        node *child[M];
        node()
        {
            end = 0;
            memset(child,NULL,sizeof(child));
        }
    };
    
    char map[WordCount][WordSize];
    
    void insert(node *root, char *str)
    {
        node *p = root;
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            int k = str[i] - 'a';
            if(p->child[k] == NULL)
                p->child[k] = new node;
            p = p->child[k];
        }
        p->end = 1;
    }
    
    int find(node *root, char *str)
    {
        node *p = root;
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            int k = str[i] - 'a';
            if(p->child[k] == NULL)
                return 0;
            p = p->child[k];
        }
        return p->end;
    }
    
    int cutFind(node *root, char *str)
    {
        int len = strlen(str);
        for(int i=1; i<len; i++)
        {
            if(find(root,str + i))    ;
            else    continue;
            char ch = str[i];
            str[i] = '';
            if(find(root,str))
            {
                str[i] = ch;
                return 1;
            }
            str[i] = ch;
        }
        return 0;
    }
    
    
    int main(int argc, char* argv[])
    {
        #ifdef __MYLOCAL
        freopen("in.txt","r",stdin);
        #endif
    
        node *root = new node;
        int n = 0;
        while(scanf("%s",map[n]) != EOF)
        {
            insert(root,map[n]);
            n++;
        }
        for(int i=0; i<n; i++)
        {
            cutFind(root,map[i]) == 1 ? printf("%s
    ",map[i]) : NULL;
        }
    
        return 0;
    }
  • 相关阅读:
    python常用运维脚本实例
    数据库优化方案整理
    Python第一章概述与环境安装
    九阴真经
    常用的kubectl命令
    第7篇管理存储资源
    第8篇NFS PersistentVolume
    第10篇用 ConfigMap 管理配置
    第11篇Kubernetes部署微服务电商平台
    第12篇Kubernetes 监控
  • 原文地址:https://www.cnblogs.com/lk1993/p/3207562.html
Copyright © 2011-2022 走看看