今日の質問-文字列の中で最も長い連続した数字の文字列を見つけます

文字列内で最長の連続番号文字列を検索します

文字列内で最長の連続番号文字列を検索します
制限時間:3秒
スペース制限:32768K

タイトルの説明
文字列内で最長の連続番号文字列を見つけて、この文字列の長さを返します。同じ長さの連続番号文字列がある場合は、最後の連続番号文字列を返します。
注:番号文字列のみを作成する必要があります。数値の順序は不要です。たとえば、数値文字列「1234」の長さが数値文字列「1359055」よりも短い場合、数値がない場合は
、NULLではなく空の文字列( "")が返されます。(注:負の値を考慮する必要はありません)
入力説明:
文字列説明出力:
すべての図の数字の連続文字列と
例1の最長文字列の長さ
入力
abcd12345ed125ss123058789
出力
123 058 789
.9

ソースコード

/********************************************************************************************

在字符串中找出连续最长的数字串
时间限制:3秒
空间限制:32768K

题目描述
请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;
注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,
则返回空字符串(“”)而不是NULL!(说明:不需要考虑负数)
输入描述 :
字符串输出描述:
连续数字串&在所有数字串最长的长度
示例1
	输入
	abcd12345ed125ss123058789
	输出
123058789
9
********************************************************************************************/
#include <iostream>
#include <string>
using namespace std;


int main()
{
    
    
	string srt = {
    
     "abcd155555555555552345ed125ss123058789" };
	int len_max = 0;
	int len0 = 0;
	int index = 0;
	for (int i = 0; i < srt.size(); i++)
	{
    
    
		if (srt[i] >= '0'&& srt[i] <= '9')
		{
    
    
			len0++;
			if (len0 > len_max)
			{
    
    
				index = i;
				len_max = len0;
			}
		}
		else
		{
    
    
			len0 = 0;
		}
	}
	index = index- len_max+1;
	for (int i = index; i < index+ len_max; i++)
	{
    
    
		printf("%c", srt[i]);
	}
	if (len_max == 0)
	{
    
    
		printf("数字字符串为空\n");
		printf(" ");
	}
	printf("\n");
	printf("len_max=%d", len_max);
	printf("len_max=%d", len_max);


}

おすすめ

転載: blog.csdn.net/mao_hui_fei/article/details/113271254