poj2403

学会了使用map,

首先#include <map>,然后声明map<A,B>,map可以当B类型数组用,下标为A类型。成员函数find()可以查找数组中元素,如果没找到返回值为成员函数.end()的值。找到了返回下标。

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<map>
using namespace std;

#define maxm 1005

int m, n;
string st;
long long ans;
map
<string, int> dict;

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &m, &n);
ans
= 0;
for (int i = 0; i < m; i++)
{
cin
>> st;
cin
>> dict[st];
}
getchar();
while (n--)
{
ans
= 0;
while (cin >> st, st != ".")
{
if (dict.find(st) != dict.end())
ans
+= dict[st];
}
cout
<< ans << endl;
}
return 0;
}

posted @ 2011-05-20 16:04  undefined2024  阅读(202)  评论(0)    收藏  举报