Skip to content

Commit c455794

Browse files
authored
update: 添加问题“3337.字符串转换后的长度II”的代码和题解(#936)
* 3337: WA.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * archive: before changing computer (#935) 还是先全提交到这个分支上吧,这道题还是尽量做完然后合并到master上比较好 Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - WA (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - Ok (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - WA (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - AC (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - WA (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - AC (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: test.cpp - WA (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.cpp (#935) Signed-off-by: LetMeFly666 <[email protected]> * docs: 预写题解 (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.py (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.py - AC,6.25%,71.25% (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.cpp - AC,40.84%,94.37% (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: CEjava (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: CEjava (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: CEjava (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: RE.java (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: TLE.java (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.java - AC,17.24%,82.76% (#935) F**k 调了好久 Signed-off-by: LetMeFly666 <[email protected]> * words: 昨日、前天 英语单词 (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: Init.go (#935) Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.go - AC,26.19%,95.24% (#935) 直接打一遍过喽 Signed-off-by: LetMeFly666 <[email protected]> * docs: rename (#935) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“3337.字符串转换后的长度II”的代码和题解(#936) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 8bcc85f commit c455794

12 files changed

+765
-53
lines changed

.gitlog

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-14 09:36:25
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-14 23:47:59
6+
* @Description: AC,40.84%,94.37%
7+
*/
8+
#if defined(_WIN32) || defined(__APPLE__)
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
12+
typedef long long ll;
13+
typedef array<array<ll, 26>, 26> Matrix;
14+
15+
/*
16+
1 1 1 1 ... 2
17+
18+
19+
*/
20+
21+
class Solution {
22+
private:
23+
static const int MOD = 1000000007;
24+
25+
Matrix Pow(Matrix a, int b) {
26+
Matrix ans{};
27+
for (int i = 0; i < 26; i++) {
28+
ans[i][i] = 1;
29+
}
30+
while (b) {
31+
if (b & 1) {
32+
ans = Mul(ans, a);
33+
}
34+
a = Mul(a, a);
35+
b >>= 1;
36+
}
37+
return ans;
38+
}
39+
40+
Matrix Mul(Matrix& a, Matrix& b) {
41+
Matrix ans{};
42+
for (int i = 0; i < 26; i++) {
43+
for (int j = 0; j < 26; j++) {
44+
for (int k = 0; k < 26; k++) {
45+
ans[i][k] = (ans[i][k] + a[i][j] * b[j][k] % MOD) % MOD;
46+
}
47+
}
48+
}
49+
return ans;
50+
}
51+
public:
52+
int lengthAfterTransformations(string s, int t, vector<int>& nums) {
53+
Matrix M{};
54+
for (int i = 0; i < 26; i++) {
55+
for (int j = 1; j <= nums[i]; j++) {
56+
M[i][(i + j) % 26] = 1;
57+
}
58+
}
59+
M = Pow(M, t);
60+
ll cnt[26] = {0};
61+
for (char c : s) {
62+
cnt[c - 'a']++;
63+
}
64+
int ans = 0;
65+
for (int i = 0; i < 26; i++) {
66+
for (int j = 0; j < 26; j++) {
67+
ans = (ans + M[i][j] * cnt[i] % MOD) % MOD;
68+
}
69+
}
70+
return ans;
71+
}
72+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-15 09:49:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-15 10:05:13
6+
*/
7+
package main
8+
9+
var MOD3337 = int64(1000000007)
10+
11+
type matrix3337 [26][26]int64
12+
13+
func pow(a matrix3337, b int) (ans matrix3337) {
14+
for i := 0; i < 26; i++ {
15+
ans[i][i] = 1
16+
}
17+
for ; b > 0; b >>= 1 {
18+
if b & 1 == 1 {
19+
ans = mul(ans, a)
20+
}
21+
a = mul(a, a)
22+
}
23+
return
24+
}
25+
26+
func mul(a, b matrix3337) (ans matrix3337) {
27+
for i := 0; i < 26; i++ {
28+
for k := 0; k < 26; k++ {
29+
for j := 0; j < 26; j++ {
30+
ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % MOD3337
31+
}
32+
}
33+
}
34+
return
35+
}
36+
37+
func lengthAfterTransformations(s string, t int, nums []int) int {
38+
M := matrix3337{}
39+
for i, d := range nums {
40+
for j := 1; j <= d; j++ {
41+
M[i][(i + j) % 26] = 1
42+
}
43+
}
44+
Mt := pow(M, t)
45+
times := make([]int64, 26)
46+
for i := 0; i < len(s); i++ {
47+
times[s[i] - 'a']++
48+
}
49+
ans := int64(0)
50+
for i := 0; i < 26; i++ {
51+
sum := int64(0)
52+
for j := 0; j < 26; j++ {
53+
sum += Mt[i][j]
54+
}
55+
ans = (ans + sum * times[i]) % MOD3337
56+
}
57+
return int(ans)
58+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-14 22:01:41
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-15 00:12:57
6+
*/
7+
import java.util.List;
8+
9+
class Solution {
10+
private final int MOD = 1000000007;
11+
12+
private long[][] pow(long[][] a, int b) {
13+
long[][] ans = new long[26][26];
14+
for (int i = 0; i < 26; i++) {
15+
ans[i][i] = 1;
16+
}
17+
while (b > 0) {
18+
if ((b & 1) == 1) {
19+
ans = mul(ans, a);
20+
}
21+
a = mul(a, a);
22+
b >>= 1;
23+
}
24+
return ans;
25+
}
26+
27+
private long[][] mul(long[][]a, long[][] b) {
28+
long[][] ans = new long[26][26];
29+
for (int i = 0; i < 26; i++) {
30+
for (int k = 0; k < 26; k++) {
31+
for (int j = 0; j < 26; j++) {
32+
ans[i][j] = (ans[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
33+
}
34+
}
35+
}
36+
return ans;
37+
}
38+
39+
public int lengthAfterTransformations(String s, int t, List<Integer> nums) {
40+
long[][] M = new long[26][26];
41+
for (int i = 0; i < 26; i++) {
42+
for (int j = 1; j <= nums.get(i); j++) {
43+
M[i][(i + j) % 26] = 1;
44+
}
45+
}
46+
long[][] Mt = pow(M, t);
47+
long[] cnt = new long[26];
48+
for (int i = 0; i < s.length(); i++) {
49+
cnt[s.charAt(i) - 'a']++;
50+
}
51+
long ans = 0;
52+
for (int i = 0; i < 26; i++) {
53+
long sum = 0;
54+
for (int j = 0; j < 26; j++) {
55+
sum += Mt[i][j];
56+
}
57+
sum %= MOD;
58+
ans = (ans + sum * cnt[i]) % MOD;
59+
}
60+
return (int)ans;
61+
}
62+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-14 22:01:41
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-14 23:46:10
6+
'''
7+
from typing import List
8+
9+
MOD = 1000000007
10+
11+
class Solution:
12+
def mul(self, a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
13+
ans = [[0] * 26 for _ in range(26)]
14+
for i in range(26):
15+
for k in range(26):
16+
for j in range(26):
17+
ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % MOD
18+
return ans
19+
20+
def pow(self, a: List[List[int]], b: int) -> List[List[int]]:
21+
ans = [[0] * 26 for _ in range(26)]
22+
for i in range(26):
23+
ans[i][i] = 1
24+
while b:
25+
if b & 1:
26+
ans = self.mul(ans, a)
27+
a = self.mul(a, a)
28+
b >>= 1
29+
return ans
30+
31+
def lengthAfterTransformations(self, s: str, t: int, nums: List[int]) -> int:
32+
M = [[0] * 26 for _ in range(26)]
33+
for i, v in enumerate(nums):
34+
for j in range(1, v + 1):
35+
M[i][(i + j) % 26] = 1
36+
Mt = self.pow(M, t)
37+
cnt = [0] * 26
38+
for c in s:
39+
cnt[ord(c) - ord('a')] += 1
40+
ans = 0
41+
for i in range(26):
42+
ans += sum(Mt[i]) * cnt[i]
43+
return ans % MOD
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-14 22:29:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-14 22:46:21
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
// #include "3337-total-characters-in-string-after-transformations-ii.cpp
10+
#endif
11+
12+
typedef long long ll;
13+
14+
// Copy And Change From 0x3f的题解
15+
class Solution {
16+
static constexpr int MOD = 1'000'000'007;
17+
static constexpr int SIZE = 26;
18+
19+
using Matrix = array<array<ll, SIZE>, SIZE>;
20+
21+
// 返回矩阵 a 和矩阵 b 相乘的结果
22+
Matrix mul(Matrix& a, Matrix& b) {
23+
Matrix c{};
24+
for (int i = 0; i < SIZE; i++) {
25+
for (int k = 0; k < SIZE; k++) {
26+
if (a[i][k] == 0) {
27+
continue;
28+
}
29+
for (int j = 0; j < SIZE; j++) {
30+
c[i][j] = (c[i][j] + (long long) a[i][k] * b[k][j]) % MOD;
31+
}
32+
}
33+
}
34+
return c;
35+
}
36+
37+
// 返回 n 个矩阵 a 相乘的结果
38+
Matrix Pow(Matrix a, int n) {
39+
Matrix res = {};
40+
for (int i = 0; i < SIZE; i++) {
41+
res[i][i] = 1; // 单位矩阵
42+
}
43+
while (n) {
44+
if (n & 1) {
45+
res = mul(res, a);
46+
}
47+
a = mul(a, a);
48+
n >>= 1;
49+
}
50+
return res;
51+
}
52+
53+
public:
54+
int lengthAfterTransformations(string s, int t, vector<int>& nums) {
55+
Matrix M{};
56+
for (int i = 0; i < SIZE; i++) {
57+
for (int j = i + 1; j <= i + nums[i]; j++) {
58+
M[i][j % SIZE] = 1;
59+
}
60+
}
61+
M = Pow(M, t);
62+
63+
int cnt[SIZE]{};
64+
for (char c : s) {
65+
cnt[c - 'a']++;
66+
}
67+
68+
int ans = 0;
69+
for (int i = 0; i < 26; i++) {
70+
for (int j = 0; j < 26; j++) {
71+
ans = (ans + M[i][j] * cnt[i] % MOD) % MOD;
72+
}
73+
}
74+
return ans;
75+
}
76+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@
964964
|3305.元音辅音字符串计数I|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/12/LeetCode%203305.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146197747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/solutions/3607571/letmefly-3305yuan-yin-fu-yin-zi-fu-chuan-ptkg/" target="_blank">LeetCode题解</a>|
965965
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
966966
|3335.字符串转换后的长度I|中等|<a href="https://leetcode.cn/problems/total-characters-in-string-after-transformations-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/13/LeetCode%203335.%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E5%90%8E%E7%9A%84%E9%95%BF%E5%BA%A6I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147916305" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/total-characters-in-string-after-transformations-i/solutions/3675555/letmefly-3335zi-fu-chuan-zhuan-huan-hou-9ck9y/" target="_blank">LeetCode题解</a>|
967+
|3337.字符串转换后的长度II|困难|<a href="https://leetcode.cn/problems/total-characters-in-string-after-transformations-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/15/LeetCode%203337.%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E5%90%8E%E7%9A%84%E9%95%BF%E5%BA%A6II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147976391" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/total-characters-in-string-after-transformations-ii/solutions/3677448/letmefly-3337zi-fu-chuan-zhuan-huan-hou-h56wo/" target="_blank">LeetCode题解</a>|
967968
|3340.检查平衡字符串|简单|<a href="https://leetcode.cn/problems/check-balanced-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146249653" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-balanced-string/solutions/3610899/letmefly-3340jian-cha-ping-heng-zi-fu-ch-p8eo/" target="_blank">LeetCode题解</a>|
968969
|3341.到达最后一个房间的最少时间I|中等|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/07/LeetCode%203341.%E5%88%B0%E8%BE%BE%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E6%88%BF%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147807022" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/solutions/3672076/letmefly-3341dao-da-zui-hou-yi-ge-fang-j-3m6o/" target="_blank">LeetCode题解</a>|
969970
|3342.到达最后一个房间的最少时间II|中等|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/09/LeetCode%203342.%E5%88%B0%E8%BE%BE%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E6%88%BF%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147835524" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-ii/solutions/3672695/letmefly-3342dao-da-zui-hou-yi-ge-fang-j-qrh6/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 3335.字符串转换后的长度I.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ categories: [题解, LeetCode]
103103
>
104104
> 接着从`y``a`遍历,将cnt[i]赋值给cnt[i + 1]
105105
>
106-
> 最后令`cnt[0] := z``cnt[1] += z``ans += z`这是因为没轮操作`z`会变成`ab`,同时字符串长度会加一)
106+
> 最后令`cnt[0] := z``cnt[1] += z``ans += z`这是因为每轮操作`z`会变成`ab`,同时字符串长度会加一)
107107
108108
+ 时间复杂度$O(len(s)\cdot C)$,其中$C=26$
109109
+ 空间复杂度$O(C)$

0 commit comments

Comments
 (0)