Skip to content

Commit 85efcf7

Browse files
authored
update: 添加问题“3474.字典序最小的生成字符串”的代码和题解 (#1477)
* 3474: half.cpp (#1475) + clean + docs(mac de qrcode) * 3474: WA.cpp (#1475) input: TFTF ab output: "" should: ababa * 3474: WA.cpp (#1475) - 刚刚是因为ans初始化,应该先size后char input: FT aghbdfhf output: baghbdfhf should: aaghbdfhf * 3474: WA.cpp (#1475) - 刚刚是因为有可能本来就不需要改,但all_a还是判为true了 input: big output: big should: big * 3474: AC.cpp (#1475) - AC,15.00%,100.00% 刚刚是因为修改为b后,b的位置就不能再修改为a了,要同步can_change为false * docs: 题解(half) - 2026 年 3 月 31 日,23:42:43 2026‎年‎3‎月‎31‎日 23:42:43 * update: 添加问题“3474.字典序最小的生成字符串”的代码和题解 (#1477) 2840: AC.cpp (#1473) cpp - string - AC,26.79%,66.07% cpp - hash - AC,98.21%,100.00% Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 7da6398 commit 85efcf7

11 files changed

Lines changed: 509 additions & 104 deletions
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-31 21:55:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-31 23:21:23
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
/*
11+
1 3 5 7不一样
12+
idx[7]++
13+
14+
str1: F
15+
str2: bxxx
16+
ans: aaaa
17+
只要前面有不一样的,后面则能修改的全a就好
18+
当然后面任意都行,如果后面的F需要修改某个a为其他完全ok
19+
20+
str1: F
21+
str2: abxx
22+
ans1: aaaa 第一个字符还a,但后面至少有个不一样的
23+
ans2: baaa 第一个字符就不一样,后面全a就好(当然也能再改)
24+
如果能构造ans1,一定比ans2更优,因为ans1后面字符的不同于str2会导致后面F更容易
25+
26+
str1: F
27+
str2: aabx
28+
ans: aaaa
29+
30+
str1: F
31+
str2: aaaa
32+
ans: aaab
33+
*/
34+
class Solution {
35+
private:
36+
int n, m;
37+
vector<bool> can_change;
38+
39+
bool fillT(string& ans, int idx, const string& s) {
40+
for (int i = 0; i < m; i++) {
41+
if (ans[i + idx] == '-') {
42+
ans[i + idx] = s[i];
43+
can_change[i + idx] = false;
44+
} else {
45+
if (ans[i + idx] != s[i]) {
46+
return false;
47+
}
48+
}
49+
}
50+
return true;
51+
}
52+
53+
bool fillF(string& ans, int idx, const string& s) {
54+
if (all_cannot_change_and_all_same(ans, idx, s)) {
55+
return false;
56+
}
57+
58+
// 以下逻辑一定能设置成功ans.sub(idx)
59+
60+
if (can_changed_place_are_all_a(ans, idx, s)) {
61+
// 要改且能改位置全是a,挑最后一个能改位置改为b
62+
change_last2b(ans, idx, s);
63+
} else {
64+
// 可设置为全a,这样就满足F
65+
set_all_a(ans, idx, s);
66+
}
67+
return true;
68+
}
69+
70+
bool all_cannot_change_and_all_same(string& ans, int idx, const string& s) {
71+
for (int i = 0; i < m; i++) {
72+
if (can_change[i + idx] || ans[i + idx] != s[i]) {
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
79+
// 可以修改的位置对应str2全部是a
80+
bool can_changed_place_are_all_a(string& ans, int idx, const string& s) {
81+
for (int i = 0; i < m; i++) {
82+
if (can_change[i + idx] && s[i] != 'a') {
83+
return false;
84+
} else if (!can_change[i + idx] && ans[i + idx] != s[i]) {
85+
return false;
86+
}
87+
}
88+
return true;
89+
}
90+
91+
void change_last2b(string& ans, int idx, const string& s) {
92+
bool is_last = true;
93+
for (int i = m - 1; i >= 0; i--) {
94+
if (can_change[i + idx]) {
95+
if (is_last) {
96+
ans[i + idx] = 'b';
97+
is_last = false;
98+
can_change[i + idx] = false;
99+
} else {
100+
ans[i + idx] = 'a';
101+
}
102+
}
103+
}
104+
}
105+
106+
void set_all_a(string& ans, int idx, const string& s) {
107+
for (int i = 0; i < m; i++) {
108+
if (can_change[i + idx]) {
109+
ans[i + idx] = 'a';
110+
}
111+
}
112+
}
113+
public:
114+
string generateString(const string& str1, const string& str2) {
115+
n = str1.size();
116+
m = str2.size();
117+
string ans(n + m - 1, '-');
118+
can_change = move(vector<bool>(n + m - 1, true));
119+
120+
for (int i = 0; i < str1.size(); i++) {
121+
if (str1[i] == 'T') {
122+
if (!fillT(ans, i, str2)) {
123+
return "";
124+
}
125+
}
126+
}
127+
128+
for (int i = 0; i < str1.size(); i++) {
129+
if (str1[i] == 'F') {
130+
if (!fillF(ans, i, str2)) {
131+
return "";
132+
}
133+
}
134+
}
135+
136+
return ans;
137+
}
138+
};
139+
140+
#ifdef _DEBUG
141+
/*
142+
TFTF
143+
ab
144+
145+
ababa
146+
*/
147+
/*
148+
FT
149+
aghbdfhf
150+
151+
aaghbdfhf
152+
*/
153+
int main() {
154+
string a, b;
155+
while (cin >> a >> b) {
156+
Solution sol;
157+
cout << sol.generateString(a, b) << endl;
158+
}
159+
return 0;
160+
}
161+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@
11221122
|3453.分割正方形I|中等|<a href="https://leetcode.cn/problems/separate-squares-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/13/LeetCode%203453.%E5%88%86%E5%89%B2%E6%AD%A3%E6%96%B9%E5%BD%A2I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156915633" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/separate-squares-i/solutions/3880005/letmefly-3453fen-ge-zheng-fang-xing-ier-vem4p/" target="_blank">LeetCode题解</a>|
11231123
|3459.最长V形对角线段的长度|困难|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/31/LeetCode%203459.%E6%9C%80%E9%95%BFV%E5%BD%A2%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%AE%B5%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151050078" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/solutions/3768545/letmefly-3459zui-chang-v-xing-dui-jiao-x-gu8q/" target="_blank">LeetCode题解</a>|
11241124
|3461.判断操作后字符串中的数字是否相等I|简单|<a href="https://leetcode.cn/problems/check-if-digits-are-equal-in-string-after-operations-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/23/LeetCode%203461.%E5%88%A4%E6%96%AD%E6%93%8D%E4%BD%9C%E5%90%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E7%9B%B8%E7%AD%89I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/153800840" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-digits-are-equal-in-string-after-operations-i/solutions/3814120/letmefly-3461pan-duan-cao-zuo-hou-zi-fu-8nd1t/" target="_blank">LeetCode题解</a>|
1125+
|3474.字典序最小的生成字符串|困难|<a href="https://leetcode.cn/problems/lexicographically-smallest-generated-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/31/LeetCode%203474.%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E7%94%9F%E6%88%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159736815" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-generated-string/solutions/3942826/letmefly-3474zi-dian-xu-zui-xiao-de-shen-hlj5/" target="_blank">LeetCode题解</a>|
11251126
|3494.酿造药水需要的最少总时间|中等|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/09/LeetCode%203494.%E9%85%BF%E9%80%A0%E8%8D%AF%E6%B0%B4%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E6%80%BB%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/152847907" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/solutions/3801656/letmefly-3494niang-zao-yao-shui-xu-yao-d-bjxv/" target="_blank">LeetCode题解</a>|
11261127
|3507.移除最小数对使数组有序I|简单|<a href="https://leetcode.cn/problems/minimum-pair-removal-to-sort-array-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/22/LeetCode%203507.%E7%A7%BB%E9%99%A4%E6%9C%80%E5%B0%8F%E6%95%B0%E5%AF%B9%E4%BD%BF%E6%95%B0%E7%BB%84%E6%9C%89%E5%BA%8FI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157263848" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-pair-removal-to-sort-array-i/solutions/3887006/letmefly-3507yi-chu-zui-xiao-shu-dui-shi-t5h5/" target="_blank">LeetCode题解</a>|
11271128
|3508.设计路由器|中等|<a href="https://leetcode.cn/problems/implement-router/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/20/LeetCode%203508.%E8%AE%BE%E8%AE%A1%E8%B7%AF%E7%94%B1%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151901838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/implement-router/solutions/3787451/letmefly-3508she-ji-lu-you-qi-stltao-stl-ehkf/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)