华为机试在线训练-牛客网(38)在字符串中找出连续最长的数字串



题目描述 样例输出
输出123058789,函数返回值9
输出54761,函数返回值5

接口说明
函数原型:
unsignedint Continumax(char** pOutputstr,char* intputstr)
输入参数:
char* intputstr输入字符串;
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;
返回值:
连续最长的数字串的长度





输入描述:


输入一个字符串。


输出描述:

输出字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度

输入例子:
abcd12345ed125ss123058789


输出例子:
123058789,9



这种子串问题应该用动态规划是最好的,可参考最长公共子串一题的动态规划解法,以下为硬解AC的代码:

//硬解,暴力枚举,从最长的子串开始判定 #include #include using namespace std; bool judgeSub(string subStr){ for(auto c:subStr){ if(c>='0'&&c<='9')continue; else return false; } return true; } int main(){ string str; while(cin>>str){ vector res; int flag=0; for(int len=str.size(); len>0; len--){ for(int i=0; i<=str.size()-len; i++){ string subStr=str.substr(i,len); if(judgeSub(subStr)){ res.push_back(subStr); flag=1; } } if(flag)break; } for(auto e:res){ cout<







【华为机试在线训练-牛客网(38)在字符串中找出连续最长的数字串】

    推荐阅读