最长的数字串(华为机试)
请在一个字符串中找出连续最长的数字串,并返回这个数字串。
如果存在长度相同的连续数字串,返回最后一个。
如果没有符合条件的字符串,返回空字符串””。
注意
数字串可以由数字”0-9″、小数点”.”、正负号”±”组成,长度包括组成数字串的所有符号。
“.”、“±”仅能出现一次,”.”的两边必须是数字,”±”仅能出现在开头且其后必须要有数字。
长度不定,可能含有空格。
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
1234567890abcd9.+12345.678.9ed
输出
+12345.678
代码一:
#include <iostream>
#include <list>
#include <regex>
#include <string>
#include <vector>
using namespace std;
string longestNumberString(const string& s) {
int len = s.size();
int cnt = 0;
int pre_dot = -1;
int max_cnt = 0;
int end_idx = -1;
for (int i = 0; i < len; ++i) {
if (isdigit(s[i])) {
++cnt;
} else if (s[i] == '+' || s[i] == '-') {
if (cnt >= max_cnt) {
max_cnt = cnt;
end_idx = i - 1;
}
if (i < len - 1 && isdigit(s[i + 1])) {
cnt = 1;
} else {
cnt = 0;
}
pre_dot = -1;
} else if (s[i] == '.') {
if (pre_dot == -1) {
if (cnt > 0 && i < len - 1 && isdigit(s[i + 1])) {
cnt++;
pre_dot = i;
} else {
if (cnt >= max_cnt) {
max_cnt = cnt;
end_idx = i - 1;
}
cnt = 0;
}
} else {
if (cnt >= max_cnt) {
max_cnt = cnt;
end_idx = i - 1;
}
if (i < len - 1 && isdigit(s[i + 1])) {
cnt = i - pre_dot;
pre_dot = i;
} else {
cnt = 0;
pre_dot = -1;
}
}
} else {
if (cnt >= max_cnt) {
max_cnt = cnt;
end_idx = i - 1;
}
cnt = 0;
pre_dot = -1;
}
}
if (cnt >= max_cnt) {
max_cnt = cnt;
end_idx = len - 1;
}
if (max_cnt) {
return s.substr(end_idx - max_cnt + 1, max_cnt);
} else {
return "";
}
}
string longestNumberString2(const string& s) {
string res;
if (s.empty()) {
return res;
}
string::const_iterator iter = s.begin();
smatch match;
regex r("[+-]?[0-9]+(\\.[0-9]+)?");
list<string> lst;
int pos = -1;
int step = 0;
while (regex_search(iter, s.end(), match, r)) {
lst.push_back(match.str());
pos = lst.back().find_first_of('.');
step = match.position() + (pos == string::npos ? lst.back().length() : pos + 1);
iter += step;
}
for (string target : lst) {
if (target.length() >= res.length()) {
res = target;
}
}
return res;
}
void test(const string& test_name, const string& str, const string& expected) {
cout << test_name << " begins:" << endl;
if (longestNumberString(str) == expected) {
cout << "solution1 passed." << endl;
} else {
cout << "solution1 failed!!" << endl;
}
if (longestNumberString2(str) == expected) {
cout << "solution2 passed." << endl;
} else {
cout << "solution2 failed!!" << endl;
}
cout << endl;
}
void test1() {
test("test1", "1234567890abcd9.+12345.678.9ed", "+12345.678");
}
void test2() {
test("test2", "+.1", "1");
}
void test3() {
test("test3", "123.456kkk789..7777", "123.456");
}
void test4() {
test("test4", "33.55.66", "55.66");
}
void test5() {
test("test5", "33.55nn.66", "33.55");
}
void test6() {
test("test6", "-123-1234", "-1234");
}
void test7() {
test("test7", "++12--345+-6789", "-6789");
}
void test8() {
test("test8", "+", "");
}
void test9() {
test("test9", "", "");
}
int main(void) {
/* string s;
while (getline(cin, s)) {
} */
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
return 0;
}
代码二:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LongestNumberString {
public static String longestNumberString(String s) {
Pattern pattern = Pattern.compile("[+-]?[0-9]+(\\.[0-9]+)?");
Matcher matcher = pattern.matcher(s);
List<String> list = new ArrayList<>();
int start = 0;
int pos;
String match;
while (matcher.find(start)) {
match = matcher.group();
list.add(match);
pos = match.indexOf('.');
start = matcher.start() + (pos == -1 ? match.length() : pos + 1);
}
String res = "";
for (String target : list) {
if (target.length() >= res.length()) {
res = target;
}
}
return res;
}
static void test(String testName, String str, String expected) {
System.out.println(testName + " begins:");
if (longestNumberString(str).equals(expected)) {
System.out.println("passed.");
} else {
System.out.println("failed!!");
}
System.out.println();
}
static void test1() {
test("test1", "1234567890abcd9.+12345.678.9ed", "+12345.678");
}
static void test2() {
test("test2", "+.1", "1");
}
static void test3() {
test("test3", "123.456kkk789..7777", "123.456");
}
static void test4() {
test("test4", "33.55.66", "55.66");
}
static void test5() {
test("test5", "33.55nn.66", "33.55");
}
static void test6() {
test("test6", "-123-1234", "-1234");
}
static void test7() {
test("test7", "++12--345+-6789", "-6789");
}
static void test8() {
test("test8", "+", "");
}
static void test9() {
test("test9", "", "");
}
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
}
}