蓝桥杯官网练习题(正则问题)

题目描述

考虑一种简单的正则表达式:

只由 x ( ) | 组成的正则表达式。

小明想求出这个正则表达式能接受的最长字符串的长度。

例如 ((xx|xxx)x|(x|xx))xx 能接受的最长字符串是: xxxxxx,长度是 6。

输入描述

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

输出描述

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

输入输出样例

示例
输入

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

输出

6

运行限制

java 复制代码
import java.util.Scanner;

public class Main {
  static int index=-1;
  static Scanner scan = new Scanner(System.in);
  static String str=scan.next();
    public static void main(String[] args) {
        System.out.println(dfs());
    }
    public static int dfs(){
      int max=Integer.MIN_VALUE;
      int count=0;
      while(index<str.length()-1){
        index++;
        if(str.charAt(index)=='('){
          count+=dfs();
        }
        else if(str.charAt(index)=='x'){
          count++;
        }
        else if(str.charAt(index)=='|'){
          max=Math.max(max,count);
          count=0;
        }
        else if(str.charAt(index)==')'){
          break;
        }
      }
      return Math.max(max,count);
    }
}
    • 最大运行时间:1s
    • 最大运行内存: 256M
相关推荐
躬身入世,以生证道6 小时前
面试技术栈 —— 简历篇
面试·职场和发展
Asmalin6 小时前
【代码随想录day 35】 力扣 01背包问题 一维
算法·leetcode·职场和发展
Miraitowa_cheems16 小时前
LeetCode算法日记 - Day 64: 岛屿的最大面积、被围绕的区域
java·算法·leetcode·决策树·职场和发展·深度优先·推荐算法
_不会dp不改名_17 小时前
leetcode_1382 将二叉搜索树变平衡树
算法·leetcode·职场和发展
旭意19 小时前
C++微基础备战蓝桥杯之数组篇10.1
开发语言·c++·蓝桥杯
墨染点香2 天前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展
旭意2 天前
C++微基础备战蓝桥杯string篇10.5
开发语言·c++·蓝桥杯
啊我不会诶2 天前
23ICPC澳门站补题
算法·深度优先·图论
黑色的山岗在沉睡2 天前
LeetCode 2761. 和等于目标值的质数对
算法·leetcode·职场和发展
T1an-12 天前
力扣70.爬楼梯
算法·leetcode·职场和发展