2.9每日总结

于是他错误的点名开始了

#include <bits/stdc++.h>
using namespace std;
map<string,int>tt;

int main ()
{
	int n, m;
	cin >> n;
	string a;
	while(n --){
		cin >> a;
		tt[a] = 1;
	}
	cin >> m;
	while(m --){
		string b;
		cin >> b;
		if(tt[b] == 1){
			cout << "OK" << endl;
			tt[b] = 2;
		}
		else if(tt[b] == 2){
			cout << "REPEAT" << endl;
		}
		else {
			cout << "WRONG" << endl;
		}
	}
	return 0;
}

这里我用到了map函数

用数组来记录每个名字出现的次数 最后得出答案

A-B 数对

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>a;
int main ()
{
	ll b[10000001];
	ll n, c, ans;
	ans = 0;
	cin >> n >> c;
	for(int i = 1;i <= n;i++){
		cin >> b[i];
		a[b[i]] ++;
	}
	for(int i = 1;i <= n;i++){
		ans += a[b[i] - c];
	}
	cout << ans;
	
	return 0;
}

这里有运用到了map函数

记录每个数字出现的次数

既然是减法

那就求出相差c的数的个数

【模板】字符串哈希

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

typedef unsigned long long ull;

ull base = 131;
ull a[10010];
char s[10010];
int n,ans = 1;
ull hashs(char s[])
{
    int len = strlen(s);
    ull ans = 0;
    for (int i = 0;i < len;i ++)
        ans = ans * base + (ull)s[i];
    return ans&0x7fffffff;     //0x7fffffff  代表int的最大值
}
int main()
{
    scanf("%d",&n);
    for (int i = 1;i <= n;i++)
    {
        scanf("%s",s);
        a[i] = hashs(s);
    }
    sort(a + 1,a + n + 1);
    for (int i = 2;i <= n;i ++)
        if (a[i] != a[i - 1])
            ans ++;
    printf("%d\n",ans);
}

这里运用了哈希表

把输进去的每个值用一个唯一的数代替   最后记录在一个数组中

在给数组进行排序

把相邻的不相等的数一一数出来   得答案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值