Time Limit: 1000 MS Memory Limit: 32768 KB
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
string类转换为int首先用string类的c_str()方法将string类型的数据转化为char数组,返回值是char *类型,然后再利用cstdlib中的atoi函数将其转换为int类型。
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
map<char,char> mp1;
map<int,int> mp2;
int dat[5005];
int main()
{
mp1['a']='2';
mp1['b']='2';
mp1['c']='2';
mp1['d']='3';
mp1['e']='3';
mp1['f']='3';
mp1['g']='4';
mp1['h']='4';
mp1['i']='4';
mp1['j']='5';
mp1['k']='5';
mp1['l']='5';
mp1['m']='6';
mp1['n']='6';
mp1['o']='6';
mp1['p']='7';
mp1['q']='7';
mp1['r']='7';
mp1['s']='7';
mp1['t']='8';
mp1['u']='8';
mp1['v']='8';
mp1['w']='9';
mp1['x']='9';
mp1['y']='9';
mp1['z']='9';
int t;
cin>>t;
for(int i=1;i<=t;++i)
{
int n,m;
string s;
string ss;
cin>>n>>m;
for(int j=1;j<=n;++j)
{
cin>>dat[j];
}
for(int j=1;j<=m;++j)
{
s.clear();
ss.clear();
cin>>s;//存放单词
for(int k=0;k<s.size();++k)
{
ss+=mp1[s[k]];//得到单词对应的string类型的数字
}
mp2[atoi(ss.c_str())]++;//string转int,将对应数字的单词数量++
}
for(int j=1;j<=n;++j)
{
cout<<mp2[dat[j]]<<endl;
}
mp2.clear();
}
return 0;
}