算法笔记 4.2 codeup课后习题 算法初步->哈希

问题 A: 谁是你的潜在朋友

时间限制: 1 Sec  内存限制: 32 MB

题目描述

    “臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会 并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。
    首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。

输入

    每个案例第一行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每一行有一个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M)

输出

    每个案例包括N行,每行一个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^)

样例输入

4 5
2
3
2
1

样例输出

1
BeiJu
1
BeiJu

代码:

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int n,m,s;
	int stu[210];
	int num[210];
	while(cin>>n>>m){	
		memset(num,0,sizeof(num));
		for(int i=1;i<=n;i++){
			cin>>s;
			stu[i]=s;
			num[s]++;
		}
		for(int i=1;i<=n;i++){
			if(num[stu[i]]==1){
				cout<<"BeiJu"<<endl;
			}else{
				cout<<num[stu[i]]-1<<endl;
			}
		}
	}
	return 0;
}

问题 B: 分组统计

时间限制: 1 Sec  内存限制: 32 MB

题目描述

先输入一组数,然后输入其分组,按照分组统计出现次数并输出,参见样例。

输入

输入第一行表示样例数m,对于每个样例,第一行为数的个数n,接下来两行分别有n个数,第一行有n个数,第二行的n个数分别对应上一行每个数的分组,n不超过100。

输出

输出m行,格式参见样例,按从小到大排。

样例输入

1
7
3 2 3 8 8 2 3
1 2 3 2 1 3 1

样例输出

1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}

注意:组号不一定都是重1开始连续着的,组号也要不重复地记录一遍,然后排序

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
	int m,n;
	int s[110],st[110];//st出现数的种类  对s去重
	int z[110][10000];//z[z][s]第z组中数s的个数 
	cin>>m;	
	while(m--){
		cin>>n;
		memset(z,0,sizeof(z));
		memset(st,0,sizeof(st));
		int index=0;//出现数的种类数
		int exit[10000]={0};
		for(int i=0;i<n;i++){
			cin>>s[i];
			if(exit[s[i]]==0){
				st[index++]=s[i];
				exit[s[i]]=1;
			}
		}
		int x;
		//int max=0;//最大组号
		memset(exit,0,sizeof(exit));//组号存在否
		int zt[1000];//组号
		int indexz=0;//组号个数
		for(int i=0;i<n;i++){
			cin>>x;
			if(exit[x]==0){
				zt[indexz++]=x;
				exit[x]=1;
			}
			z[x][s[i]]++;
		}
		sort(st,st+index);
		sort(zt,zt+indexz);
		for(int i=0;i<indexz;i++){
			cout<<zt[i]<<"={";
			int j;
			for(j=0;j<index-1;j++){
				cout<<st[j]<<"="<<z[zt[i]][st[j]]<<",";
			}
			cout<<st[j]<<"="<<z[zt[i]][st[j]]<<"}\n";
		}
	}
	
	return 0;
}

问题 C: Be Unique (20)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 104]. The first one who bets on a unique number wins. For example, if there are 7 people betting on 5 31 5 88 67 88 17, then the second one who bets on 31 wins.

输入

Each input file contains one test case. Each case contains a line which begins with a positive integer N (<=105) and then followed by N bets. The numbers are separated by a space.

输出

For each test case, print the winning number in a line. If there is no winner, print "None" instead.

样例输入

7 5 31 5 88 67 88 17
5 888 666 666 888 888

样例输出

31
None

代码:

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int n;
	int no[100010],num[10010];
	while(cin>>n){
		int exit[100010]={0};
		memset(num,0,sizeof(num));
		int unique[100010]={0};//去重后的数字
		int index=0;
		for(int i=0;i<n;i++){
			cin>>no[i];
			num[no[i]]++;
			if(exit[no[i]]==0){
				exit[no[i]]=1;
				unique[index++]=no[i];
			}
		}
		int i;
		for(i=0;i<index;i++){
			if(num[unique[i]]==1){
				cout<<unique[i]<<endl;
				break;
			}
		}
		if(i==index){
			cout<<"None"<<endl;
		}
	}
	return 0;
}

问题 D: String Subtraction (20)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2for any given strings. However, it might not be that simple to do it fast.

输入

Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

输出

For each test case, print S1 - S2 in one line.

样例输入

They are students.
aeiou

样例输出

Thy r stdnts.

分析还是老样子,空间换时间,简单的散列,正好一ascii码编码肯定唯一

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	string s1,s2;
	while(getline(cin,s1)){
		int id[130]={0};
		getline(cin,s2);
		for(int i=0;i<s2.length();i++){
			id[int(s2[i])]=1;
		}		
		for(int i=0;i<s1.length();i++){
			if(id[int(s1[i])]==0){
				cout<<s1[i];
			}
		}
		cout<<endl;
		//cin.get();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hza419763578/article/details/82935030
今日推荐