zoukankan      html  css  js  c++  java
  • HDOJ 1075

    字典树

    9890974 2013-12-25 15:31:06 Accepted 1075 468MS 59832K 1342 B G++ 泽泽
     1 #include<stdio.h>
     2 #include<cstring>
     3 #include<cstdlib>
     4 struct node
     5 {
     6     node *next[26];
     7     int key;
     8     char ans_s[10];
     9 }root;
    10 void insert(char *str,char *s)
    11 {
    12     int len=strlen(str);
    13     node *p=&root,*q;
    14     for(int i=0;i<len;i++)
    15     {
    16         int id=str[i]-'a';
    17         if(p->next[id]==NULL)
    18         {
    19             q=(node *)malloc(sizeof(root));
    20             q->key=1;
    21             for(int j=0;j<26;j++)
    22                 q->next[j]=NULL;
    23             p->next[id]=q;
    24             p=p->next[id];
    25         }
    26         else
    27             p=p->next[id];
    28     }
    29     if(p->key!=-1)
    30     {
    31         strcpy(p->ans_s,s);
    32         p->key=-1;
    33     }
    34 }
    35 void find(char *str)
    36 {
    37     int len=strlen(str);
    38     node *p=&root;
    39     for(int i=0;i<len;i++)
    40     {
    41         int id=str[i]-'a';
    42         if(p->next[id]==NULL)
    43         {
    44             printf("%s",str);
    45             return ;
    46         }
    47         else
    48         {
    49             p=p->next[id];
    50         }
    51     }
    52     if(p->key==-1)
    53         printf("%s",p->ans_s);
    54     else
    55         printf("%s",str);
    56     return;
    57     
    58 }
    59 int main()
    60 {
    61     int i;
    62     char s1[11],s2[11],s[3001];
    63     for(i=0;i<26;i++)
    64         root.next[i]=NULL;
    65     scanf("%s",s1);
    66     while(scanf("%s",s1)!=EOF&&strcmp(s1,"END")!=0)
    67     {
    68         scanf("%s",s2);
    69         insert(s2,s1);
    70     }
    71     scanf("%s",s1);
    72     getchar();
    73     while(gets(s)&&strcmp(s,"END")!=0)
    74     {
    75         int k=0,len=strlen(s); 
    76         for(i=0;i<len;i++)
    77         {
    78             while(s[i]>='a'&&s[i]<='z')//满足是字母,wa了一次
    79             {
    80                 s2[k++]=s[i++];
    81             }
    82             s2[k]='';
    83             k=0;
    84             find(s2);
    85             printf("%c",s[i]);
    86         }
    87         printf("
    ");
    88     }
    89     
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    关于mysql主从架构master宕机
    恢复drop后的表空间
    rman 恢复drop 表空间模拟恢复
    backup controlfile 和create standby controlfile
    catalog
    rman恢复表(不完全恢复)--需要关闭数据库
    rman备份恢复
    oracle DG 启动和关闭顺序
    ASM-本地数据拷贝
    图解Java设计模式之模板模式
  • 原文地址:https://www.cnblogs.com/zeze/p/hdoj1075.html
Copyright © 2011-2022 走看看