POJ 2001 Phone

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>

using namespace std;

const int M = 26;
int n;
#define maxn 1005

char S[maxn][21];

struct Node
{
    int v;
    Node *next[M];
}*root;

void Creat(char *s) //分配内存最好使用calloc 因为会进行初始化 而malloc不初始化 都是随机的数据垃圾
{
    int len = strlen(s);
    Node *p = root, *q;

    for(int i=0; i<len; i++)
    {
        int id = s[i] - 'a';
        //cout<<id<<" ";
        if(p->next[id] == NULL)
        {
            q = (Node *)calloc(1, sizeof(Node));
            q->v = 1;
            p->next[id] = q;
            p = q;
        }
        else
        {
            p = p->next[id];
            p->v++;
        }
    }
}

void Find(char *s)
{
    int len  = strlen(s);
    Node *p = root;
    for(int i=0; i<len; i++)
    {
        int id = s[i] - 'a';
        p = p->next[id];
        printf("%c", s[i]);
        if(p->v == 1)
            break;
    }
    cout<<endl;
}

int main()
{
    n = 0;
    root = (Node *)calloc(1, sizeof(Node));  // 根节点
    //root = new Node;
    while(~scanf("%s", S[n]))
    {
        Creat(S[n++]);
    }

    for(int i=0; i<n; i++)
    {
        printf("%s ", S[i]);
        Find(S[i]);
    }
    return 0;
}

时间: 2024-08-07 16:46:40

POJ 2001 Phone的相关文章

POJ 2001 Shortest Prefixes (Trie)

题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon"

POJ 2001 字典树(入门题)

#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<stack> #include<cmath> #include<queue> #include<map> using namespace std; struct Node { int c; int ne

Poj 2001 (Trie 前缀树)

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #define MAXN 400010 #defi

poj 2001 trie

第一道trie 还需要写题来建立自己的代码习惯. 1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 #define maxn 20010 5 using namespace std; 6 7 struct node { 8 char v; 9 int sz; 10 bool isword, mark; 11 node *son[26], *pre; 12 }pool[maxn], *tail

POJ 2001

#include<iostream> using namespace std; const int kind=26; struct trienode { trienode * next[kind]; int branch; trienode() { branch=0; for(int i=0;i<kind;i++) next[i]=NULL; } }; class trie { trienode * root; public: trie() { root=NULL; } void ins

acm学习

要学习的   基本结构 高级结构 题单 集合结构   幷查集 POJ 1182 POJ 1308 POJ 1611 POJ 1986 POJ 1988 线性结构 数组 栈 队列 双端队列 POJ POJ POJ POJ POJ 树状结构 二叉树 BST AVL树 splay树(伸展树) Treap Cartesian Tree Size Balance Tree POJ 3580(splay tree) POJ 2761(Treap) POJ 2201(Cartesian Tree) POJ 3

字典树trie的学习与练习题

博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/blog/1166118 http://www.360doc.com/content/12/1116/15/9615799_248213540.shtml http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html http://bl

【数据结构】字典树/Trie树/前缀树 - 字符串的统计、排序和保存

字典树 描述 字典树,又称单词查找树.Trie树.前缀树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计.排序和保存大量的字符串(但不仅限于字符串). 常见操作有插入和查找,删除操作少见. 性质 根节点不包含字符 除根节点外每一个节点都只包含一个字符 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串 每个节点的所有子节点包含的字符都不相同 优点 利用字符串的公共前缀来减少查询时间 最大限度地减少无谓的字符串比较 查询效率比哈希树高 自带字典序排序 直接判断重复,或者记

POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误

------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况复制代码时,一定要小心,注意修改变量名,一不留神就会带来不可估量的后果,一定要仔细挨着一个一个变量的修改,别跳着看着哪个变量就改哪一个变量! (这个题目中,就是复制了一下,代码,ca,我找了一下午的错....还好终于找到了,一个字母的错,) -----------------------------