关于字符串hash的一个题目,有点不懂
  • 板块学术版
  • 楼主Xiongzx
  • 当前回复20
  • 已保存回复20
  • 发布时间2023/3/28 23:05
  • 上次更新2023/10/23 20:10:08
查看原帖
关于字符串hash的一个题目,有点不懂
956071
Xiongzx楼主2023/3/28 23:05

题面:找到一个字符串中的最大回文串,有多组数据 我的问题为什么字符串都hash了哈可以通过进制来计算两个串是否相同

#include <bits/stdc++.h>

#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define pre(i, a, b) for(int i = (a); i >= (b); i--)
#define Ede(i, u) for(int i = h[u]; i; i = ne[i])
#define go(i, a) for(auto i : a)
//#define int long long
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define pb push_back	
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
const int inf = 0x3f3f3f3f;

template <typename T> inline void rd(T &x){
	x = 0; bool f = true; char ch = getchar();
	while(ch < '0' || ch > '9'){ if(ch == '-') f = false; ch = getchar();}
	while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
	if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}

using namespace std;

const int N = 2e6 + 10, base = 131;
char str[N];
ULL hl[N], hr[N], p[N];
ULL get(ULL h[], int l, int r){
	return h[r] - h[l - 1] * p[r - l + 1];
} 

int main(){
	/*
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
	*/
	int T = 0;
	while(sf("%s", str + 1), strcmp(str + 1, "END")){
		int n = strlen(str + 1) * 2;
		for(int i = n; i; i -= 2){
			str[i] = str[i / 2];
			str[i - 1] = 'a' + 26;
		}
		p[0] = 1;
		for(int i = 1, j = n; i <= n; i++, j--){
			hl[i] = hl[i - 1] * base + (str[i] - 'a' + 1);
			hr[i] = hr[i - 1] * base + (str[j] - 'a' + 1);		
			p[i] = p[i - 1] * base;
 		}
 		int res = 0;
 		rep(i, 1, n){
			int l = 0, r = min(i - 1, n - i);
			while(l < r){
				int mid = (l + r + 1) >> 1;
				if(get(hl, i - mid, i - 1) == get(hr, n - (i + mid) + 1, n - (i + 1) + 1)) l = mid;
				else r = mid - 1;
			}
			if(str[i - l] <= 'z') res = max(res, l + 1);
			else res = max(res, l); 
		}
		prf("Case %d: %d\n", ++T , res);
	}
	
	
	return 0;
}





2023/3/28 23:05
加载中...