HJ92 在字符串中找出连续最长的数字串
摘要
Title: HJ92 在字符串中找出连续最长的数字串
Tag: 双指针
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
HJ92 在字符串中找出连续最长的数字串
-
题意
输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)
-
思路
双指针即可,固定左指针,遍历右指针
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
signed main()
{
// freopen("Tests/input_1.txt", "r", stdin);
IOS;
string s;
while(cin >> s){
vector<string> ans;
int mx = 0, n = SZ(s);
for(int i = 0, j = 0; i < n; ++ i){
if(isdigit(s[i])){
int cnt = 0;
j = i;
while(j < n && isdigit(s[j])){
j ++;
}
if (j - i > mx){
ans.clear();
ans.push_back(s.substr(i, j - i));
mx = j - i;
}
else if (j - i == mx) {
ans.push_back(s.substr(i, j - i));
}
i = j - 1;
}
}
for(auto ss : ans) cout << ss;
cout << "," << mx << '\n';
}
return 0;
}