zoukankan      html  css  js  c++  java
  • HDOJ4287 Intelligent IME[字典树水题]

    Intelligent IME

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 145    Accepted Submission(s): 80


    Problem Description
      We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below:
      2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o    
      7 : p, q, r, s  8 : t, u, v    9 : w, x, y, z
      When we want to input the word “wing”, we press the button 9, 4, 6, 4, then the input method will choose from an embedded dictionary, all words matching the input number sequence, such as “wing”, “whoi”, “zhog”. Here comes our question, given a dictionary, how many words in it match some input number sequences?
     
    Input
      First is an integer T, indicating the number of test cases. Then T block follows, each of which is formatted like this:
      Two integer N (1 <= N <= 5000), M (1 <= M <= 5000), indicating the number of input number sequences and the number of words in the dictionary, respectively. Then comes N lines, each line contains a number sequence, consisting of no more than 6 digits. Then comes M lines, each line contains a letter string, consisting of no more than 6 lower letters. It is guaranteed that there are neither duplicated number sequences nor duplicated words.
     
    Output
      For each input block, output N integers, indicating how many words in the dictionary match the corresponding number sequence, each integer per line.
     
    Sample Input
    1 3 5 46 64448 74 go in night might gn
     
    Sample Output
    3 2 0
     
    Source
     
    Recommend
    liuyiding
     
     
     
    题意:下面的字符串中,有多少个是按照相应的数字输入顺序得到的。
     
    code:
     1 #include<iostream>
     2 using namespace std;
     3 
     4 int n,m;
     5 char str[5100][8];
     6 int ans;
     7 
     8 struct node
     9 {
    10     int cnt;
    11     node *next[26];
    12 }*p;
    13 
    14 int key[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
    15 
    16 void build(char str[],int k,node *head)
    17 {
    18     while(k<strlen(str))
    19     {
    20         if(head->next[str[k]-'a']!=NULL)
    21         {
    22             head->next[str[k]-'a']->cnt+=1;
    23             head=head->next[str[k]-'a'];
    24         }
    25         else
    26         {
    27             head->next[str[k]-'a']=new node;
    28             head=head->next[str[k]-'a'];
    29             head->cnt=1;
    30             for(int i=0;i<26;i++)
    31                 head->next[i]=NULL;
    32         }
    33         k++;
    34     }
    35 }
    36 
    37 void search(char str[],int k,node *head)
    38 {
    39     if(head==NULL)
    40         return;
    41     if(k==strlen(str))
    42     {
    43         ans+=head->cnt;
    44         return;
    45     }
    46     for(int i=0;i<26;i++)
    47     {
    48         if(key[i]-(str[k]-'0')==0)
    49         {
    50             search(str,k+1,head->next[i]);
    51         }
    52     }
    53 }
    54 
    55 int main()
    56 {
    57     int t;
    58     scanf("%d",&t);
    59     while(t--)
    60     {
    61         p=new node;
    62         for(int i=0;i<26;i++)
    63             p->next[i]=NULL;
    64         scanf("%d%d",&n,&m);
    65         for(int i=0;i<n;i++)
    66             scanf("%s",str[i]);
    67         for(int i=0;i<m;i++)
    68         {
    69             char strt[20];
    70             scanf("%s",strt);
    71             build(strt,0,p);
    72         }
    73         for(int i=0;i<n;i++)
    74         {
    75             ans=0;
    76             search(str[i],0,p);
    77             printf("%d\n",ans);
    78         }
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    jQuery cxCalendar 日期选择器
    JS中this的4种绑定规则
    Redis入门教程
    大型网站限流算法的实现和改造
    JS中的继承(下)
    剑指Offer面试题:6.旋转数组中的最小数字
    剑指Offer面试题:5.重建二叉树
    剑指Offer面试题:4.从尾到头打印链表
    C++之运算符重载
    剑指Offer面试题:3.替换空格
  • 原文地址:https://www.cnblogs.com/XBWer/p/2677829.html
Copyright © 2011-2022 走看看