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 }
  • 相关阅读:
    10年后编程还有意义吗?
    专访Jeffrey Richter:Windows 8是微软的重中之重
    x86汇编指令脚本虚拟机
    基于容器的持续交付管道
    NET Core 整合Autofac和Castle
    数据结构与算法1
    Redis集群
    react + iscroll5
    MongoDB
    WebComponent
  • 原文地址:https://www.cnblogs.com/LK1994/p/2987502.html
Copyright © 2011-2022 走看看