给一个字符串,找到连续的最长数字字符串的长度和地址
扫描一遍,类似最大连续子串的做法
1 #include <iostream>
2 #include <string>
3 #include <cctype>
4 using namespace std;
5
6 string foo(const string& str)
7 {
8 string ret, tmp;
9 string::size_type i, j, left, right;
10 i = 0;
11 j = 0;
12 left = right = 0;
13 bool first = true;
14 for (string::size_type k = 0; k != str.size(); ++k)
15 {
16 if (isdigit(str[k]))
17 {
18 if (first)
19 {
20 first = false;
21 i = k;
22 j = k;
23 }
24 else
25 {
26 j = k;
27 }
28 }
29 else
30 {
31 first = true;
32 if ((j - i) > (right - left))
33 {
34 left = i;
35 right = j;
36 }
37 i = j = k;
38 }
39 }
40 ret = str.substr(left, right - left + 1);
41 return ret;
42 }
43
44 int main()
45 {
46 string str;
47 while (cin >> str)
48 {
49 cout << foo(str) << endl;
50 }
51 return 0;
52 }
53
posted on 2011-07-11 14:31
unixfy 阅读(149)
评论(0) 编辑 收藏 引用