历届试题 正则问题【递归】

问题描述
  考虑一种简单的正则表达式:
  只由 x ( ) | 组成的正则表达式。
  小明想求出这个正则表达式能接受的最长字符串的长度。

  例如 ((xx|xxx)x|(x|xx))xx 能接受的最长字符串是: xxxxxx,长度是6。
输入格式
  一个由x()|组成的正则表达式。输入长度不超过100,保证合法。
输出格式
  这个正则表达式能接受的最长字符串的长度。
样例输入
((xx|xxx)x|(x|xx))xx
样例输出
6
题目分析
处理好遇到每一种字符的情况即可。
#include <cstdio>
#include <iostream>
#include <stack>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
char s[10000];
int i = 0;
int num = 0;
int dfs() {
    int n = 0;
    int m = 0;
    char c = 'a';
    while (c != '\0') {
        c = s[i++];
        if (c == 'x')
            n++;
        else if (c == '|') {
            m = max(n, m);
            n = 0;
        }
        else if (c == ')') {
            m = max(n, m);
            return m;
        }
        else if (c == '(')
            n += dfs();
    }
    return max(n, m);
}
int main() {
    scanf("%s", s);
    num = dfs();
    cout << num;
    return 0;
}

 

转载于:https://www.cnblogs.com/woxiaosade/p/10494918.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值