hdu 1228 A + B 详细题解 字符串/哈希

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1228

这道题可以同时用两种方法做,第一种是字符串,第二种是哈希。

第一种方法:

我们可以定义一个字符串类型的二位数组,存放”zero”……”nine”十个字符串

这十个字符串可以与下标0……9一一对应。这样就可以建立字符串与数字之间的关系了

char a[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};

我们注意到输入数据中分两个部分,我们可以把每组输入看做以下格式

“第一部分”+“第二部分”=

第一部分或第二部分有可能出现两个字符串,但是中间有空格隔开,所以我们可以用%s的形式来输入。

while(scanf("%s",num)&&strcmp(num,"+")!=0){//tra函数在下面讲
            A=10*A+tra(num);
        }
        while(scanf("%s",num)&&strcmp(num,"=")!=0){
            B=10*B+tra(num);
        }

输入进来的字符串怎样处理呢?

上文我们的tra就是处理过程。

由于我们将zero……..nine 的字符串全部存在了字符串数组a中所以我们每次都必须去查找输入进来的数字字符串(num)到底对应的数字是谁。很简单咯,把a数组遍历一遍寻找相对应的字符串的下标再返回即可。

int tra(char num[]){
    int i;
    for(i=0;i<10;i++){
        if(strcmp(num,a[i])==0)
            return i;
    }
}

到这里我们的程序差不多就完成了。

贴个完整的代码:

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

char a[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};

int tra(char num[]){
    int i;
    for(i=0;i<10;i++){
        if(strcmp(num,a[i])==0)
            return i;
    }
}

int main()
{
    int A,B;
    char num[10];
    while(1){
        A=B=0;
        while(scanf("%s",num)&&strcmp(num,"+")!=0){
            A=10*A+tra(num);
        }
        while(scanf("%s",num)&&strcmp(num,"=")!=0){
            B=10*B+tra(num);
        }
        if(A+B>0){
            printf("%d\n",A+B);
        }
        else {
            break;
        }
    }
    return 0;
}

接下来讲第二种方法:

我们可以使用stl中的map来让一个string串和int数映射起来。

map的使用方法大家可以上网百度,我就不详细说了

大概讲一下:

头文件是#include <map>

map<string,int>mp;表示定义了一个名称为mp的数组,

这个数组的形式是这样的:mp[string]=int;

也就是说数组的下表可以是一个字符串,如:mp[“zero”]=0,mp[“one”]=1……mp[“nine”]=9

这样的话就好办多了。

我们定义如下数组:

char f[11][6]= {"zero","one","two","three","four","five","six","seven","eight","nine"};

我们就可以用f数组来建立mp数组,确定映射

for (int i=0; i<10; i++)
    {
        mp[f[i]]=i;
    }

这样,映射就建立完成了。

接下来的输入跟第一种方法相似:

	int A=0,B=0;
	while(strcmp(a,"+")!=0)
        {
            A=A*10+mp[a];
            scanf("%s",a);
        }
        scanf("%s",a);
        while(strcmp(a,"=")!=0)
        {
            B=B*10+mp[a];
            scanf("%s",a);
        }
贴个完整的代码:
#include<cstdio>
#include<cstdlib>
#include <map>
#include <iostream>
#include <string.h>
using namespace std;
map<string,int>mp;
char f[11][6]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
char a[1000],b[1000];
int main()
{
    for (int i=0; i<10; i++)
    {
        mp[f[i]]=i;
    }

    while (~ scanf("%s",a))
    {
        int A=0,B=0;
        while(strcmp(a,"+")!=0)
        {
            A=A*10+mp[a];
            scanf("%s",a);
        }
        scanf("%s",a);
        while(strcmp(a,"=")!=0)
        {
            B=B*10+mp[a];
            scanf("%s",a);
        }
        if (A+B==0)
            break;
        printf("%d\n",A+B);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-13 01:23:03

hdu 1228 A + B 详细题解 字符串/哈希的相关文章

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

HDU 2553 N皇后问题(详细题解)

这是一道深搜题目!问题的关键是在剪枝. 下面我们对问题进行分析: 1.一行只能放一个皇后,所以我们一旦确定此处可以放皇后,那么该行就只能放一个皇后,下面的就不要再搜了. 2.每一列只能放一个皇后,所以我们下次搜索就不要再搜已经放过的皇后了. 3.斜的45°线也只能放一个. 综上如何才能最快速的确定一列和45°是否用过这个是个关键步骤,一旦此步骤确定我们就可以很快的进行搜索了. 我们用三个数组来保存他的每一个状态及(三个方向 ↑ ) 但是如果我们保存↑(每一列方向上的皇后)是非常容易保存的 但是保

[ACM] hdu 1228 A+B (字符串处理)

A + B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11543    Accepted Submission(s): 6699 Problem Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. Input 测试输入包含若干测试用例,每个测试用例占一行,

HDU 4821 杭州现场赛:每个片段字符串哈希比较

I - String Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4821 Description Given a string S and two integers L and M, we consider a substring of S as "recoverable" if and only if (i) I

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU 1228 A + B 的浙大考研题

Problem Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. Input 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即A+B的值. Sample Input one + two = three four + five six = zero seven + eig

HDU 1251 统计难题 Trie题解

基本上是标准的寻找前缀的问题,只需要insert和search函数就可以了. 我这里主要是修改一下n的记录方法,这里的n代表的不是叶子节点的标志,而是有多少单词经过了这条路径的标志. 然后是查找需要查找的前缀单词,如果没有找到,就返回0,表示没有单词以这个前缀单词为前缀,如果找到,直接返回n就是答案了.因为有n个单词经过了这条路径. 查找效率是常数. 使用静态分配空间的办法. #include <stdio.h> #include <string.h> const int MAX_

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? M

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1