zoukankan      html  css  js  c++  java
  • poj2001

    Trie树,每个结点记录其下面分支的单词总数。

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    using namespace std;

    struct Node
    {
    Node
    *next[26];
    int count;
    } trie[
    100000];

    int ncount;

    void ins(Node *proot, char *st)
    {
    proot
    ->count++;
    if (st[0] == '\0')
    return;
    if (!proot->next[st[0] - 'a'])
    {
    ncount
    ++;
    proot
    ->next[st[0] - 'a'] = trie + ncount;
    }
    ins(proot
    ->next[st[0] - 'a'], st + 1);
    }

    void query(Node *proot, char *st)
    {
    if (st[0] == '\0')
    return;
    printf(
    "%c", st[0]);
    if (proot->next[st[0] - 'a']->count > 1)
    query(proot
    ->next[st[0] - 'a'], st + 1);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    ncount = 0;
    char st[1005][30];
    memset(trie,
    0, sizeof(trie));
    int i = 0;
    while (scanf("%s", st[i]) != EOF && strlen(st[i]) != 0)
    {
    ins(trie, st[i]);
    i
    ++;
    }
    int n = i;
    for (i = 0; i < n; i++)
    {
    printf(
    "%s ", st[i]);
    query(trie, st[i]);
    printf(
    "\n");
    }
    return 0;
    }
  • 相关阅读:
    118.Java_前定义
    117.数据结构概述(定义,基本术语,层次)
    116.C语言_文件
    115.C语言_函数
    java数组
    sql语句学习(第二季
    linux查看内存
    增强型for和Iterator学习
    ArrayList和LinkedList
    java并发回答
  • 原文地址:https://www.cnblogs.com/rainydays/p/2073237.html
Copyright © 2011-2022 走看看