33 * Project : leetcode-cpp
44 * Author : Wei Tan <tanwei.winterreise@gmail.com>
55 * Date : 2026-02-12 17:15:14
6- * Last Modified Date: 2026-02-12 18:58:37
6+ * Last Modified Date: 2026-02-23 10:19:48
77 * Last Modified By : Wei Tan <tanwei.winterreise@gmail.com>
88 */
99
1414#include < iomanip>
1515#include < iostream>
1616#include < regex>
17+ #include < set>
1718#include < sstream>
1819#include < stdexcept>
1920#include < string>
21+ #include < utility>
2022#include < vector>
2123
2224// This project headers
@@ -178,6 +180,81 @@ static std::string parse_extra_use(const std::string& code) {
178180 return extra;
179181}
180182
183+ static std::string parse_std_headers (const std::string& code) {
184+ std::vector<std::pair<std::string, std::string>> mapping = {
185+ {" vector" , " vector" },
186+ {" string" , " string" },
187+ {" map" , " map" },
188+ {" unordered_map" , " unordered_map" },
189+ {" set" , " set" },
190+ {" unordered_set" , " unordered_set" },
191+ {" tuple" , " tuple" },
192+ {" pair" , " utility" },
193+ {" queue" , " queue" },
194+ {" stack" , " stack" },
195+ {" list" , " list" },
196+ {" deque" , " deque" },
197+ {" array" , " array" },
198+ {" bitset" , " bitset" },
199+ {" sort" , " algorithm" },
200+ {" lower_bound" , " algorithm" },
201+ {" upper_bound" , " algorithm" },
202+ {" binary_search" , " algorithm" },
203+ {" next_permutation" , " algorithm" },
204+ {" reverse" , " algorithm" },
205+ {" unique" , " algorithm" },
206+ {" cout" , " iostream" },
207+ {" cin" , " iostream" },
208+ {" cerr" , " iostream" },
209+ {" stringstream" , " sstream" },
210+ {" istringstream" , " sstream" },
211+ {" ostringstream" , " sstream" },
212+ {" setw" , " iomanip" },
213+ {" setfill" , " iomanip" },
214+ {" setprecision" , " iomanip" },
215+ {" function" , " functional" },
216+ {" shared_ptr" , " memory" },
217+ {" unique_ptr" , " memory" },
218+ {" make_shared" , " memory" },
219+ {" accumulate" , " numeric" },
220+ {" iota" , " numeric" },
221+ {" numeric_limits" , " limits" },
222+ {" pow" , " cmath" },
223+ {" sqrt" , " cmath" },
224+ {" memset" , " cstring" },
225+ {" memcpy" , " cstring" },
226+ {" int64_t" , " cstdint" },
227+ {" uint64_t" , " cstdint" },
228+ {" size_t" , " cstddef" },
229+ {" regex" , " regex" },
230+ {" smatch" , " regex" },
231+ {" regex_search" , " regex" },
232+ {" enable_if" , " type_traits" },
233+ {" is_same" , " type_traits" }};
234+
235+ std::set<std::string> headers;
236+ for (auto & pr : mapping) {
237+ try {
238+ std::regex r (" \\ b" + pr.first + " \\ b" );
239+ if (std::regex_search (code, r)) {
240+ headers.insert (pr.second );
241+ }
242+ } catch (...) {
243+ // ignore regex errors for safety
244+ }
245+ }
246+
247+ if (headers.empty ()) {
248+ headers.insert (" iostream" );
249+ }
250+
251+ std::ostringstream oss;
252+ for (const auto & header : headers) {
253+ oss << " #include <" << header << " >\n " ;
254+ }
255+ return oss.str ();
256+ }
257+
181258static std::string build_desc (const std::string& content) {
182259 std::string s = content;
183260 std::vector<std::pair<std::string, std::string>> reps = {
@@ -212,6 +289,13 @@ static std::string build_desc(const std::string& content) {
212289 s.replace (pos, needle.size (), repl);
213290 pos += repl.size ();
214291 }
292+ // remove <font ...> and </font> tags that may appear in descriptions
293+ try {
294+ s = std::regex_replace (s, std::regex (" <font[^>]*>" ), std::string ());
295+ s = std::regex_replace (s, std::regex (" </font>" ), std::string ());
296+ } catch (...) {
297+ // ignore regex errors
298+ }
215299 return s;
216300}
217301
@@ -239,6 +323,7 @@ static void deal_solving(int id) {
239323 throw std::runtime_error (" problem file not found" );
240324 }
241325 std::string base = found.filename ().string ();
326+ std::string orig_base = base;
242327 if (base.size () > 4 && base.substr (base.size () - 4 ) == " .cpp" ) {
243328 base.resize (base.size () - 4 );
244329 }
@@ -251,11 +336,30 @@ static void deal_solving(int id) {
251336 throw std::runtime_error (" solution already exists" );
252337 }
253338 fs::rename (found, solution_path);
339+ // Update the File header inside the moved file: replace orig filename with
340+ // new one
341+ try {
342+ std::ifstream in (solution_path);
343+ if (in) {
344+ std::ostringstream ss;
345+ ss << in.rdbuf ();
346+ std::string content = ss.str ();
347+ std::string orig_name = orig_base;
348+ size_t pos = content.find (orig_name);
349+ if (pos != std::string::npos) {
350+ std::string new_name = base + " .cpp" ;
351+ content.replace (pos, orig_name.length (), new_name);
352+ std::ofstream out (solution_path);
353+ out << content;
354+ }
355+ }
356+ } catch (...) {
357+ // ignore errors updating header
358+ }
254359}
255360
256361static void deal_problem (const fetcher::Problem& problem,
257- const fetcher::CodeDefinition& code,
258- bool write_mod_file) {
362+ const fetcher::CodeDefinition& code) {
259363 std::ostringstream fn;
260364 fn << " p" << std::setw (4 ) << std::setfill (' 0' ) << problem.question_id
261365 << " _" ;
@@ -305,8 +409,11 @@ static void deal_problem(const fetcher::Problem& problem,
305409 std::ostringstream idbuf;
306410 idbuf << std::setw (4 ) << std::setfill (' 0' ) << problem.question_id ;
307411 replace_all (tpl, " __PROBLEM_ID__" , idbuf.str ());
412+ // Replace file name placeholder with actual file name (pXXXX_slug.cpp)
413+ replace_all (tpl, " __FILE_NAME__" , file_name + " .cpp" );
308414 }
309415 replace_all (tpl, " __EXTRA_USE__" , parse_extra_use (default_code));
416+ replace_all (tpl, " __STL_INCLUDES__" , parse_std_headers (default_code));
310417 replace_all (tpl, " __PROBLEM_LINK__" ,
311418 std::string (" https://leetcode.com/problems/" ) +
312419 problem.title_slug +
@@ -323,8 +430,6 @@ static void deal_problem(const fetcher::Problem& problem,
323430 std::ofstream out (file_path);
324431 out << tpl;
325432 out.close ();
326-
327- (void )write_mod_file; // mod file generation intentionally disabled
328433}
329434
330435int main () {
@@ -377,7 +482,7 @@ int main() {
377482 continue ;
378483 }
379484 try {
380- deal_problem (*prob, prob->code_definition [0 ], true );
485+ deal_problem (*prob, prob->code_definition [0 ]);
381486 } catch (const std::exception& e) {
382487 std::cerr << " Error initializing problem " << fid << " : "
383488 << e.what () << " \n " ;
@@ -395,7 +500,7 @@ int main() {
395500 problem->code_definition .empty () ? fetcher::CodeDefinition{}
396501 : problem->code_definition [0 ];
397502 try {
398- deal_problem (*problem, cd, true );
503+ deal_problem (*problem, cd);
399504 } catch (const std::exception& e) {
400505 std::cerr << " Error: " << e.what () << " \n " ;
401506 return 1 ;
0 commit comments