zoukankan      html  css  js  c++  java
  • hdu 1247

    Problem Description
    A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
    You are to find all the hat’s words in a dictionary.
     
    Input
    Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
    Only one case.
     
    Output
    Your output should contain all the hat’s words, one per line, in alphabetical order.
     
    Sample Input
    a
    ahat
    hat
    hatword
    hziee
    word
     
    Sample Output
    ahat
    hatword
     
    题解:刚开始就老想着在  find函数里修改,其实  模板不用变,就是多了对字符串的处理。
     
    代码:
      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <math.h>
      4 #include <algorithm>
      5 #include <iostream>
      6 #include <ctype.h>
      7 #include <iomanip>
      8 #include <queue>
      9 #include <stdlib.h>
     10 using namespace std;
     11 
     12 struct Tri
     13 {
     14     bool v;
     15     Tri* child[26];
     16 };
     17 
     18 Tri* root;
     19 
     20 void Init()
     21 {
     22     root->v=false;
     23     for(int i=0;i<26;i++)
     24     {
     25         root->child[i]=NULL;
     26     }
     27 }
     28 
     29 void CreateDic(char* s)
     30 {
     31     Tri* p;
     32     int j;
     33     int len=strlen(s);
     34     if(len==0)
     35         return ;
     36     p=root;
     37     for(int i=0 ;i < len; i++)
     38     {
     39         if(p->child[s[i]-'a']==NULL)
     40         {
     41             p->child[s[i]-'a']=(Tri*)new Tri;
     42             p->child[s[i]-'a']->v=false;
     43             for(j=0;j<26;j++)
     44                 p->child[s[i]-'a']->child[j]=NULL;
     45         }
     46         p=p->child[s[i]-'a'];
     47         
     48     }
     49     p->v=true;
     50 }
     51 
     52 bool Find(char *s)
     53 {
     54     Tri* p=root;
     55     int len=strlen(s);
     56     if(len==0)
     57         return 0;
     58     for(int i=0 ;i < len; i++)
     59     {
     60         if(p->child[s[i]-'a']==NULL)
     61             return 0;
     62         p=p->child[s[i]-'a'];
     63     }
     64     return p->v;
     65 }
     66 
     67 void Del(Tri* p)
     68 {
     69     for(int i=0;i<26;i++)
     70         if(p->child[i])
     71             Del(p->child[i]);
     72     Del(p);
     73     
     74 }
     75 
     76 char total[50002][100];
     77 int main()
     78 {
     79     char a[100],b[100],c[100];
     80     int i=0,j,k;
     81 
     82     root=(Tri*)new Tri;
     83     Init();
     84     while(gets(a))
     85     {
     86         CreateDic(a);
     87         strcpy(total[i++],a);
     88     }
     89 
     90     for(j=0;j<i;j++)
     91     {
     92         for(k=1;k<strlen(total[j]);k++)
     93         {
     94             strncpy(b,total[j],k);
     95             b[k]='';
     96             strcpy(c,total[j]+k);
     97                 
     98             if(Find(b)&&Find(c))
     99             {
    100                 cout<<total[j]<<endl;
    101                 break;   //注意结束循环                   
    102             }
    103         }
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    MYSQL 日志参数与性能的关系
    mybatis 中 foreach collection的三种用法
    SqlServer查找引用存储过程的地方_字段在哪些表中
    ubuntu中sudo不用输入密码配置
    matlab练习程序(罗德里格斯变换)
    matlab练习程序(多边形扩展算法)
    matlab练习程序(生成gif图片)
    python3 mac环境 生成pdf
    requests 爬取 台州市 履行中状态的合同附件
    个人支付方案(免签约)-支付宝当面付
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5011313.html
Copyright © 2011-2022 走看看