Give me the most optimised c++ code with passing a...

Создано: 25 октября 2026 г.

Отвечено с использованием GPT-5.6 Thinking от Chat01

Вопрос

IMG-20261025-WA0004.jpg
IMG-20261025-WA0005.jpg
IMG-20261025-WA0006(1).jpg
IMG-20261025-WA0003.jpg

Give me the most optimised c++ code with passing all the test cases and simple and short

Мысль

Parsing email rules

I’m identifying each name’s first and last parts, removing hyphens from surnames, truncating them to eight characters, and normalizing everything to lowercase. I’m also tracking duplicate email bases so numbering starts at 2 while preserving original order and spelling.

Parsed email rules

Worked for 6s

Ответ

This is O(|S|) time and handles duplicates, hyphens, case conversion, and 8-character last-name truncation.

cpp
#include <string> #include <unordered_map> #include <algorithm> #include <cctype> using namespace std; string solution(string &S, string &C) { unordered_map<string, int> cnt; string ans; transform(C.begin(), C.end(), C.begin(), ::tolower); size_t pos = 0; while (pos < S.size()) { size_t end = S.find("; ", pos); string name = S.substr(pos, end == string::npos ? string::npos : end - pos); string first = name.substr(0, name.find(' ')); string last = name.substr(name.rfind(' ') + 1); last.erase(remove(last.begin(), last.end(), '-'), last.end()); if (last.size() > 8) last.resize(8); transform(first.begin(), first.end(), first.begin(), ::tolower); transform(last.begin(), last.end(), last.begin(), ::tolower); string base = first + "." + last; int n = ++cnt[base]; string email = base + (n > 1 ? to_string(n) : "") + "@" + C + ".com"; if (!ans.empty()) ans += "; "; ans += name + " <" + email + ">"; if (end == string::npos) break; pos = end + 2; } return ans; }

For the screenshot example, it correctly produces [email protected], then [email protected], [email protected], and watson-parker becomes watsonpa after removing the hyphen before truncating.

Вопрос

IMG-20261025-WA0007.jpg

Got this error give me the correct most optimised code in c++

Мысль

Worked for 8s

Ответ

The errors in the screenshot are from a few typing mistakes like size_tpos instead of size_t pos, '' instead of ' ', and misspelled string::npos.

Copy this exact code:

cpp
#include <string> #include <unordered_map> #include <algorithm> #include <cctype> using namespace std; string solution(string &S, string &C) { unordered_map<string, int> count; string result; string company = C; transform(company.begin(), company.end(), company.begin(), [](unsigned char c) { return tolower(c); }); size_t pos = 0; while (pos < S.size()) { size_t end = S.find("; ", pos); string name = S.substr( pos, end == string::npos ? string::npos : end - pos ); string first = name.substr(0, name.find(' ')); string last = name.substr(name.rfind(' ') + 1); last.erase(remove(last.begin(), last.end(), '-'), last.end()); if (last.size() > 8) last.resize(8); transform(first.begin(), first.end(), first.begin(), [](unsigned char c) { return tolower(c); }); transform(last.begin(), last.end(), last.begin(), [](unsigned char c) { return tolower(c); }); string base = first + "." + last; int n = ++count[base]; string email = base; if (n > 1) email += to_string(n); email += "@" + company + ".com"; if (!result.empty()) result += "; "; result += name + " <" + email + ">"; if (end == string::npos) break; pos = end + 2; } return result; }

Important

Make sure these characters are typed exactly:

cpp
name.find(' ') name.rfind(' ') string::npos size_t pos = 0; size_t end = S.find("; ", pos);

For the example, it correctly generates:

Complexity is O(N), where N is the length of the input string.

Поделиться этим Q&A