{"id":34180,"date":"2024-11-01T09:25:12","date_gmt":"2024-11-01T09:25:12","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34180"},"modified":"2024-11-01T10:58:17","modified_gmt":"2024-11-01T10:58:17","slug":"c-coding-test-course-string-search-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34180\/","title":{"rendered":"C++ Coding Test Course, String Search"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this article, we will explore the problem of finding strings using C++. Solving algorithmic problems helps improve programming skills and assists in achieving good results in coding tests. Particularly in recent years, string manipulation problems have frequently appeared in coding tests. Therefore, a thorough understanding and practice of this topic are necessary.<\/p>\n<h2>Problem Description<\/h2>\n<p>In this lecture, we will address the problem of finding a specific string (pattern) within a given text and counting how many times that string appears in the text. This problem serves as a fundamental example of string search algorithms and will solve the following problem.<\/p>\n<p><strong>Problem:<\/strong> <\/p>\n<p>Given a string <code>text<\/code> and <code>pattern<\/code>, write a program to determine how many times <code>pattern<\/code> appears in <code>text<\/code>.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>First line: string <code>text<\/code> (1 \u2264 |text| \u2264 100,000)<\/li>\n<li>Second line: string <code>pattern<\/code> (1 \u2264 |pattern| \u2264 100,000)<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<ul>\n<li>Print the count of matching patterns.<\/li>\n<\/ul>\n<h2>Approach<\/h2>\n<p>There are several algorithms that can be used to solve the problem, but we will look at a simple brute force method here. This method checks each position in the text against the pattern to determine if they match. The performance is O(n * m), where n is the length of the text and m is the length of the pattern. This method is straightforward and intuitive, but one might also consider more efficient methods like the KMP algorithm or the Rabin-Karp algorithm.<\/p>\n<h2>Code Implementation<\/h2>\n<p>Let&#8217;s implement the code. Below is a string-finding program written in C++:<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nint countOccurrences(const std::string &amp;text, const std::string &amp;pattern) {\n    int count = 0;\n    int textLength = text.length();\n    int patternLength = pattern.length();\n\n    for (int i = 0; i &lt;= textLength - patternLength; i++) {\n        \/\/ Compare substring through partial string comparison\n        if (text.substr(i, patternLength) == pattern) {\n            count++;\n        }\n    }\n    return count;\n}\n\nint main() {\n    std::string text, pattern;\n\n    std::cout &lt;&lt; \"Enter text: \";\n    std::getline(std::cin, text);\n    std::cout &lt;&lt; \"Enter pattern: \";\n    std::getline(std::cin, pattern);\n\n    int occurrences = countOccurrences(text, pattern);\n    std::cout &lt;&lt; \"The pattern appears \" &lt;&lt; occurrences &lt;&lt; \" times.\" &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>The above C++ code solves the string-finding problem. It performs functions based on the <code>text<\/code> and <code>pattern<\/code> provided by the user.<\/p>\n<ul>\n<li><strong>countOccurrences function<\/strong>: This function counts the occurrences of <code>pattern<\/code> in the given <code>text<\/code>. It extracts substrings from each position in the text and increases the count whenever there is a match with the given pattern.<\/li>\n<li><strong>main function<\/strong>: This part handles basic input and output. It takes a string input from the user and calls the <code>countOccurrences<\/code> function to print the result.<\/li>\n<\/ul>\n<h2>Performance Analysis<\/h2>\n<p>The brute-force approach described above has a worst-case time complexity of O(n * m). As the length of the text increases, performance may degrade. However, if simplicity and intuitiveness are prioritized, this method can be useful.<\/p>\n<p>For an efficient solution, consider the KMP (Knuth-Morris-Pratt) algorithm. The KMP algorithm optimizes string searches by utilizing repeated parts of the pattern. This algorithm has a time complexity of O(n + m).<\/p>\n<h2>KMP Algorithm Introduction<\/h2>\n<p>The KMP algorithm is designed to solve pattern search problems and reduces repetitive checks by utilizing previously matched parts. This algorithm consists of two main steps. The first step builds an &#8216;LPS (Longest Prefix Suffix)&#8217; array for the pattern, and the second step uses this LPS array to search for the pattern in the text.<\/p>\n<h3>Building the LPS Array<\/h3>\n<p>The LPS array stores the lengths of the longest prefixes and suffixes among each prefix of the pattern. For example, for the pattern &#8220;ABAB,&#8221; the LPS array becomes [0, 0, 1, 2]. This has the following implications:<\/p>\n<ul>\n<li>No prefix and suffix for A at index 0, so it&#8217;s 0<\/li>\n<li>No prefix and suffix for B at index 1, so it&#8217;s 0<\/li>\n<li>AB is both prefix and suffix at index 2, so it&#8217;s 1<\/li>\n<li>ABA is both prefix and suffix at index 3, so it&#8217;s 2<\/li>\n<\/ul>\n<h3>KMP Algorithm Implementation<\/h3>\n<p>Below is the C++ code that implements the KMP algorithm:<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n\nstd::vector<int> computeLPS(const std::string &amp;pattern) {\n    int m = pattern.length();\n    std::vector<int> lps(m);\n    int len = 0; \n    lps[0] = 0; \n    int i = 1;\n\n    while (i &lt; m) {\n        if (pattern[i] == pattern[len]) {\n            len++;\n            lps[i] = len;\n            i++;\n        } else {\n            if (len != 0) {\n                len = lps[len - 1];\n            } else {\n                lps[i] = 0;\n                i++;\n            }\n        }\n    }\n    return lps;\n}\n\nint KMP(const std::string &amp;text, const std::string &amp;pattern) {\n    std::vector<int> lps = computeLPS(pattern);\n    int n = text.length();\n    int m = pattern.length();\n    int i = 0; \n    int j = 0; \n    int count = 0;\n\n    while (i &lt; n) {\n        if (pattern[j] == text[i]) {\n            i++;\n            j++;\n        }\n        if (j == m) {\n            count++;\n            j = lps[j - 1];\n        } else if (i &lt; n &amp;&amp; pattern[j] != text[i]) {\n            if (j != 0)\n                j = lps[j - 1];\n            else\n                i++;\n        }\n    }\n    return count;\n}\n\nint main() {\n    std::string text, pattern;\n\n    std::cout &lt;&lt; \"Enter text: \";\n    std::getline(std::cin, text);\n    std::cout &lt;&lt; \"Enter pattern: \";\n    std::getline(std::cin, pattern);\n\n    int occurrences = KMP(text, pattern);\n    std::cout &lt;&lt; \"The pattern appears \" &lt;&lt; occurrences &lt;&lt; \" times.\" &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/int><\/int><\/int><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned how to solve the string-finding problem using C++. After introducing the brute-force method, we explored how the efficient KMP algorithm operates. Problems related to strings can further develop based on basic algorithm knowledge. Therefore, it is essential to continue practicing such problems to enhance programming skills.<\/p>\n<p>Try solving various string problems and develop your own algorithms! You&#8217;ll gain confidence in coding tests. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this article, we will explore the problem of finding strings using C++. Solving algorithmic problems helps improve programming skills and assists in achieving good results in coding tests. Particularly in recent years, string manipulation problems have frequently appeared in coding tests. Therefore, a thorough understanding and practice of this topic are necessary. Problem &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34180\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, String Search&#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-34180","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, String Search - \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\/34180\/\" \/>\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, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this article, we will explore the problem of finding strings using C++. Solving algorithmic problems helps improve programming skills and assists in achieving good results in coding tests. Particularly in recent years, string manipulation problems have frequently appeared in coding tests. Therefore, a thorough understanding and practice of this topic are necessary. Problem &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, String Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34180\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:17+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, String Search\",\"datePublished\":\"2024-11-01T09:25:12+00:00\",\"dateModified\":\"2024-11-01T10:58:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/\"},\"wordCount\":612,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34180\/\",\"name\":\"C++ Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:12+00:00\",\"dateModified\":\"2024-11-01T10:58:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34180\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34180\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, String Search\"}]},{\"@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, String Search - \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\/34180\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this article, we will explore the problem of finding strings using C++. Solving algorithmic problems helps improve programming skills and assists in achieving good results in coding tests. Particularly in recent years, string manipulation problems have frequently appeared in coding tests. Therefore, a thorough understanding and practice of this topic are necessary. Problem &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, String Search\"","og_url":"https:\/\/atmokpo.com\/w\/34180\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:12+00:00","article_modified_time":"2024-11-01T10:58:17+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34180\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34180\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, String Search","datePublished":"2024-11-01T09:25:12+00:00","dateModified":"2024-11-01T10:58:17+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34180\/"},"wordCount":612,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34180\/","url":"https:\/\/atmokpo.com\/w\/34180\/","name":"C++ Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:12+00:00","dateModified":"2024-11-01T10:58:17+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34180\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34180\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34180\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, String Search"}]},{"@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\/34180","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=34180"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34180\/revisions"}],"predecessor-version":[{"id":34181,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34180\/revisions\/34181"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}