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

编程入门 行业动态 更新时间:2024-10-25 15:29:14

蓝桥杯官网<a href=https://www.elefans.com/category/jswz/34/1768594.html style=练习题(正则问题)"/>

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

题目描述

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

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

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

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

输入描述

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

输出描述

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

输入输出样例

示例
输入

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

输出

6

运行限制

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

更多推荐

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

本文发布于:2023-11-16 16:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:练习题   正则   官网   蓝桥杯

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!