算法笔记学习1(散列)

散列:hash,实现方法在此不再详细阐述,可以查看相关源码
哈希函数:将元素通过一个函数转化为整数,使得该整数可以尽量唯一的代表这个元素


在大部分OJ题中,有一种方法非常实用:

直接把输入的数作为数组的下标来对这个数的性质进行统计

在codeup中有几道题可供参考理解:
codeup题

问题 C: Be Unique (20)
时间限制: 1 Sec  内存限制: 32 MB
提交: 1030  解决: 483
[提交][状态][讨论版][命题人:外部导入]
题目描述
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

Accept

#include<cstdio>
#include<cstring>
 
const int maxn = 10010; 
 
int main(){
	int m;
	while(scanf("%d",&m)!=EOF){
		int hashTable[maxn]={0};
		int temp[m],i;
		for(i=0;i<m;i++){
			scanf("%d",&temp[i]);
			hashTable[temp[i]]++;
		}
		for(i=0;i<m;i++){     
			if(hashTable[temp[i]]==1){    //按输入顺序查询是否该号唯一 
				printf("%d\n",temp[i]);
				break;
			}
		}
		if(i==m){
			printf("None\n");
		}
	}
	return 0;
}

此处注意运行错误!!!
如果根据题意,用两个for循环只输出两行会显示运行错误,必须要用while来保证可以不断的输入

问题 D: String Subtraction (20)
时间限制: 1 Sec  内存限制: 32 MB
提交: 987  解决: 492
[提交][状态][讨论版][命题人:外部导入]
题目描述
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.

Accept

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 10010;
string a,b;
bool HashTable[128];
int main()
{
    getline(cin,a);
    getline(cin,b);
    int lena = a.size();
    int lenb = b.size();
    for(int i = 0;i < lenb;i++)
    {
        HashTable[b[i]] = true;
    }
    for(int i = 0;i < lena;i++)
    {
        if(HashTable[a[i]] == false)
            cout<<a[i];
    }
}

getline用法:将键盘上输入的一串字符串传入到指定的字符串中

注意 使用bool类型的数组可以判断该字符串是否出现,也就实现了题目所要求的相减

此题也是散列表思想,打擂台算法改良求出最大值对应坐标,然后输出所对应的字符

F - 字母统计
现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个。

Input
题目有多组测试数据

第一行输入一个正整数T(0<T<25)

随后T行输入一个字符串s,s长度小于1000。

当T=0时终止

Output
每组数据输出占一行,输出出现次数最多的字符;

Sample Input
3
abcd
bbaa
jsdhfjkshdfjksahdfjkhsajkf
Sample Output
a
a
j
#include<stdio.h>
#include<string.h>
int main()
{
    int x,i,max,q;
    char a[1005];
    scanf("%d",&x);
    getchar();
    while(x--)
    {
        int s[26] = {0};
        gets(a);
        for(int i = strlen(a)-1;i>=0;i--)
        {
            s[a[i]-97]++;
        }
        max = 0;
        for(int i = 0;i < 26;i++)
        {
            if(max < s[i])
            {
                max = s[i];
                q = i;
            }
        }
        if(x!=0)
        printf("%c\n",q+97);
        else
            printf("%c",q+97);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值