zoukankan      html  css  js  c++  java
  • [蓝桥杯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;
    }
  • 相关阅读:
    bit byte哪些事
    各种字符集和编码详解
    常用工具大全
    表单提交的3种方式,http post的contentType
    主动发电
    Hibernate与 MyBatis的比较
    storm简介
    2013年十大免费云空间排行榜-给开发者建站用的免费云主机
    中国著名讲师全录
    iptables只允许指定ip地址访问指定端口
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/12245141.html
Copyright © 2011-2022 走看看