赞
踩
认真处理特殊情况即可
前缀和+双指针
树型dp
哈希表
并查集 + 遍历
- n = int(input())
- origin = list(input())
- res = float('inf')
-
-
- class UF:
- def __init__(self, n) -> None:
- self.parent = []
- self.index = []
- for i in range(n):
- self.parent.append(i)
- self.index.append(i)
-
- def find(self, x) -> int:
- if x != self.parent[x]:
- x = self.find(self.parent[self.parent[x]])
- return x
-
- def union(self, x, y) -> None:
- px = self.find(x)
- py = self.find(y)
- if px == py: return
- self.parent[py] = px
-
-
- def solve(grid) -> int:
- n = len(grid)
- m = len(grid[0])
- uf = UF(n * m)
- ret = set()
-
- for r in range(n):
- for c in range(m):
- for x, y in [[r-1, c], [r+1, c], [r, c-1], [r, c+1]]:
- if 0 <= x < n and 0 <= y < m:
- if grid[r][c] == grid[x][y]:
- uf.union(r*m + c, x*m + y)
- for i in range(n*m):
- ret.add(uf.find(i))
-
- return len(ret)
-
-
- for r in range(1, n): # r行c列
- if n % r == 0:
- c = n // r
- grid = []
- for i in range(r):
- grid.append(origin[i*c: i*c + c])
- res = min(res, solve(grid))
-
- if n == 1:
- print(1)
- else:
- print(res)
这题暴力只能过90%的例子呜呜呜
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。