zoukankan      html  css  js  c++  java
  • HDU -- 1247 Hat’s Words

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6912    Accepted Submission(s): 2543


    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

     思路:年前学得Trie树,当时没过,可能当时没完全理解吧,今天碰到了,就凭着理解直接敲的,调了两次终于过了,把每个单词结尾标记为1,查询的时候看某个单词的子串是否存在,这题WA点不少,必须搞清判断条件。

     1 #include<cstdio>
     2 #include<string>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 class Node{
     8     public:
     9         int cnt;
    10         Node *child[26];
    11         Node(){
    12             cnt = 0;
    13             for(int i = 0;i < 26;i ++) child[i] = NULL;
    14         }
    15 };
    16 Node *root = new Node();
    17 char str[50005][111];
    18 void Update(char *str){
    19     Node *tmp = root;
    20     while(*str != ''){
    21         if(tmp->child[*str-'a'] == NULL){
    22             Node *p = new Node();
    23             tmp->child[*str-'a'] = p;
    24         }
    25         tmp = tmp->child[*str-'a'];
    26         str++;
    27     }
    28     tmp->cnt = 1;
    29 }
    30 bool Judge(char *str){
    31     Node *tmp = root;
    32     while(*str != ''){
    33         if(tmp->child[*str-'a'] == NULL) return false;
    34         tmp = tmp->child[*str-'a'];
    35         str++;
    36     }
    37     return tmp->cnt == 1;
    38 }
    39 int main(){
    40     int n = 0;
    41 //    freopen("in.cpp","r",stdin);
    42     memset(str,0,sizeof(str));
    43     while(~scanf("%s",str[n])) Update(str[n]),n++;
    44     for(int i = 0;i < n;i ++){
    45         int len = strlen(str[i]);
    46         char tmp1[111],tmp2[111];
    47         memset(tmp1,0,sizeof(tmp1));
    48         memset(tmp2,0,sizeof(tmp2));
    49         for(int j = 1;j < len;j ++){
    50             int p = 0;
    51             for(int k = 0;k < j;k ++) tmp1[p++] = str[i][k];
    52             p = 0;
    53             for(int k = j;k < len;k ++) tmp2[p++] = str[i][k];
    54             if(Judge(tmp1) && Judge(tmp2)) {
    55                 printf("%s
    ",str[i]);
    56                 break;
    57             }
    58             memset(tmp1,0,sizeof(tmp1));
    59             memset(tmp2,0,sizeof(tmp2));
    60         }
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    日期格式化
    面向对象高级编程——使用__slots__
    面向对象编程——实例属性和类属性(六)
    面向对象编程——获取对象信息(五)
    面向对象编程——类方法和静态方法(八)
    面向对象编程——多重继承(七)
    面向对象高级编程——使用@property
    python常用模块——logger模块
    python常用模块——hashlib模块
    面向对象编程——super进阶(十)
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3676694.html
Copyright © 2011-2022 走看看