{"id":34208,"date":"2024-11-01T09:25:32","date_gmt":"2024-11-01T09:25:32","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34208"},"modified":"2024-11-01T10:58:10","modified_gmt":"2024-11-01T10:58:10","slug":"c-coding-test-course-dictionary","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34208\/","title":{"rendered":"C++ Coding Test Course, Dictionary"},"content":{"rendered":"<div class=\"blog-post\">\n<p>Hello! Today we will tackle an algorithm problem using C++. The topic is &#8220;Dictionary Lookup&#8221;. This problem deals with the process of finding a specific word in a given list of words. We will explore the related algorithm and learn how to efficiently solve the problem.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is as follows.<\/p>\n<pre>\n    Problem: Finding a word in a dictionary\n    - You are given a dictionary consisting of n words.\n    - When a word to search is given as input, write a program to check if this word exists in the dictionary.\n    \n    Input:\n    - The first line contains the number of words n (1 \u2264 n \u2264 100,000).\n    - In the next n lines, each line consists of a word made up of lowercase letters.\n    - The last line contains the word to search.\n    \n    Output:\n    - If the word exists in the dictionary, print 'YES', otherwise print 'NO'.\n    <\/pre>\n<h2>Analysis of the Problem<\/h2>\n<p>This problem is a simple string search problem that involves finding a word in a dictionary. Therefore, the main point to solve is an efficient search method. Since the number of words can be up to 100,000, inefficient methods can lead to timeouts.<\/p>\n<p>The simplest method is to use a list to traverse all words and compare values, but this method has a time complexity of O(n), which is inefficient for lightweight search engines that demand optimal performance in the worst cases. Therefore, we need a method that utilizes a hashmap since it allows for average-case search time complexity of O(1).<\/p>\n<h2>Implementation Method<\/h2>\n<p>Now, let&#8217;s write the code in C++ to solve the problem. We will proceed with the following steps:<\/p>\n<ol>\n<li>Take the number of words as input.<\/li>\n<li>Create a hashmap using unordered_map to store the words.<\/li>\n<li>Add the words in the dictionary to the hashmap.<\/li>\n<li>Input the word to search and check for its existence in the hashmap.<\/li>\n<\/ol>\n<h2>Code Implementation<\/h2>\n<pre>\n    <code>\n    #include <iostream>\n    #include <unordered_map>\n    #include <string>\n\n    using namespace std;\n\n    int main() {\n        int n;\n        cin >> n; \/\/ Input number of words\n        unordered_map<string, bool> dictionary; \/\/ Initialize the hashmap\n\n        \/\/ Input words\n        for (int i = 0; i < n; i++) {\n            string word;\n            cin >> word;\n            dictionary[word] = true; \/\/ Add word to hashmap\n        }\n\n        string searchWord;\n        cin >> searchWord; \/\/ Input the word to search\n\n        \/\/ Check the existence of the word\n        if (dictionary.find(searchWord) != dictionary.end()) {\n            cout << \"YES\" << endl; \/\/ If exists\n        } else {\n            cout << \"NO\" << endl; \/\/ If does not exist\n        }\n\n        return 0;\n    }\n    <\/code>\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>Let me briefly explain the code.<\/p>\n<ul>\n<li><code>#include <iostream><\/code>: Includes the library for input and output in C++.<\/li>\n<li><code>#include <unordered_map><\/code>: Library for using hashmaps, allowing for O(1) search time complexity.<\/li>\n<li><code>#include <string><\/code>: Library for handling strings.<\/li>\n<li><code>unordered_map<string, bool> dictionary;<\/code>: Declares a hashmap to store words.<\/li>\n<li><code>cin >> n;<\/code>: Takes the number of words as input.<\/li>\n<li><code>dictionary[word] = true;<\/code>: Adds the input word to the hashmap. The key of the hashmap is the word, and the value is set to true.<\/li>\n<li><code>dictionary.find(searchWord)<\/code>: Searches for the query in the hashmap to check its existence.<\/li>\n<\/ul>\n<h2>Testing and Results<\/h2>\n<p>Now let's test this code in practice. For example, we will input the following:<\/p>\n<pre>\n    5\n    apple\n    banana\n    cherry\n    date\n    elderberry\n    banana\n    <\/pre>\n<p>If we input as above, the output will be:<\/p>\n<pre>\n    YES\n    <\/pre>\n<p>We can consider another example:<\/p>\n<pre>\n    5\n    apple\n    banana\n    cherry\n    date\n    elderberry\n    fig\n    <\/pre>\n<p>In this case, the output will be:<\/p>\n<pre>\n    NO\n    <\/pre>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is as follows:<\/p>\n<ul>\n<li>Word Insertion: O(1) (on average, due to the nature of the hashmap)<\/li>\n<li>Word Search: O(1) (also on average)<\/li>\n<\/ul>\n<p>Thus, the overall time complexity is O(n). The space complexity depends on the number of words stored in the hashmap, which is O(n).<\/p>\n<h2>Conclusion<\/h2>\n<p>Today we learned how to solve the problem of finding a word in a dictionary using C++. We demonstrated how the approach using hashmaps can significantly improve time complexity. Algorithm problems can always be approached in various ways, so I encourage you to try out different methods!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will tackle an algorithm problem using C++. The topic is &#8220;Dictionary Lookup&#8221;. This problem deals with the process of finding a specific word in a given list of words. We will explore the related algorithm and learn how to efficiently solve the problem. Problem Description The problem is as follows. Problem: Finding &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34208\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Dictionary&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34208","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34208\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will tackle an algorithm problem using C++. The topic is &#8220;Dictionary Lookup&#8221;. This problem deals with the process of finding a specific word in a given list of words. We will explore the related algorithm and learn how to efficiently solve the problem. Problem Description The problem is as follows. Problem: Finding &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Dictionary&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34208\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:10+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Dictionary\",\"datePublished\":\"2024-11-01T09:25:32+00:00\",\"dateModified\":\"2024-11-01T10:58:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/\"},\"wordCount\":443,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34208\/\",\"name\":\"C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:32+00:00\",\"dateModified\":\"2024-11-01T10:58:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34208\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34208\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Dictionary\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34208\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will tackle an algorithm problem using C++. The topic is &#8220;Dictionary Lookup&#8221;. This problem deals with the process of finding a specific word in a given list of words. We will explore the related algorithm and learn how to efficiently solve the problem. Problem Description The problem is as follows. Problem: Finding &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Dictionary\"","og_url":"https:\/\/atmokpo.com\/w\/34208\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:32+00:00","article_modified_time":"2024-11-01T10:58:10+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34208\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34208\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Dictionary","datePublished":"2024-11-01T09:25:32+00:00","dateModified":"2024-11-01T10:58:10+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34208\/"},"wordCount":443,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34208\/","url":"https:\/\/atmokpo.com\/w\/34208\/","name":"C++ Coding Test Course, Dictionary - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:32+00:00","dateModified":"2024-11-01T10:58:10+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34208\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34208\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34208\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Dictionary"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34208"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34208\/revisions"}],"predecessor-version":[{"id":34209,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34208\/revisions\/34209"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}