• [蓝桥杯2017初赛]正则问题


    题目链接:http://oj.ecustacm.cn/problem.php?id=1321

    题目描述

    考虑一种简单的正则表达式:只由 x ( ) | 组成的正则表达式。
    小明想求出这个正则表达式能接受的最长字符串的长度。  
    例如 ((xx|xxx)x|(x|xx))xx 能接受的最长字符串是: xxxxxx,长度是6

    输入

    输入一个由x()|组成的正则表达式。输入长度不超过100,保证合法。  

    输出

    输出这个正则表达式能接受的最长字符串的长度。  

    样例输入 Copy

    ((xx|xxx)x|(x|xx))xx  

    样例输出 Copy

    6

    这题目的描述有点不清楚,这里放一张图就懂题意了

     思路:

    从前向后遍历。遇到(之后,进入dfs,遇到)之后跳出dfs,遇到x,统计x的数字。遇到 | 就比较大小。

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <string.h>
    #include <vector>
    #include <map>
    #include <stack>
    #include <set>
    #include <queue>
    #include <math.h>
    #include <cstdio>
    #include <iomanip>
    #include <time.h>
     
    #define LL long long
    #define INF 0x3f3f3f3f
    #define ls nod<<1
    #define rs (nod<<1)+1
     
    const int maxn = 1e9 + 10 ;
    const LL mod = 20010905;
     
    std::string s;
    int i;
     
    int dfs() {
        int ans = 0;
        int cnt = 0;
        int len = s.length();
        while (i < len) {
            if (s[i] == '(') {
                i++;
                cnt += dfs();
            }
            else if (s[i] == ')') {
                i++;
                break;
            }
            else if (s[i] == '|') {
                i++;
                ans = std::max(ans,cnt);
                cnt = 0;
            }
            else {
                i++;
                cnt++;
            }
        }
        return std::max(ans,cnt);;
    }
     
     
    int main() {
        std::cin >> s;
        printf("%d
    ",dfs());
        return 0;
    }
  • 相关阅读:
    iOS开发之Masonry框架源码解析
    iOS开发针对对Masonry下的FPS优化讨论
    React-native Android环境搭建
    Android中ListView使用总结
    Android开发布局方式
    轮播图
    大文件断点下载
    基于第三方库FMDB的数据库的二次封装
    md5加密
    AssignToObject文件(字典转模型、字典数组转模型数组)
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/12245141.html
Copyright © 2020-2023  润新知