STL map的使用

刚学了标准模板库的map,感觉很好很强大,所以马上做题试试!!!

map是一个容器。

最直观的用法就是先定义一个容器:map<string, int> mapsi;

通过这样子来映射——mapsi[字符串] = 整形数字

最后遍历的话,用迭代器,it->first是关键字(索引),it->second是映射的内容

另外,这个容器里面元素的类型实际上是pair<>,上面的例子中元素的类型就是pair<const string, int>


hdoj1004

这个最典型最基础了,完全是裸的——

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
	int n;
	string tmp;
	typedef map<string, int> msi;
	msi mapsi;
	while (cin >> n && n) {
		mapsi.clear();
		while (n--) {
			cin >> tmp;
			mapsi[tmp]++;
		}
		int maxn = -1;
		string rec;
		for (msi::iterator it = mapsi.begin(); it != mapsi.end(); it++) {
			if (it->second > maxn) {
				maxn = it->second;
				rec = it->first;
			}
		}
		cout << rec << endl;
	}
	return 0;
}


hdoj1029

这个题目用cin cout超时,1000MS+,改成scanf(), printf()搞定!

644K && 312MS

#include <iostream>
#include <cstdio>
#include <map>

using namespace std;
typedef map<int, int> mii;

int main()
{
    int n;
    mii mapii;
    while (scanf("%d", &n) != EOF) {
        mapii.clear();
        int key;
        for (int i = 0; i != n; i++) {
            scanf("%d", &key);
            mapii[key]++;
        }
        int cmpkey = (n+1)/2;
        for (mii::iterator it = mapii.begin(); it != mapii.end(); it++) {
            if (it->second >= cmpkey) {
                printf("%d\n", it->first);
                break;
            }
        }
    }
    return 0;
}


继续做题~~~


hdoj1263

376K && 15MS

这个题目是多重map,其实等价于C语言的多重数组,关键是理解key是不允许重复的,那么某个省份的某种水果其实可以用二维数组的两个下标来表示(一个坐标点)。

即某个省份的某种水果决定了其数量。

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef map<string, int> msi; // 水果种类  数量
typedef map<string, msi> msm; // 省份 水果种类(数量)

int main()
{
	int testcase, n, num;
	scanf("%d", &testcase);
	while (testcase--) {
		msm mapsm;
		mapsm.clear();
		scanf("%d", &n);
		for (int i = 0; i != n; i++) {
			char fruit[85], province[85];
			scanf("%s%s%d", fruit, province, &num);
			mapsm[province][fruit] += num; //NOTICE!
		}
		for (msm::iterator it = mapsm.begin(); it != mapsm.end(); it++) {
			cout << it->first << endl;
			for (msi::iterator i = (it->second).begin(); i != (it->second).end(); i++) {
				printf("   |----");
				cout << i->first;
				printf("(%d)\n", i->second);
			}
		}
		if (testcase != 0)
			printf("\n");
	}
	return 0;
}


hdoj1075


前阵子用Tire树做过此题,现在用map做~~

41192K && 3203MS (限制5000MS)

#include <iostream>
#include <string>
#include <cctype>
#include <map>

using namespace std;

typedef map<string, string> mss;
char line[3010];

int main()
{
	string key, mapword;
	mss mapss;
	cin >> mapword;
	while (true) {
		cin >> mapword;
		if (isupper(mapword[0]))
			break;
		cin >> key;
		mapss[key] = mapword;
	}
	cin >> mapword;
	getchar();
	while (true) {
		char word[15];
		fgets(line, sizeof (line), stdin);
		if (isupper(line[0]))
			break;
		int k = 0;
		for (char *p = line; *(p-1) != '\n'; p++) {
			if (isalpha(*p))
				word[k++] = *p;
			else {
				word[k] = '\0';
				mapss.find(word) != mapss.end() ?
					cout << mapss[word] : cout << word;
				putchar(*p);
				k = 0;
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值