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;
    }
  • 相关阅读:
    解决Xcode 7编译错误:does not contain bitcode
    iOS无处不在详解iOS集成第三方登录(SSO授权登录无需密码)
    iOS- 如何集成支付宝
    99.Recover Binary Search Tree
    101.Symmetric Tree
    108.Convert Sorted Array to Binary Search Tree
    242.Valid Anagram
    292.Nim Game
    872.Leaf-Similar Trees
    HDU-1390 Binary Numbers
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/12245141.html
Copyright © 2011-2022 走看看