HDU 4287 Intelligent IME

来源:互联网 发布:mac 磁盘工具 bootcamp 编辑:程序博客网 时间:2024/04/29 03:26

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

13 5466444874goinnightmightgn
 

Sample Output

32

0

英语题,我也没读懂,不过主要意思我明白了,大致是这样的:

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

这些要求就是只要是这些字符,就转化为相应的数字字符形式。

输入有T行,每行先输入N和M

先输入N行,然后再输入M行

把M行输入的都转化为数字,然后与N行中的数比较,统计个数,输出。

刚开始用的是atoi,结果一直时间超限,没闹明白为什么会这样,哪位大神帮忙测测这个代码的运行时间吧。

是不是atoi运行起来特别耗时??

//Time Limit Exceeded

#include<stdio.h>#include<string.h>#include<math.h>#include <stdlib.h>#include<ctype.h>#include<iostream>#include<string>#include<algorithm>#include<set>#include<vector>#include<queue>#include<map>#include<numeric>#include<stack>#include<list>const int INF=1<<30;const int inf=-(1<<30);using namespace std;char ss(char c){    if(c<='c')        c='2';    else if(c<='f')        c='3';    else if(c<='i')        c='4';    else if(c<='l')        c='5';    else if(c<='o')        c='6';    else if(c<='s')        c='7';    else if(c<='v')        c='8';    else if(c<='z')        c='9';    return c;}char s[5001][5001];char str[5000];int a[5001],b[5000];int main(){    int T,n,m,j;    cin>>T;    while(T--)    {        memset(b,0,sizeof(b));        memset(str,0,sizeof(str));        cin>>n>>m;        for(int i=0; i<n; i++)        {            cin>>a[i];        }        for(int i=0; i<m; i++)        {            cin>>s[i];        }        for(int i=0; i<m; i++)        {            for( j=0; s[i][j]!='\0'; j++)            {                str[j]=ss(s[i][j]);            }            str[j]='\0';            for(int k=0; k<n; k++)                if(a[k]==atoi(str))                    ++b[k];        }        for(int i=0; i<n; i++)            cout<<b[i]<<endl;    }    return 0;}

//AC(这两个代码唯一的区别就是atoi和累加)

#include<stdio.h>#include<string.h>#include<math.h>#include <stdlib.h>#include<ctype.h>#include<iostream>#include<string>#include<algorithm>#include<set>#include<vector>#include<queue>#include<map>#include<numeric>#include<stack>#include<list>const int INF=1<<30;const int inf=-(1<<30);using namespace std;char ss(char c){    if(c<='c')        c='2';    else if(c<='f')        c='3';    else if(c<='i')        c='4';    else if(c<='l')        c='5';    else if(c<='o')        c='6';    else if(c<='s')        c='7';    else if(c<='v')        c='8';    else if(c<='z')        c='9';    return c;}char s[5001][5001];char str[5000];int a[5000],b[5000];//b数组用来累加数量int main(){    int T,n,m,j;    cin>>T;    while(T--)    {        memset(b,0,sizeof(b));        memset(str,0,sizeof(str));        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        for(int i=0; i<m; i++)        {            int sum=0;            scanf("%s",s[i]);            for( j=0; s[i][j]!='\0'; j++)            {                str[j]=ss(s[i][j]);                sum=sum*10+str[j]-'0';            }//            str[j]='\0';            for(int k=0; k<n; k++)                if(a[k]==sum)//判断是否a[k]==sum,是的话,用对应位置的b[k]统计个数,然后输出                    ++b[k];        }        for(int i=0; i<n; i++)            printf("%d\n",b[i]);    }    return 0;}


0 0
原创粉丝点击