zoukankan      html  css  js  c++  java
  • hdu 4287 Intelligent IME

    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

    题解:手机九宫格,字母与对应的数字....   自己过了案例,但是还是WA,之后才知道自己写的代码不能判断相等的情况....srO

    WA代码:

      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 
     13 struct node
     14 {
     15     int count;     
     16     node *next[200];    
     17     node(){          //构造函数
     18         count=0;
     19         memset(next,0,sizeof(next));
     20     }
     21 };
     22 node *root;
     23 int k=0;
     24 void insert(char *a)
     25 {
     26     int l=strlen(a);
     27     node *p=root;
     28     int i;
     29     for(i=0;i<l;i++)
     30     {
     31         if(p->next[a[i]-'a']==0)
     32         {
     33             p->next[a[i]-'a']=new node;
     34             if(a[i]<'a'&&a[i]>'z')
     35                p->next[a[i]-'a']->count=p->count+0;
     36             if(a[i]>='a'&&a[i]<='c')
     37                p->next[a[i]-'a']->count+=p->count+2;
     38             if(a[i]>='d'&&a[i]<='f')
     39                p->next[a[i]-'a']->count+=p->count+3;
     40             if(a[i]>='g'&&a[i]<='i')
     41                p->next[a[i]-'a']->count+=p->count+4;
     42             if(a[i]>='j'&&a[i]<='l')
     43                p->next[a[i]-'a']->count+=p->count+5;
     44             if(a[i]>='m'&&a[i]<='o')
     45                p->next[a[i]-'a']->count+=p->count+6;
     46             if(a[i]>='p'&&a[i]<='s')
     47                p->next[a[i]-'a']->count+=p->count+7;
     48             if(a[i]>='t'&&a[i]<='v')
     49                p->next[a[i]-'a']->count+=p->count+8;
     50             if(a[i]>='w'&&a[i]<='z')
     51                p->next[a[i]-'a']->count+=p->count+9;
     52         }    
     53         // 已存在此前缀
     54         p=p->next[a[i]-'a'];
     55     }
     56 }
     57 int find(char *s)
     58 {
     59     struct node *p;
     60     int len=strlen(s);
     61     if(len==0) return 0;
     62     p=root;
     63     for(int i=0;i<len;i++){
     64         if(p->next[s[i]-'a']!=0)
     65             p=p->next[s[i]-'a'];
     66         else
     67             return 0;
     68     }
     69     return p->count;
     70 }
     71 void de(node *p)
     72 {
     73     if(p==0)
     74         return ;
     75     int i;
     76     for(i=0;i<26;i++)
     77     {
     78         de(p->next[i]);
     79     }
     80     delete p;
     81 
     82 }
     83 int main()
     84 {
     85     int t;
     86     scanf("%d",&t);
     87     char a[30];
     88     int b[5050],s[5050];
     89     while(t--)
     90     {
     91         root = new node;
     92         memset(b,0,sizeof(b));
     93         memset(s,0,sizeof(s));
     94         memset(a,'',sizeof(a));
     95         int n,m,p,x,j,sum=0;
     96         k=0;
     97         scanf("%d%d",&n,&m);
     98         int i;
     99         for(i=0;i<n;i++){
    100             scanf("%d",&p);
    101             while(p!=0){
    102                 x=p%10;
    103                 p/=10;
    104                 k+=x;
    105             }
    106             b[i]=k;
    107             k=0;
    108         }
    109         /*for (i = 0; i < n; ++i)
    110         {
    111             cout<<b[i]<<endl;
    112         }*/
    113         for(i=0;i<m;i++)
    114         {
    115             scanf("%s",a);
    116             insert(a);
    117             s[i]=find(a);
    118         }
    119         /*for (i = 0; i < m; ++i)
    120         {
    121             cout<<s[i]<<endl;
    122         }*/
    123         for (i = 0; i < n; ++i)
    124         {
    125             for(j=0;j<m;j++){
    126                 if(b[i]==s[j]){
    127                     sum++;
    128                 }
    129             }
    130             printf("%d
    ",sum);
    131             sum=0;
    132         }
    133         de(root);
    134     }
    135     return 0;
    136 }
    View Code

    玉民代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <stdio.h>
     5 #include <cstring> 
     6 using namespace std;
     7 #define Max 13
     8 struct dot
     9 {
    10     dot *next[Max];
    11     int flag;
    12 } ;
    13 dot *newnode()
    14 {
    15     dot *temp=new dot;
    16          temp->flag=0;
    17     for(int i=0;i<Max;i++)
    18        temp->next[i]=NULL;
    19     return temp;
    20 }
    21 void tree(char *st,dot *root)
    22 {
    23     dot *p=root;
    24     int id=0;
    25     int len=strlen(st);
    26     for(int i=0;i<len;i++)
    27     {
    28         if(st[i]>='a'&&st[i]<='c')
    29           id=2;
    30         else if(st[i]>='d'&&st[i]<='f')
    31           id=3;
    32         else if(st[i]>='g'&&st[i]<='i')
    33           id=4;
    34         else if(st[i]>='j'&&st[i]<='l')
    35           id=5;
    36         else if(st[i]>='m'&&st[i]<='o')
    37           id=6;
    38         else if(st[i]>='p'&&st[i]<='s')
    39           id=7;
    40         else if(st[i]>='t'&&st[i]<='v')
    41           id=8;
    42         else if(st[i]>='w'&&st[i]<='z')
    43           id=9;
    44         if(p->next[id]==NULL)
    45             p->next[id]=newnode();
    46         p=p->next[id];
    47     }
    48     p->flag++;
    49 }
    50 int find(char *st,dot *root)
    51 {
    52     int id;
    53     dot *p=root;
    54     for(int i=0;i<strlen(st);i++)
    55     {
    56         id=st[i]-'0';
    57         if(p->next[id]==NULL)
    58             return 0;
    59         p=p->next[id];
    60     }
    61     return p->flag;
    62 }
    63 void del(dot *t)
    64 {
    65     if(t==NULL) return ;
    66     for(int i=0;i<Max;i++)
    67        if(t->next[i]==NULL)
    68           del(t->next[i]);
    69     delete t;
    70 }
    71 int main()
    72 {
    73     int n,m,i,j,k,t;
    74     cin>>t;
    75     char s[5005][20],st[30];
    76     while(t--)
    77     {
    78         cin>>n>>m;
    79         dot *root=new dot;
    80            root=newnode();
    81         for(i=0;i<n;i++)
    82            cin>>s[i];
    83         while(m--)
    84         {
    85             cin>>st;
    86             tree(st,root);
    87         }
    88         for(i=0;i<n;i++)
    89           cout<<find(s[i],root)<<endl;
    90         del(root);
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    tinymce富文本的实践(vue)
    package.json 版本号说明
    Java注解代码生成
    swagger注释API :@ApiModel ....
    Vue的生命周期函数和beforeRouteEnter()/beforeRouteLeave()函数
    各类程序员学习路线图
    mock.js的使用方法
    推荐使用rpx替代upx的公告
    DispatcherServlet解读
    学会fetch的用法
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5017671.html
Copyright © 2011-2022 走看看