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 }
  • 相关阅读:
    warning: LF will be replaced by CRLF in ***. The file will have its original line endings in your working directory.
    GitHub出现Permissiondenied (publickey).
    浏览器编辑web页面的方法
    用scikit-learn进行LDA降维
    支持向量机原理(二) 线性支持向量机的软间隔最大化模型
    奇异值分解(SVD)原理与在降维中的应用
    scikit-learn K近邻法类库使用小结
    矩阵分解在协同过滤推荐算法中的应用
    精确率与召回率,RoC曲线与PR曲线
    异常点检测算法小结
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3676694.html
Copyright © 2011-2022 走看看