Optimal approach: Identify the best algorithm base...
생성일: 2026년 10월 25일
답변에 사용된 모델 GPT-5.6 Thinking by Chat01
생성일: 2026년 10월 25일
답변에 사용된 모델 GPT-5.6 Thinking by Chat01
Optimal approach: Identify the best algorithm based on the problem constraints. Avoid brute force if a more efficient solution exists.
Simple logic: Use the simplest approach that achieves optimal or best practical complexity.
Concise code: Write short, clean, readable C++ code without unnecessary variables, functions, comments, or explanations.
C++ standard: Use C++17 and STL efficiently.
Time complexity: Minimize execution time and analyze the worst-case complexity.
Space complexity: Minimize auxiliary memory usage without sacrificing correctness or performance.
Edge cases: Handle empty inputs where applicable, duplicates, negative values, boundary conditions, and integer overflow
Use one pass through the names and an unordered_map to count duplicate base email addresses.
cpp#include <string> #include <sstream> #include <unordered_map> #include <algorithm> #include <vector> using namespace std; string solution(string &S, string &C) { transform(C.begin(), C.end(), C.begin(), ::tolower); unordered_map<string, int> cnt; stringstream ss(S); string name, ans; while (getline(ss, name, ';')) { if (!name.empty() && name[0] == ' ') name.erase(0, 1); stringstream ns(name); vector<string> parts; string x; while (ns >> x) parts.push_back(x); string first = parts.front(); string last = parts.back(); transform(first.begin(), first.end(), first.begin(), ::tolower); transform(last.begin(), last.end(), last.begin(), ::tolower); last.erase(remove(last.begin(), last.end(), '-'), last.end()); if (last.size() > 8) last.resize(8); string base = first + "." + last; int n = ++cnt[base]; string email = base; if (n > 1) email += to_string(n); email += "@" + C + ".com"; if (!ans.empty()) ans += "; "; ans += name + " <" + email + ">"; } return ans; }
Complexity:
N is the number of employees.It correctly handles middle names, case conversion, hyphen removal before the 8-character truncation, and duplicate addresses such as john.doe2@..., john.doe3@....