zoukankan      html  css  js  c++  java
  • Hat’s Words hdu-1247

    就是查找这个单词能不能有两个单词组成,简单的字典树题目
    //////////////////////////////////////////////////////////////
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;

    #define maxn 26

    struct node
    {
        int sum;
        node *next[26];
    };

    node *root;
    char a[50005][30];

    void Insert(char *s);//建立字典树
    bool Find(char *s);
    bool Query(char *s);

    int main()
    {
        int i, n=0;

        root = new node();

        for(i=0; scanf("%s", a[i]) != EOF; i++)
            Insert(a[i]);
       
        for(n=1, i=0; i<=n; i++)
        {
            if(Query(a[i]))
                printf("%s ", a[i]);
        }

        return 0;
    }

    void Insert(char *s)
    {
        node *p = root;

        for(int i=0; s[i]; i++)
        {
            int k = s[i] - 'a';

            if(!p->next[k])
                p->next[k] = new node();
            p = p->next[k];
        }

        p->sum = 1;
    }
    bool Find(char *s)
    {
        node *p = root;

        for(int i=0; s[i]; i++)
        {
            int k = s[i] - 'a';

            if(!p->next[k])
                return false;
            p = p->next[k];
        }

        if(p->sum)return true;
        return false;
    }
    bool Query(char *s)
    {
        node *p = root;

        for(int i=0; s[i]; i++)
        {
            int k = s[i] - 'a';

            p = p->next[k];

            if(p->sum && Find(s+i+1))
                return true;
        }

        return false;

    } 

  • 相关阅读:
    YAML 语法
    mysqladmin
    CentOS7
    MySQL 基准测试
    Sqlite 约束条件 Constraints
    sqlite3数据库 sqlite3_get_table
    MFC中使用sqlite3操作数据库
    数据库函数:sqlite3_exec() SQL语句
    VS2013 生成sqlite3动态连接库及sqlite3.dll的调用
    Windows下VS2013创建与使用动态链接库(.dll)
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4674319.html
Copyright © 2011-2022 走看看