Hack me
查看原帖
Hack me
784650
__My0217__楼主2024/5/15 12:59

写的哈希但是感觉有问题 不排除rz错误 WA

#include<iostream>
#include<string>
#include<cstring>
#define p 1000007
using namespace std;
unsigned long long hsh[p], _pow[p];
void get_hash(string s){
	int cur = 0;
	for(auto i : s) hsh[++cur] = hsh[cur-1]*p+i;
}
unsigned long long _hash(int l, int r){
	return (unsigned long long)(hsh[r]-hsh[l-1]*_pow[r-l+1]);
}
int main(){
	int n;
	cin >> n;
	_pow[0] = 1; 
	for(int i = 1 ; i < p ; i++) _pow[i] = _pow[i-1] * p;
	while(n--){
		string s;
		cin >> s;
		get_hash(s);
		bool flag = 1;
		for(int i = 1 ; i <= s.length()/2 ; i++){
			int h1 = _hash(1, s.length()-i), h2 = _hash(i+1, s.length());
			//cout << '|' << h1 << ' ' << h2 << "|\n";  
			if(h1 == h2) {
				cout << i << (!n ? "" : "\n\n");
				flag = 0;
				break;
			}
		}
		if(flag) cout << s.length() << (!n ? "" : "\n\n");
	}
	return 0;
}
2024/5/15 12:59
加载中...