华为机试在线训练-牛客网(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)在字符串中找出连续最长的数字串】
推荐阅读
- 广角叙述|广角叙述 展众生群像——试析鲁迅《示众》的展示艺术
- 亲子日记(287)2019.4.27.
- 在线版的迅捷思维导图怎么操作()
- 移动端h5调试方法
- 合理情绪疗法之试用|李克富思维训练营56/90
- 试论化院的学生自组织
- 自律第1天
- 蓝桥杯试题
- 女生该不该用小号测试男朋友()
- BNC公链|BNC公链 | Eth2.0测试网Topaz已质押超100万枚ETH