当前位置:网站首页>[day_030420] find the longest consecutive number string in the string
[day_030420] find the longest consecutive number string in the string
2022-07-26 06:11:00 【On the Bank of Anhe Bridge】
Find the longest consecutive number string in the string
Title source
Cattle from : Find the longest consecutive number string in the string
Title Description
Read in a string str, Output string str The longest consecutive number string in
Input description
Each test input contains 1 Test cases , A string str, Length not exceeding 255.
Output description
Output... In one line str The longest consecutive number string in .
Example 1
Input
abcd12345ed125ss123456789
Output
123456789
Thought analysis
- Traversal string , Use one cur Save the current continuous number string , use ret Save the current longest number string
- Each occurrence of a non numeric character represents a cur End of string , Compare the current cur And ret The length of
- After each comparison , Empty cur String content
Code display
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
string cur;
string ret;
getline(cin, str);
// The control condition of the outer cycle is <=, Otherwise, when the number string is at the end of the original string
// If the last number string found is the longest string , If the cycle condition is not satisfied, it will exit the cycle directly , No more assignments
for (int i = 0; i <= str.length(); i++)
{
// If the current character is a number , Then make a splice
if (isdigit(str[i]))
{
cur += str[i];
}
// Characters are non numeric , Is the current cur End of string
else
{
//string Initial after definition lenth by 0
if (cur.length() > ret.length())
{
// Find a longer string , Update string
ret = cur;
}
// After comparison cur And ret after , Yes cur Clear
cur.clear();
}
}
cout << ret << endl;
return 0;
}
summary
- isdigit() function , Determine whether the character is a numeric character
- clear() function , Empty the string contents
边栏推荐
- Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
- 【Day_01 0418】删除公共字符串
- 基于消防GIS系统的智慧消防应用
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
- 移动web
- 【Day03_0420】C语言选择题
- Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
- C language explanation series - comprehensive exercises, guessing numbers games
- 【Day05_0422】C语言选择题
- 二叉树的前中后序遍历——本质(每个节点都是“根”节点)
猜你喜欢

【Day04_0421】C语言选择题

Youwei low code: Brick life cycle component life cycle

H. Take the Elevator 贪心

flex布局

知识沉淀一:架构师是做什么?解决了什么问题

Blurring of unity pixel painting

Modifiers should be declared in the correct order

Kingbasees SQL language reference manual of Jincang database (6. Expression)

The time complexity of two recursive entries in a recursive function

【Day05_0422】C语言选择题
随机推荐
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
递归函数中 有两个递归入口的时间复杂度
【Day06_0423】C语言选择题
【Day_05 0422】连续最大和
Jincang database kingbasees SQL language reference manual (5. Operators)
Leetcode:934. The shortest Bridge
【Day_04 0421】计算糖果
Ganglia安装部署流程
【BM2 链表内指定区间反转】
Embedded sharing collection 14
How to divide the disks under the devices and drives in win10 new computer
The time complexity of two recursive entries in a recursive function
Workflow activiti5.13 learning notes (I)
卸载手机自带APP的操作步骤
Leetcode 42. rainwater connection
Latex merges multiple rows and columns of a table at the same time
Mysql45 talks about transaction isolation: why can't I see it after you change it?
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
逆序打印链表
【(SV && UVM) 笔试面试遇到的知识点】~ phase机制