const int N = 1e5 + 10, P = 131;
typedef unsigned long long ULL;
class Solution {
public:
ULL h[N] = {0}, p[N] = {0};
ULL get(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
string longestPrefix(string s) {
int n = s.size();
p[0] = 1, h[0] = 0;
for(int i = 1; i <= n; i ++)
{
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + s[i - 1];
}
for(int i = n - 1; i; i --)
{
if(get(1, i) == get(n - i + 1, n))
return s.substr(0, i);
}
return "";
}
};
LeetCode 1392. 最长快乐前缀
最新推荐文章于 2024-08-26 22:22:22 发布