2017年蓝桥杯省赛A组第七题 正则问题

题目

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

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

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

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

例如,
输入:
((xx|xxx)x|(x|xx))xx  

程序应该输出:
6  

思路  括号表示优先计算,| 表示的是或

2017年蓝桥杯省赛A组第七题 正则问题_第1张图片

代码

#include
#include
#include
#include
using namespace std;
char s[1000];
int i=0;
int f()
{
    int maxn=0;
    int temp=0;
    while(imaxn) maxn=temp; // 统计遇到|时候x的最大值保存到maxn
            temp=0; // 从新计数
        }
        else{
            temp++;
            i++;
        }
    }
    if(temp>maxn) maxn=temp; // 每一次遇到右括号比较|之间的最大值将它保存在maxn
    return maxn;
}
int main()
{   scanf("%s",s);
    cout<

你可能感兴趣的:(蓝桥杯历年题解)