acwing: https://www.acwing.com/problem/content/843/

包含大小写英文字母和数字的字符串中,问某两段区间的字符串是不是相同的

#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
const int N = 1e5 + 10, P = 131;
int n, m;
char s[N];
ULL h[N], p[N];
ULL get(int l, int r){
	return h[r] - h[l - 1] * p[r - l + 1];
}
int main(){
	cin >> n >> m >> (s + 1);
	p[0] = 1;
	for (int i = 1; i <= n; i ++ ){
		p[i] = p[i - 1] * P;
		h[i] = h[i - 1] * P + s[i];
	}
	for (int i = 1; i <= m; i ++ ){
		int l1, r1, l2, r2;
		cin >> l1 >> r1 >> l2 >> r2;
		if ( get(l1, r1) == get(l2, r2) ) cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

luogu: https://www.luogu.com.cn/problem/P3370

给定 \(n\) 个字符串,输出不同的字符串的个数。

#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
const int N = 1e4 + 10, P = 131;
const ULL mod = 212370440130137957ll;//质数 
int n;
string s;
vector <ULL> a;
ULL get(){
	ULL ans = 1;
	for (int i = 0; i < s.size(); i ++ )
		ans = (ans * P + s[i]) % mod;
	return ans;
}
int main(){
	cin >> n;
	for (int i = 1; i <= n; i ++ ){
		cin >> s;
		a.push_back( get() );
	}
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	cout << a.size() << "\n";
	return 0;
}
posted on 2022-04-25 12:25  Hamine  阅读(118)  评论(0编辑  收藏  举报