本题目要求读入任意字符串,输出字符串的最长数字子串。若无数字字符,则输出“No digits”。
输入格式:
输入任意字符串。
输出格式:
输出字符串的最长数字子串。若无数字字符,则输出“No digits”。
输入样例:
12345
输出样例:
12345
输入样例:
abc12de33445fg6
输出样例:
33445
输入样例:
abcde
输出样例:
No digits
简单题,状态机泡一下就行了
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO {
char print_f[105];
void read() { }
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K;
char buf[MAXN];
int main() {
#ifdef debug
freopen("test", "r", stdin);
// freopen("out_main", "w", stdout);
clock_t stime = clock();
#endif
scanf("%s ", buf);
// int lef = -1, rig = -1, ans = 0;
#define IS_NUM (buf[i]>='0' && buf[i]<='9')
//状态机
//1数字状态 0其他状态
enum now { IN = 1, OUT = 0 }; //在数字串内,和在数字串外
int status = OUT, cnt = 0, ans = 0;
string str, tmp;
for(int i=0; ; i++) {
if(status == IN) {
if(IS_NUM) {
cnt ++;
tmp.push_back(buf[i]);
} else {
if(tmp.length() > str.length())
str = tmp;
ans = max(ans, cnt);
tmp.clear();
cnt = 0;
}
} else {
if(IS_NUM) {
status = IN;
++ cnt;
tmp.push_back(buf[i]);
}
}
if(!buf[i]) {
if(tmp.length() > str.length())
str = tmp;
ans = max(ans, cnt);
break;
}
}
// printf("%d %s\n", ans, str.data());
printf("%s\n", str.length() ? str.data() : "No digits");
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}