{"id":316,"date":"2013-07-13T02:54:35","date_gmt":"2013-07-13T06:54:35","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=316"},"modified":"2015-11-24T10:31:47","modified_gmt":"2015-11-24T15:31:47","slug":"uva-12478-hardest-problem-ever-easy","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html","title":{"rendered":"UVA 12478 &#8211; Hardest Problem Ever (Easy)"},"content":{"rendered":"<p>So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn&#8217;t mind at most 7 WA&#8217;s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve it for me.<\/p>\n<p>I used this string normalization technique I was &#8216;taught&#8217; in college. It&#8217;s a neat little trick that I seem to use every chance I get. Pretty easy solution once you know you can map all permutations of a string to just one string!<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.PrintWriter;\r\nimport java.util.Arrays;\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * \r\n * @author Sanchit M. Bhatnagar\r\n * @see http:\/\/uhunt.felix-halim.net\/id\/74004\r\n * \r\n *\/\r\npublic class P12478 {\r\n\r\n  public static void main(String&#x5B;] args) {\r\n    Scanner sc = new Scanner(System.in);\r\n    PrintWriter out = new PrintWriter(System.out);\r\n\r\n    String&#x5B;] grid = new String&#x5B;] { &quot;OBIDAIBKR&quot;, &quot;RKAULHISP&quot;, &quot;SADIYANNO&quot;, &quot;HEISAWHIA&quot;, &quot;IRAKIBULS&quot;, &quot;MFBINTRNO&quot;, &quot;UTOYZIFAH&quot;, &quot;LEBSYNUNE&quot;, &quot;EMOTIONNAL&quot; };\r\n\r\n    String&#x5B;] originalNames = new String&#x5B;] { &quot;RAKIBUL&quot;, &quot;ANINDYA&quot;, &quot;MOSHIUR&quot;, &quot;SHIPLU&quot;, &quot;KABIR&quot;, &quot;SUNNY&quot;, &quot;OBAIDA&quot;, &quot;WASI&quot; };\r\n\r\n    String&#x5B;] names = new String&#x5B;originalNames.length];\r\n    int&#x5B;] count = new int&#x5B;names.length];\r\n\r\n    for (int i = 0; i &amp;lt; names.length; i++) {\r\n      names&#x5B;i] = normalize(originalNames&#x5B;i]);\r\n    }\r\n\r\n    \/\/ Check Horizontally\r\n    for (int zz = 0; zz &amp;lt; 9; zz++) {\r\n      for (int i = 0; i &amp;lt; 9; i++) {\r\n        for (int j = i + 4; j &amp;lt;= 9; j++) { \/\/Shortest name is 4 characters so we are checking for sub-strings of at least that length. We could also have capped the sub-string to at most 7 characters based on the names provided.\r\n          String substring = normalize(grid&#x5B;zz].substring(i, j));\r\n          for (int k = 0; k &amp;lt; names.length; k++) {\r\n            if (substring.equals(names&#x5B;k])) {\r\n              count&#x5B;k]++;\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\n    \/\/ Check Vertically\r\n    String&#x5B;] grid2 = new String&#x5B;grid.length];\r\n    for (int j = 0; j &amp;lt; 9; j++) {\r\n      char&#x5B;] tmp = new char&#x5B;9];\r\n      for (int i = 0; i &amp;lt; 9; i++) {\r\n        tmp&#x5B;i] = grid&#x5B;i].charAt(j);\r\n      }\r\n      grid2&#x5B;j] = new String(tmp);\r\n    }\r\n\r\n    for (int zz = 0; zz &amp;lt; 9; zz++) {\r\n      for (int i = 0; i &amp;lt; 9; i++) {\r\n        for (int j = i + 4; j &amp;lt;= 9; j++) {\r\n          String substring = normalize(grid2&#x5B;zz].substring(i, j));\r\n          for (int k = 0; k &amp;lt; names.length; k++) {\r\n            if (substring.equals(names&#x5B;k])) {\r\n              count&#x5B;k]++;\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\n    for (int i = 0; i &amp;lt; count.length; i++) {\r\n      if (count&#x5B;i] == 2) {\r\n        out.println(originalNames&#x5B;i]);\r\n      }\r\n    }\r\n\r\n    out.close();\r\n    sc.close();\r\n  }\r\n\r\n  \/\/ Normalizes all strings\r\n  private static String normalize(String word) {\r\n    char&#x5B;] list = word.toCharArray();\r\n    Arrays.sort(list);\r\n    return new String(list);\r\n  }\r\n}\r\n<\/pre>\n<p>Edit: Further explanation. I&#8217;ve also updated the code to avoid the IndexOutOfBounds exception I was strangely using a try-catch for.<\/p>\n<p>Firstly let us cover the normalize function. As you can tell by the code, it\u00a0takes a word, splits it into a character array, sorts the character array and then converts it back into a string. Let us take some examples so that this is clear.<\/p>\n<p>Take a word, let&#8217;s say: apple once you run it through the normalize function it will become aelpp. The same will happen for all permutations of apple (I won&#8217;t list them all) for example aplpe, ppael, leapp, etc. This allows us to quickly check if a particular permutation of a word is in the grid.<\/p>\n<p>If this part is clear then we can just move on to the checking part.<\/p>\n<p>We will go through the grid one row at a time (the code with the comment stating horizontal). Find all substrings within that line, normalize them and then compare them with the names array. If they are in the array then we increment a counter (named count). We do the same thing for the grid vertically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn&#8217;t mind at most 7 WA&#8217;s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UVA 12478 &#8211; Hardest Problem Ever (Easy)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14,18,19,17],"tags":[23,21,20,22],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-competitive-programming-3","category-java","category-uva-online-judge","tag-algorithms-2","tag-competitive-programming-3-2","tag-java-2","tag-uva-online-judge-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder<\/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:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn&#8217;t mind at most 7 WA&#8217;s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve &hellip; Continue reading UVA 12478 &#8211; Hardest Problem Ever (Easy) &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2013-07-13T06:54:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-11-24T15:31:47+00:00\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"UVA 12478 &#8211; Hardest Problem Ever (Easy)\",\"datePublished\":\"2013-07-13T06:54:35+00:00\",\"dateModified\":\"2015-11-24T15:31:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html\"},\"wordCount\":635,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"keywords\":[\"algorithms\",\"competitive programming 3\",\"java\",\"uva online judge\"],\"articleSection\":[\"Algorithms\",\"Competitive Programming 3\",\"Java\",\"UVA Online Judge\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html\",\"name\":\"UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-07-13T06:54:35+00:00\",\"dateModified\":\"2015-11-24T15:31:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-12478-hardest-problem-ever-easy-316.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UVA 12478 &#8211; Hardest Problem Ever (Easy)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder","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:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html","og_locale":"en_US","og_type":"article","og_title":"UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder","og_description":"So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn&#8217;t mind at most 7 WA&#8217;s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve &hellip; Continue reading UVA 12478 &#8211; Hardest Problem Ever (Easy) &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html","og_site_name":"Ferocious Coder","article_published_time":"2013-07-13T06:54:35+00:00","article_modified_time":"2015-11-24T15:31:47+00:00","author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"UVA 12478 &#8211; Hardest Problem Ever (Easy)","datePublished":"2013-07-13T06:54:35+00:00","dateModified":"2015-11-24T15:31:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html"},"wordCount":635,"commentCount":2,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"keywords":["algorithms","competitive programming 3","java","uva online judge"],"articleSection":["Algorithms","Competitive Programming 3","Java","UVA Online Judge"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html","name":"UVA 12478 - Hardest Problem Ever (Easy) - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"datePublished":"2013-07-13T06:54:35+00:00","dateModified":"2015-11-24T15:31:47+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-12478-hardest-problem-ever-easy-316.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"UVA 12478 &#8211; Hardest Problem Ever (Easy)"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1mYMV-56","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":3,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/316\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}