算法竞赛进阶指南学习day8

关于map:

简介:map 是有序键值对容器,它的元素的键是唯一的。搜索、移除和插入操作拥有对数复杂度——oiwiki

特点:键可以为字符型

建立一个map:       

 map<key,num> mp;//key为键,num为对应值

增:

法一

mp.insert(pair<string,int>("key",num));//insert的方法添加需要以pair的形式

 

法二、直接用键进行赋值

mp[key]=num;

删:

mp.erase(key/pos);//字面意思,删除键或迭代器对应的值       

mp.erase(pos1,pos2);//删除两个迭代器之间的元素 

mp.clear()//清空

 

查:

mp.count(key)//返回键为key的元素数量

mp.find(x)//返回键为x的迭代器,若没有返回end().

lower_bound()和upper_bound()跟原来一样

mp.empty()//返回是否为空

mp.size()//返回元素个数

接下来探究oiwiki说的”有序“到底是怎么个有序法

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
using namespace std;
map<string,int> mp;
int main(){
	mp["a"]=2;
	mp["b"]=2;
	mp["c"]=3;
	map<string,int>::iterator n;
	n=mp.begin() ;
	for(int i=1;n!=mp.end();i++,n++){
		cout<<i<<"、"<<(*n).first<<" "<<(*n).second<<endl;
	}
	mp.clear() ;
	mp["c"]=1;
	mp["1"]=3;
	mp["C"]=2;
	n=mp.begin() ;
	cout<<endl;
	for(int i=1;n!=mp.end();i++,n++){
		cout<<i<<"、"<<(*n).first<<" "<<(*n).second<<endl;
	}
	cout<<int('a')<<" "<<int('A')<<" "<<int('1');
	return 0;
}

结果:

1、a 2
2、b 2
3、c 3

1、1 3
2、C 2
3、c 1
97 65 49
--------------------------------
Process exited after 0.01444 seconds with return value 0
请按任意键继续. . .

由此可以得出map中是按键的升序进行排序的

map例(shui)题——点名  p2580

(map吊打字典树)

题目:

输入:

第一行一个整数 nn,表示班上人数。

接下来 nn 行,每行一个字符串表示其名字(互不相同,且只含小写字母,长度不超过 5050)。

第 n+2n+2 行一个整数 mm,表示教练报的名字个数。

接下来 mm 行,每行一个字符串表示教练报的名字(只含小写字母,且长度不超过 5050)。

输出:

对于每个教练报的名字,输出一行。

如果该名字正确且是第一次出现,输出 OK,如果该名字错误,输出 WRONG,如果该名字正确但不是第一次出现,输出 REPEAT

#include<iostream>
#include<string>
#include<cstring>
#include<map> 
using namespace std;
map<string,int > stu;
int main(){
	int n,m;
	cin>>n;
	string a,b; 
	while(n--){
		cin>>a;
		stu[a]=1; 
	} 
	cin>>m;
	while(m--){
		cin>>b;
		if(stu[b]==1){
			stu[b]++;
			cout<<"OK"<<endl;
		}else if(stu[b]>1){
			cout<<"REPEAT"<<endl;
		}else{
			cout<<"WRONG"<<endl;
		}
	} 
	
}

关于KMP:https://www.cnblogs.com/dusf/p/kmp.html这里超详细,干看代码的话挺不好理解的

CH1601

字典树板子

不同于上面字典树被map吊打,字典树在处理前缀匹配问题上具有更好的效果

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
using namespace std;
struct tree{
	int end;
	int a[26];
}trie[1000001];
int tot=1;
void insert(char *str){
	int len=strlen(str),p=1;
	for(int k=0;k<len;k++){
		int ch=str[k]-'a';
		if(trie[p].a[ch]==0)
			trie[p].a[ch]=++tot;
		p=trie[p].a[ch];
	}
	trie[p].end++;
}
int find(char *str){
	int len=strlen(str),p=1,num=0;
	for(int k=0;k<len;k++){
		p=trie[p].a[str[k]-'a'];
		num+=trie[p].end;
		if(p==0)	break;
	}
	return num;
}
int main(){
	int n,m;
	cin>>n>>m;
	char s[100001];
	for(int i=0;i<n;i++){
		cin>>s;
		insert(s);
	}
	for(int i=0;i<m;i++){
		cin>>s;
		cout<<find(s)<<endl;
	}
	return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值