Skip to content

feat: add solutions to lc problem: No.3337 #4406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:矩阵快速幂加速递推

我们定义 $f[i][j]$ 表示经过 $i$ 次转换后,字母表中第 $j$ 个字母的个数。初始时 $f[0][j]$ 为字符串 $s$ 中字母表中第 $j$ 个字母的个数。

由于每一次转换后第 $j$ 个字母的个数,都跟下一次转换有关,转换的次数 $t$ 较大,我们可以使用矩阵快速幂,来加速整个递推过程。

注意,答案可能非常大,我们需要对 $10^9 + 7$ 取模。

时间复杂度 $O(n + \log t \times |\Sigma|^3)$,空间复杂度 $O(|\Sigma|^2)$。其中 $|\Sigma|$ 为字母表的大小。

<!-- tabs:start -->

Expand Down Expand Up @@ -251,13 +259,195 @@ class Solution {
#### C++

```cpp
class Solution {
public:
static constexpr int MOD = 1e9 + 7;
static constexpr int M = 26;

using Matrix = vector<vector<int>>;

Matrix matmul(const Matrix& a, const Matrix& b) {
int n = a.size(), p = b.size(), q = b[0].size();
Matrix res(n, vector<int>(q, 0));
for (int i = 0; i < n; ++i) {
for (int k = 0; k < p; ++k) {
if (a[i][k]) {
for (int j = 0; j < q; ++j) {
res[i][j] = (res[i][j] + 1LL * a[i][k] * b[k][j] % MOD) % MOD;
}
}
}
}
return res;
}

Matrix matpow(Matrix mat, int power) {
Matrix res(M, vector<int>(M, 0));
for (int i = 0; i < M; ++i) res[i][i] = 1;
while (power) {
if (power % 2) res = matmul(res, mat);
mat = matmul(mat, mat);
power /= 2;
}
return res;
}

int lengthAfterTransformations(string s, int t, vector<int>& nums) {
vector<int> cnt(M, 0);
for (char c : s) {
cnt[c - 'a']++;
}

Matrix matrix(M, vector<int>(M, 0));
for (int i = 0; i < M; ++i) {
for (int j = 1; j <= nums[i]; ++j) {
matrix[i][(i + j) % M] = 1;
}
}

Matrix cntMat(1, vector<int>(M));
for (int i = 0; i < M; ++i) cntMat[0][i] = cnt[i];

Matrix factor = matpow(matrix, t);
Matrix result = matmul(cntMat, factor);

int ans = 0;
for (int x : result[0]) {
ans = (ans + x) % MOD;
}

return ans;
}
};
```

#### Go

```go
func lengthAfterTransformations(s string, t int, nums []int) int {
const MOD = 1e9 + 7
const M = 26

cnt := make([]int, M)
for _, c := range s {
cnt[int(c-'a')]++
}

matrix := make([][]int, M)
for i := 0; i < M; i++ {
matrix[i] = make([]int, M)
for j := 1; j <= nums[i]; j++ {
matrix[i][(i+j)%M] = 1
}
}

matmul := func(a, b [][]int) [][]int {
n, p, q := len(a), len(b), len(b[0])
res := make([][]int, n)
for i := 0; i < n; i++ {
res[i] = make([]int, q)
for k := 0; k < p; k++ {
if a[i][k] != 0 {
for j := 0; j < q; j++ {
res[i][j] = (res[i][j] + a[i][k]*b[k][j]%MOD) % MOD
}
}
}
}
return res
}

matpow := func(mat [][]int, power int) [][]int {
res := make([][]int, M)
for i := 0; i < M; i++ {
res[i] = make([]int, M)
res[i][i] = 1
}
for power > 0 {
if power%2 == 1 {
res = matmul(res, mat)
}
mat = matmul(mat, mat)
power /= 2
}
return res
}

cntMat := [][]int{make([]int, M)}
copy(cntMat[0], cnt)

factor := matpow(matrix, t)
result := matmul(cntMat, factor)

ans := 0
for _, v := range result[0] {
ans = (ans + v) % MOD
}
return ans
}
```

#### TypeScript

```ts
function lengthAfterTransformations(s: string, t: number, nums: number[]): number {
const MOD = BigInt(1e9 + 7);
const M = 26;

const cnt: number[] = Array(M).fill(0);
for (const c of s) {
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
}

const matrix: number[][] = Array.from({ length: M }, () => Array(M).fill(0));
for (let i = 0; i < M; i++) {
for (let j = 1; j <= nums[i]; j++) {
matrix[i][(i + j) % M] = 1;
}
}

const matmul = (a: number[][], b: number[][]): number[][] => {
const n = a.length,
p = b.length,
q = b[0].length;
const res: number[][] = Array.from({ length: n }, () => Array(q).fill(0));
for (let i = 0; i < n; i++) {
for (let k = 0; k < p; k++) {
const aik = BigInt(a[i][k]);
if (aik !== BigInt(0)) {
for (let j = 0; j < q; j++) {
const product = aik * BigInt(b[k][j]);
const sum = BigInt(res[i][j]) + product;
res[i][j] = Number(sum % MOD);
}
}
}
}
return res;
};

const matpow = (mat: number[][], power: number): number[][] => {
let res: number[][] = Array.from({ length: M }, (_, i) =>
Array.from({ length: M }, (_, j) => (i === j ? 1 : 0)),
);
while (power > 0) {
if (power % 2 === 1) res = matmul(res, mat);
mat = matmul(mat, mat);
power = Math.floor(power / 2);
}
return res;
};

const cntMat: number[][] = [cnt.slice()];
const factor = matpow(matrix, t);
const result = matmul(cntMat, factor);

let ans = 0n;
for (const v of result[0]) {
ans = (ans + BigInt(v)) % MOD;
}
return Number(ans);
}
```

<!-- tabs:end -->
Expand Down
Loading