zoukankan      html  css  js  c++  java
  • Shortest Prefixes 字典树

       virtual judge  树 A题

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 struct node
     5 {
     6     int flag;
     7     struct node *next[26];
     8 };
     9 struct node *creat()
    10 {
    11     struct node *p;
    12     p=(struct node *)malloc(sizeof(struct node));
    13     for(int i=0;i<26;i++)
    14         p->next[i]=NULL;
    15     p->flag=1;
    16     return p;
    17 }
    18 void insert(struct node *p,char s[])
    19 {
    20     int i,t;
    21     for(i=0;s[i]!='\0';i++)
    22     {
    23         t=s[i]-'a';
    24         if(p->next[t]==NULL)
    25             p->next[t]=creat();
    26         else
    27             p->next[t]->flag++;
    28         p=p->next[t];
    29     }
    30 }
    31 void  shortt(struct node *p,char s[])
    32 {
    33     int i,t;
    34     char ss[100];
    35     for(i=0;s[i]!='\0';i++)
    36     {
    37         ss[i]=s[i];
    38         t=s[i]-'a';
    39         if(s[i+1]=='\0'||p->next[t]->flag==1)
    40         {
    41             ss[i+1]='\0';
    42             break;
    43         }
    44         p=p->next[t];
    45     }
    46     printf("%s\n",ss);
    47 
    48 }
    49 int main ()
    50 {
    51     char s[1010][100];
    52     int i,j;
    53     struct node *p;
    54     p=creat();
    55     for(i=0;scanf("%s",s[i]) != EOF;i++)
    56          insert(p,s[i]);
    57     for(j=0;j<i;j++)
    58     {
    59         printf("%s ",s[j]);
    60         shortt(p,s[j]);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    MongoDB 基本概念
    MongoDB 设置参数
    MongoDB 操作数据库
    MongoDB 目录分析、基础命令、参数设置
    Windows下MongoDB的下载安装、环境配置
    MongoDB 简介
    SQL与NoSQL
    es6 箭头函数(arrow function) 学习笔记
    WebPack 简明学习教程
    vue自定义指令
  • 原文地址:https://www.cnblogs.com/LK1994/p/2987502.html
Copyright © 2011-2022 走看看