{
  "question_id": 7443725,
  "question_title": "输出最长的递增数字字符串",
  "question_content": "<p>如何在一亿位整数组成的字符串中找到最长的递增数字字符串&#xff1f;</p>",
  "difficulty": "中等",
  "answer_id": 53421026,
  "answer_content": "<p>1亿个字符串占用的缓存超出了编译器的默认缓存&#xff0c;所以我代码中定义了一个宏来表示字符串的最大长度&#xff0c;如果你需要扩展到1亿&#xff0c;需要修改编译器的默认缓存大小&#xff0c;然后把宏定义的值调大即可。代码如下&#xff0c;如有帮助&#xff0c;请采纳一下&#xff0c;谢谢。</p>\n\n<pre>\n<code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#define MAX_SIZE 100000\nint main()\n{\n\tchar buf[MAX_SIZE] &#61; {0};\n\tint i &#61; 0,len &#61; 0,index &#61; 0;\n\tchar maxbuf[12] &#61; {0}; //最大的可能是0-9&#xff0c;最多10个字符&#xff0c;12个空间足够用\n\tchar maxbuf2[12] &#61; {0};\n\tint maxlen &#61; 0;\n\tgets(buf); //实际字符串由外部输入&#xff0c;或者从文件读取&#xff0c;这个根据实际情况修改吧\n\tlen &#61; strlen(buf); //获取实际长度\n\n\tmaxbuf2[0] &#61; buf[0];\n\ti &#61; 1;\n\tindex &#61; 1;\n\twhile(i &lt; len)\n\t{\n\t\tif (buf[i] &gt; buf[i-1])\n\t\t{\n\t\t\tmaxbuf2[index] &#61; buf[i];\n\t\t\tindex&#43;&#43;;\n\t\t}else\n\t\t{\n\t\t\tif (index &gt; maxlen)\n\t\t\t{\n\t\t\t\tmaxlen &#61; index;\n\t\t\t\tstrcpy(maxbuf,maxbuf2);\n\t\t\t\tmaxbuf[index] &#61; &#39;\\0&#39;;\n\t\t\t\t\n\t\t\t\tmaxbuf2[0] &#61; buf[i];\n\t\t\t\tindex &#61; 1;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tmaxbuf2[0] &#61; buf[i];\n\t\t\t\tindex &#61; 1;\n\t\t\t}\n\t\t}\n\t\ti&#43;&#43;;\n\t}\n\t//处理最后一个串\n\tif (index &gt; maxlen)\n\t{\n\t\tmaxlen &#61; index;\n\t\tstrcpy(maxbuf,maxbuf2);\n\t\tmaxbuf[index] &#61; &#39;\\0&#39;;\n\t}\n\tprintf(&#34;最大串长度:%d,字符串:%s\\n&#34;,maxlen,maxbuf);\n\treturn 0;\n}\n</code></pre>\n\n<p> </p>\n",
  "tag_name": "c语言",
  "cpp": "#include <stdio.h>\n#include <string.h>\n#define MAX_SIZE 100000\nint main()\n{\n\tchar buf[MAX_SIZE] = {0};\n\tint i = 0,len = 0,index = 0;\n\tchar maxbuf[12] = {0}; \n\tchar maxbuf2[12] = {0};\n\tint maxlen = 0;\n\tgets(buf); \n\tlen = strlen(buf); \n\tmaxbuf2[0] = buf[0];\n\ti = 1;\n\tindex = 1;\n\twhile(i < len)\n\t{\n\t\tif (buf[i] > buf[i-1])\n\t\t{\n\t\t\tmaxbuf2[index] = buf[i];\n\t\t\tindex++;\n\t\t}else\n\t\t{\n\t\t\tif (index > maxlen)\n\t\t\t{\n\t\t\t\tmaxlen = index;\n\t\t\t\tstrcpy(maxbuf,maxbuf2);\n\t\t\t\tmaxbuf[index] = '\\0';\n\t\t\t\tmaxbuf2[0] = buf[i];\n\t\t\t\tindex = 1;\n\t\t\t}else\n\t\t\t{\n\t\t\t\tmaxbuf2[0] = buf[i];\n\t\t\t\tindex = 1;\n\t\t\t}\n\t\t}\n\t\ti++;\n\t}\n\tif (index > maxlen)\n\t{\n\t\tmaxlen = index;\n\t\tstrcpy(maxbuf,maxbuf2);\n\t\tmaxbuf[index] = '\\0';\n\t}\n\tprintf(\"最大串长度:%d,字符串:%s\\n\",maxlen,maxbuf);\n\treturn 0;\n}",
  "topic_link": "https://bbs.csdn.net/topics/600469968",
  "status": 1,
  "keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配",
  "license": "csdn.net",
  "notebook": {
    "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/137.ipynb?type=file"
  },
  "notebook_enable": 1,
  "author": "lumh163"
}