初级数据结构-哈希

 

//查重
//unordered_map写法
#include 

using namespace std;

int n, m, a[200001], b[200001];
unordered_mapc;
int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i<= n; i++)
		scanf("%d",&a[i]);
	for(int i = 1; i<= m; i++)
		scanf("%d",&b[i]);
	c.clear();
	for(int i = 1; i <= n; i++)
		c[a[i]] = 1;
	int x = 0;
	for(int i = 1; i <= m; i++)
		if(c.find(b[i]) != c.end())
			x++;
	if(2 * x >=m)
		printf("Yes\n");
	else
		printf("No\n");
}
//vector写法
#include 

using namespace std;

const int P = 999971;
int n, m, a[200001], b[200001];
vector c[P];

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i<= n; i++)
		scanf("%d",&a[i]);
	for(int i = 1; i<= m; i++)
		scanf("%d",&b[i]);
	for(int i = 0; i< P; i++)
		c[i].clear();
	for(int i = 1; i<= n; i++){
		c[a[i] % P].push_back(a[i]);
	}
	int x = 0;
	for(int i = 1; i<= m; i++){
		int v = b[i] % P;
		int l = c[v].size();
		bool ok = false;
		for(int j = 0; j< l && !ok; j++)
			if(c[v][j] == b[i])
				ok = true;
		if(ok)
			x++;
	}
	if(2 * x >=m)
		printf("Yes\n");
	else
		printf("No\n");
}
//学生信息管理
#include 

using namespace std;
int n, m;
struct Info{
	int x, y ,z;
};
unordered_map c;

int main(){
	scanf("%d%d",&n, &m);
	for(int i = 1; i <= n; i++){
		string str;
		Info tmp;
		char s[21];
		scanf("%s",s);
		str = s;
		scanf("%d%d%d",&tmp.x,&tmp.y,&tmp.z);
		c[str] = tmp;
	}
	for(int i = 1; i <= m; i++){
		string str;
		char s[21];
		scanf("%s",s);
		str = s;
		if(c.find(str) == c.end())
			printf("-1 -1 -1\n");
		else
			printf("%d %d %d\n", c[str].x,c[str].y,c[str].z);
	}
}
//求重数
#include 

using namespace std;

int n, m, a[200001], b[200001];
unordered_mapc;
int main(){
	scanf("%d",&n);
	c.clear();
	for(int i = 1; i <= n; i++){
		int x;
		scanf("%d", &x);
		++c[x];
	}
	int x = 0, l = 0;
	for(auto itr : c){
		if(itr.second > x)
			x = itr.second, l = 0;
		if(itr.second == x)
			a[++l] = itr.first;
	}
	sort(a + 1, a + l + 1);
	for(int i = 1; i <= l; i++)
		printf("%d\n", a[i]);
	printf("\n");
}
//构造最长回文数
#include 

using namespace std;

int n, m, a[200001], b[200001];
unordered_mapc;
int main(){
	scanf("%d",&n);
	c.clear();
	for(int i = 1; i <= n; i++){
		int x;
		scanf("%d", &x);
		++c[x];
	}
	int x = 0;
	bool old = false;
	for(auto itr : c){
		if(itr.second & 1)
			old = true;
		x += itr.second / 2 * 2;
	}
	if(old)
		++x;
	printf("%d\n", x);
}
//小蜗蜗的疑问
#include 

using namespace std;

const int p = 9999971, base = 101;

int n,m,ha[200011], hb[200011], c[200011];
char a[200011], b[200011];

int main(){
	scanf("%d%d",&n, &m);
	scanf("%s%s", a+1,b+1);
	c[0] = 1;
	for(int i = 1; i <= 200000; i++)
		c[i] = c[i - 1] * base % p;
	for(int i = 1; i <= n; i++)
		ha[i] = (ha[i - 1] * base + a[i] - 'a') % p;
	for(int i = 1; i <= n; i++)
		hb[i] = (hb[i - 1] * base + b[i] - 'a') % p;
	int ans = 0;
	for(int i = 1; i + m - 1 <= n; i++)
		if((ha[i + m - 1] - 1LL * ha[i - 1] * c[m] % p + p) % p == hb[m])
			++ans;
	printf("%d\n", ans);
}

你可能感兴趣的:(算法学习,哈希算法,算法,c++)