{"id":258,"date":"2016-04-18T11:06:02","date_gmt":"2016-04-18T11:06:02","guid":{"rendered":"http:\/\/emacslisp.com\/?p=258"},"modified":"2016-04-18T11:06:02","modified_gmt":"2016-04-18T11:06:02","slug":"google-codejam-problem-a-the-last-word","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=258","title":{"rendered":"Google CodeJam &#8211; Problem A. The Last Word"},"content":{"rendered":"<p>https:\/\/code.google.com\/codejam\/contest\/4304486\/dashboard#s=p0&#038;a=2<\/p>\n<p>This problem is very simple, use basic linked operation will be enough,<\/p>\n<p>it only compare the head item and tail item.<\/p>\n<p>if new item > head item, then new item will be inserted into head.<\/p>\n<p>if new item <= head item, then it will be inserted int tail.\n\ncheck following code.\n\n\n\n<pre lang=\"c\" line=\"1\"> \r\n#include <stdio.h>\r\n#include <string.h>\r\n#include <stdlib.h>\r\n#include <math.h>\r\n\r\nstruct element\r\n{\r\n\tchar c;\r\n\tstruct element *next;\r\n};\r\n\r\nint main(int argc,char **argv) \r\n{ \r\n\tint N;\r\n\tchar c;\r\n\tscanf(&#8220;%d\\n&#8221;,&#038;N);\r\n\r\n\tchar array[2000];\r\n\r\n\tfor(int i=0;i<N;i++)\r\n\t{\r\n\t\tmemset(array,0,sizeof(char)*2000);\r\n\r\n\t\tscanf(\"%s\\n\",array);\r\n\r\n\t\t\r\n\t\tstruct element *tail;\r\n\t\tstruct element *head = (struct element *)malloc(sizeof(struct element));\r\n\t\tmemset(head,0,sizeof(struct element));\r\n\t\thead->c = array[0];\r\n\t\thead->next = NULL;\r\n\t\ttail = head;\r\n\r\n\t\tfor(int j=1;array[j]!=&#8217;\\0&#8242;;j++)\r\n\t\t{\r\n\t\t\tif(array[j]>=head->c)\r\n\t\t\t{\r\n\t\t\t\tstruct element *e = (struct element *)malloc(sizeof(struct element));\r\n\t\t\t\te->c = array[j];\r\n\t\t\t\te->next = head;\r\n\t\t\t\thead = e;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tstruct element *e = (struct element *)malloc(sizeof(struct element));\r\n\t\t\t\te->c = array[j];\r\n\t\t\t\te->next = NULL;\r\n\t\t\t\ttail->next = e;\r\n\t\t\t\ttail = e;\r\n\t\t\t}\r\n\t\t}\r\n\t\tprintf(&#8220;Case #%d: &#8220;,i+1);\r\n\t\tstruct element *p = head;\r\n\t\tfor(;;)\r\n\t\t{\r\n\t\t\tif(p!=NULL){\r\n\t\t\t\tprintf(&#8220;%c&#8221;,p->c);\r\n\t\t\t\tp=p->next;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t\tbreak;\r\n\t\t}\r\n\t\tprintf(&#8220;\\n&#8221;);\r\n\r\n\t}\r\n\r\n\treturn 0; \r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/code.google.com\/codejam\/contest\/4304486\/dashboard#s=p0&#038;a=2 This problem is very simple, use basic linked operation will be enough, it only compare the head item and tail item. if new item > head item, then new item will be inserted into head. if new item next = NULL; tail = head; for(int j=1;array[j]!=&#8217;\\0&#8242;;j++) { if(array[j]>=head->c) { struct element *e = (struct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-258","post","type-post","status-publish","format-standard","hentry","category-googlecodejam"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258"}],"version-history":[{"count":1,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":259,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions\/259"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}