poj 2403 map

题意:给出一些单词和整数对作为词典,再给出若干文章片段,求文章的值。值为其包含的在词典中出现的单词对应的数字之和。

思路:词典用map<string,int>来存储即可。注意可以直接用char*来作为map的键。另外摘自(http://www.cnblogs.com/zhixing/archive/2013/06/04/3116814.html)的char*和string之间的转换方式。

1. string转const char*
string s ="abc";
const char* c_s = s.c_str();

2. const char*转string
直接赋值即可
const char* c_s ="abc";
string s(c_s);

3. string转char*
string s ="abc";
char* c;
const int len = s.length();
c =newchar[len+1];
strcpy(c,s.c_str());

4. char*转string
char* c ="abc";
string s(c);

5. const char*转char*
const char* cpc ="abc";
char* pc =newchar[100];//足够长
strcpy(pc,c)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;
#define clc(s,t) memset(s,t,sizeof(s))
map<string,int> mm;
char s[20];
int n,m;
int main(){
    int i,res;
    scanf("%d %d",&n,&m);
    for(i = 0;i<n;i++){
        scanf("%s",s);
        scanf("%d",&mm[s]);
    }
    for(i = 0;i<m;i++){
        res = 0;
        while(scanf("%s",s) && strcmp(s,"."))
            res += mm[s];
        printf("%d\n",res);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值