一维用法
#include <bits/stdc++.h>
using namespace std;
map<char, int> a;
int main()
{
for(int i=1; i<=5; ++i){
char c='a'+i-1;
a[c]=i;
}
for(char i='a'; i<='e'; ++i){
printf("%c %d\n", i, a[i]);
}
return 0;
}
P3370 【模板】字符串哈希
#include <bits/stdc++.h>
using namespace std;
int n, ans;
string s;
map<string, int> m;
int main()
{
scanf("%d", &n);
while(n--){
cin >> s;
if(m.count(s)==0){
ans++;
m[s]=ans;
}
}
printf("%d", ans);
return 0;
}
P3405 [USACO16DEC]Cities and States S
#include <bits/stdc++.h>
using namespace std;
int n, cnt, id1, id2;
long long ans, asd[737][737];
map<string, int> m;
string str, city, state, newcity, newstate;
int main()
{
// freopen("P3405_2.in", "r", stdin);
scanf("%d", &n);
for(int i=1; i<=n; ++i){
cin >> city >> state;
newcity=city.substr(0, 2);
newstate=state;
if(m.count(newcity)==0){
cnt++;
m[newcity]=cnt;
id1=cnt;
}
else{
id1=m[newcity];
}
if(m.count(newstate)==0){
cnt++;
m[newstate]=cnt;
id2=cnt;
}
else{
id2=m[newstate];
}
asd[id1][id2]++;
}
for(int i=1; i<=736; ++i){
for(int j=1; j<=736; ++j){
if(i==j){
continue;
}
ans+=asd[i][j]*asd[j][i];
}
}
printf("%lld", ans/2);
return 0;
}
P1102 A-B 数对
#include <bits/stdc++.h>
using namespace std;
int n, c;
int a[200000];
long long result;
map<int, long long> m;
int main()
{
cin >> n >> c;
for(int i=0; i<n; i++){
cin >> a[i];
m[a[i]]++;
a[i]-=c; //a[i]+=c;也行
}
for(int i=0; i<n; i++){
result+=m[a[i]];
}
cout << result;
return 0;
}
二维用法
P3613 【深基15.例2】寄包柜
#include <bits/stdc++.h>
using namespace std;
int n, q, opt, i, j, k;
map<int, int> a[100010];
int main()
{
scanf("%d %d", &n, &q);
while(q--){
scanf("%d", &opt);
if(opt==1){ //存入或者清空
scanf("%d %d %d", &i, &j, &k);
a[i][j]=k;
}
else if(opt==2){ //查询
scanf("%d %d", &i, &j);
//查询第i个格子的第j个格子存放的是什么
printf("%d\n", a[i][j]);
}
}
return 0;
}
P1097 [NOIP2007 提高组] 统计数字
#include <bits/stdc++.h>
using namespace std;
int n, m, asd[10010], cnt;
struct node
{
int x; //这个数是x
int num; //x出现的次数
}a[10010];
map<int, int> base;
bool cmp(node node1, node node2)
{
return node1.x<node2.x;
}
int main()
{
scanf("%d", &n); //自然数的个数 n<=2e5
for(int i=1; i<=n; ++i){
scanf("%d", &m);
if(base[m]==0){
cnt++;
asd[cnt]=m; //第cnt个不同的数是m
}
base[m]++; //m这个数出现的次数
}
//枚举cnt个不同的数
for(int i=1; i<=cnt; ++i){
a[i].x=asd[i]; //这个数
a[i].num=base[asd[i]]; //这个数出现的次数
}
sort(a+1, a+cnt+1, cmp); //排序
for(int i=1; i<=cnt; ++i){
printf("%d %d\n", a[i].x, a[i].num);
}
return 0;
}
P5266 【深基17.例6】学籍管理
#include <bits/stdc++.h>
using namespace std;
int n, opt, score, cnt;
string name;
map<string, int> a;
map<string, int>::iterator asd;
int main()
{
scanf("%d", &n);
while(n--){
scanf("%d", &opt);
if(opt==1){
cin >> name;
scanf("%d", &score);
asd=a.find(name);
if(asd==a.end()){ //找不到这个学生
cnt++; //学生数加一
a.insert(make_pair(name, score)); //添加
}
else{
a[name]=score; //修改学生成绩
}
printf("OK\n");
}
else if(opt==2){
cin >> name;
asd=a.find(name);
if(asd==a.end()){ //找不到学生
printf("Not found\n");
}
else{
printf("%d\n", a[name]); //输出成绩
}
}
else if(opt==3){
cin >> name;
asd=a.find(name);
if(asd==a.end()){ //找不到
printf("Not found\n");
}
else{
cnt--;
a.erase(asd); //能找到就删除
printf("Deleted successfully\n");
}
}
else if(opt==4){ //输出总数
printf("%d\n", cnt);
}
}
return 0;
}
P2580 于是他错误的点名开始了
查找某个key是否出现
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
map<string, int> a;
int main()
{
scanf("%d", &n);
for(int i=1; i<=n; ++i){
cin >> s;
a[s]=0;
}
scanf("%d", &m);
for(int i=1; i<=m; ++i){
cin >> s;
if(a.count(s)==1){
if(a[s]==0){
printf("OK\n");
a[s]++;
}
else if(a[s]){
printf("REPEAT\n");
}
}
else{
printf("WRONG\n");
}
}
return 0;
}
P4305 [JLOI2011]不重复数字
#include <bits/stdc++.h>
using namespace std;
int t, n, x, cnt;
map<int , int> m;
int main()
{
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i=1; i<=n; ++i){
scanf("%d", &x);
if(m.count(x)==0){
cnt++;
m[x]=cnt;
printf("%d ", x);
}
}
printf("\n");
m.clear();
}
return 0;
}
P3879 [TJOI2010] 阅读理解
map 结合 vector
#include <bits/stdc++.h>
using namespace std;
int n, m, l, cnt, id;
string str;
map<string, int> asd;
vector<int> v[5000010];
bool vis[1010];
int main()
{
scanf("%d", &n);
for(int i=1; i<=n; ++i){
scanf("%d", &l);
for(int j=1; j<=l; ++j){
cin >> str;
if(asd.count(str)==0){
cnt++;
asd[str]=cnt;
id=cnt;
}
else{
id=asd[str];
}
v[id].push_back(i);
}
}
scanf("%d", &m);
while(m--){
cin >> str;
memset(vis, 0, sizeof(vis));
if(asd.count(str)==0){
printf("\n");
}
else{
id=asd[str];
for(int i=0; i<v[id].size(); ++i){
if(!vis[v[id][i]]){
vis[v[id][i]]=true;
printf("%d ", v[id][i]);
}
}
printf("\n");
}
}
return 0;
}
P2264 情书 (map映射、字符串题目,题面描述不清晰)
#include <bits/stdc++.h>
using namespace std;
int n, ans, cnt, len;
map<string, int> m;
string word;
bool vis[110];
int main()
{
freopen("P2264_3.in", "r", stdin);
scanf("%d", &n);
while(n--){
cin >> word;
len=word.length();
//转为小写
for(int i=0; i<len; ++i){
if(word[i]>='A' && word[i]<='Z'){
word[i]=word[i]-'A'+'a';
}
}
cnt++;
m[word]=cnt; //使用map将单词映射为整数
}
while(cin>>word){
char ch=getchar();
len=word.length();
string newstr="";
//转为小写
for(int i=0; i<len; ++i){
if(word[i]==','){
if(m.count(newstr) && !vis[m[newstr]]){ //有感动值, 并且在这一句中还没出现过
ans++;
vis[m[newstr]]=true; //在这一句中已经出现了
}
newstr="";
}
else if(word[i]=='.'){
if(m.count(newstr) && !vis[m[newstr]]){ //有感动值, 并且在这一句中还没出现过
ans++;
vis[m[newstr]]=true; //在这一句中已经出现了
}
newstr="";
//遇到句号, 清空标记数组
memset(vis, 0, sizeof(vis));
}
else{
if(word[i]>='A' && word[i]<='Z'){
word[i]=word[i]-'A'+'a';
}
newstr+=word[i];
}
}
if(m.count(newstr) && !vis[m[newstr]]){ //有感动值, 并且在这一句中还没出现过
ans++;
vis[m[newstr]]=true; //在这一句中已经出现了
}
if(ch=='\n') break;
}
printf("%d", ans);
return 0;
}
P2264_2.in
3
love
so
much
I love you so, much I love you so. Much.
P2264_2.out
4
P2264_3.in
1
SB
Yuno is S B. Yuno is SB. Yuno is SB SB. Yuno is SB,SB. Yuno is SB.SB.
P2264_3.out
5