欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2012 ACM/ICPC Asia Regional Tianjin Online hdu 4287 map和char[]的合作应用

程序员文章站 2022-06-16 14:28:11
intelligent ime time limit: 2000/1000 ms (java/others)    memory limit: 32768/32768...

intelligent ime
time limit: 2000/1000 ms (java/others)    memory limit: 32768/32768 k (java/others)
total submission(s): 386    accepted submission(s): 203


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
2012 acm/icpc asia regional tianjin online
 

recommend
liuyiding
 
题意:
  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
 
a 是2 b是2 c 是2  d是3 e是3.。。。。。。。
把字符串转化以后的数字串  看在上面出现了几次
理所当然 map解决
这题主要是练习 map 和char[] 的合作
 
[html] 
#include<stdio.h> 
#include<string.h> 
#include<string> 
#include<iostream> 
#include<map> 
using namespace std; 
char key[27]="22233344455566677778889999"; 
char num[5005][10],word[5005][10]; 
int main() 

    int t; 
    scanf("%d",&t); 
    while(t--) 
    { 
        int n,m,i,j,len; 
        map <string,int> m; 
        scanf("%d%d",&n,&m); 
        for(i=0;i<n;i++) 
        { 
            scanf("%s",num[i]); 
        } 
        map<string,int>::iterator it;  
        for(i=0;i<m;i++) 
        { 
            scanf("%s",word[i]); 
            len=strlen(word[i]); 
            for(j=0;j<len;j++) 
                word[i][j]=key[word[i][j]-'a']; 
            it=m.find(word[i]); 
            if(it==m.end()) 
                m[word[i]]=1; 
            else 
                m[word[i]]++; 
        }     
        for(i=0;i<n;i++) 
        { 
            it=m.find(num[i]); 
            if(it==m.end()) 
                printf("0\n"); 
            else 
                printf("%d\n",m[num[i]]); 
        } 
    } 
    return 0;