5.3刷题

P3370 【模板】字符串哈希

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef unsigned long long ull;
int n;
ull myhash(string s){
	ull code = 0, x = 131, y = 140814840257324663;
	for(int i = 0; i < s.size(); i++){
		code = (code * x + (ull)s[i]) % y;
	}
	return code;
}
void solve(){
	ull a[10010], ans, n;
	string s;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> s;
		a[i] = myhash(s);
	}
	sort(a, a + n);
	ans = unique(a, a + n) - a;
	cout << ans << endl;
}
void solve1(){
	ull a = 0xffffffffffffffff;
	a = a / 131 - 131;
	cout << a << endl;
	while(1){
		int f = 0;
		for(ull i = 2; i <= a / i; i++){
			if(a % i == 0){
				f = 1;
				break;
			}
		}
		if(!f){
			cout << a << endl;
			return;
		}
		a--;
	}
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    solve();
    
    
    return 0;
}

P3405 [USACO16DEC] Cities and States S

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
map<pair<int, int>, int>mp;
int myhash(string s){
	int code = 0;
	for(int i = 0; i < 2; i++){
		code = code * 26 + (s[i] - 'A');
	}
	return code;
}

void solve(){
	int ans = 0, x, y;
	string s, c;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> c >> s;
		x = myhash(c);
		y = myhash(s);
		if(x == y)continue; 
		mp[{x, y}]++;
		ans += mp[{y, x}];
	}
	cout << ans << endl;
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    solve(); 
    
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值