classSolution: defthreeEqualParts(self, arr: List[int]) -> List[int]: c = sum(arr) if c == 0: return [0,len(arr) - 1] if c % 3: return [-1,-1] MOD = 10**13 + 7 P = 131 n = len(arr) h = (n+1) * [0] p = (n+1) * [0] p[0] = 1
for i inrange(1,n + 1): h[i] = (h[i-1] * P % MOD + arr[i - 1] ) % MOD p[i] = p[i-1] * P % MOD
defget(l,r): #print(l,r) return (h[r] - h[l - 1] * p[r - l + 1] %MOD) % MOD d = defaultdict(int) # 先算O(n) 算出所有 1~l 的值 # 然后O(n) 枚举 r 比较结果得出答案 for l inrange(1,n+1): #print("-----") d[get(1,l)] = l for r inrange(n,0,-1): #print("xxxx") if get(r,n) in d: l = d[get(r,n)] if get(l+1,r-1) == get(r,n): return [l-1,r-1] return [-1,-1]
funcmax(a int,b int)int{ if a > b{ return a } return b }
funcdeleteString(s string)int { n := len(s) p := make([]ULL, n+1) h := make([]ULL, n+1) p[0] = 1 var P ULL = 131 for i := 1; i <= n; i++ { h[i] = h[i-1]*P + ULL(s[i-1]) p[i] = p[i-1] * P }
f := make([]int, n+1) get := func(l int,r int) ULL { return h[r] - h[l-1]*p[r-l+1] }
for i := n; i >= 1; i-- { f[i] = 1 for j := 1; j <= (n-i + 1)/2 ; j++ { if get(i,i+j-1) == get(i+j,i+2*j-1){ f[i] = max(f[i],f[i+j] + 1) } } }