题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
# include<iostream> # include<string> # include<vector> using namespace std; int main() { vector<string> mys; string s; while(cin >> s) { mys.push_back(s); } for(int i = 0; i <mys.size(); i++) { string str = mys[i]; vector<pair<int, int>> record; int l = 0; int r = 0; while(r < str.size()) { while(r < str.size() && str[r] >= '0' && str[r] <= '9') { r++; } if(l < r) { record.push_back({l,r}); l = r; } else { l++; r++; } } int length = 0; vector<int> left; for(int i = 0; i < record.size(); i++) { if(record[i].second - record[i].first >length) { while(!left.empty()) { left.pop_back(); } length = record[i].second - record[i].first; left.push_back(record[i].first); } else if(record[i].second - record[i].first == length) { left.push_back(record[i].first); } } string s; for(int i = 0; i < left.size(); i++) { s += str.substr(left[i], length); } cout<< s << ',' << length << endl; } system("pause"); return 0; }