zoukankan      html  css  js  c++  java
  • 5.2.1 Hat’s Words

    Hat’s Words

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

    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

    思路:正解该用字典树啊!我怎么这么偷懒,不学新东西,直接用map就过了 TAT

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <string>
     7 #include <map>
     8 using namespace std;
     9 
    10 map<string,int> mp;
    11 const int maxn=50100,maxl=20;
    12 int n,l;
    13 char s[maxn][maxl],s1[maxl],s2[maxl];
    14 
    15 void close()
    16 {
    17     exit(0);
    18 }
    19 
    20 void work()
    21 {
    22 }
    23 
    24 void init ()
    25 {
    26 n=0;
    27    while (scanf("%s",s[n])!=EOF)
    28     {
    29         mp[s[n]]=n;
    30         n++;
    31     }
    32     for (int i=0;i<n;i++)
    33     {
    34         l=strlen(s[i]);
    35         for (int j=0;j<l-1;j++)
    36         {
    37             memset(s1,'\0',sizeof(s1));
    38             memset(s2,'\0',sizeof(s2));
    39             for (int k=0;k<=j;k++)
    40                 s1[k]=s[i][k];
    41             s1[j+1]='\0';
    42             for (int k=j+1;k<l;k++)
    43                 s2[k-j-1]=s[i][k];
    44             s2[l]='\0';
    45         //    cout<<s[i]<<": "<<s1<<" "<<s2<<'\n';
    46             if (mp.find(s1)!=mp.end() && mp.find(s2)!=mp.end())
    47             {
    48                 puts(s[i]);
    49                 break;
    50             }
    51         }
    52     }
    53 }
    54 
    55 int main ()
    56 {
    57     init();
    58     work();
    59     close();
    60     return 0;
    61 }
  • 相关阅读:
    java(JDBC连接数据库)[完整版封装]
    java(JDBC连接数据库)[对Statement进行封装]
    HTML基础(DTD & 注释 &常见HTML编码)
    HTML基础(基本结构)
    HTML基础(格式标签)
    java(安全方便的从控制台读入数据)[对Scanner类进行封装,用正则表达式判断]
    java(JDBC连接数据库)[对PreparedStatement进行封装]
    for循环
    什么是操作系统
    字符串内置方法
  • 原文地址:https://www.cnblogs.com/cssystem/p/2912720.html
Copyright © 2011-2022 走看看