Blue Bridge Cup Daily One Question 1.9 2017 Provincial Competition Group A 7. Regular Problem [DFS Recursive Imitation Stack]

Title description

http://oj.ecustacm.cn/problem.php?id=1321


  A simple regular expression: a regular expression consisting only of x () |.
  Xiao Ming wants to find the length of the longest string that this regular expression can accept.
  For example ((xx|xxx)x|(x|xx))xx The longest string that can be accepted is: xxxxxx, the length is 6


Explanation:
  Regular expressions, also known as regular expressions, are usually used to retrieve and replace text that meets a certain pattern (rule).
  For example, for the regular expression composed of x () | in the title, the parenthesis "()" has the highest priority, or the operation "|" takes the second place. Inside the brackets is a whole, or keep the longest one on both sides .
   ((xx|xxx)x|(x|xx))How is xx executed and why is it 6?
  Execute the parentheses first, and then execute the or. The steps are:
  (1) Look at the first parenthesis and find that there are nested parentheses inside. Find the innermost parenthesis. Inside the parentheses is an OR operation. ( (xx|xxx) x|(x|xx))xx, get: ( xxx x|(x|xx))xx
  (2) Continue to execute the innermost bracket. (xxxx| (x|xx) )xx, get: (xxxx| xx )xx
  (3) Continue to execute the last bracket. (xxxx|xx) xx, get: xxxx xx, end, get a string of length 6.
  Regular expression tutorial: https://www.runoob.com/regexp/regexp-tutorial.html , it comes with its own testing tool.

answer:

https://blog.csdn.net/weixin_43914593/article/details/112363933

Ni Wendi's words: "This problem is done with a recursive idea, and the code is quite concise. When you encounter a left bracket'(', go to recursive solution, when you encounter a right bracket')', the current recursion ends and the result is returned; |'then record and reset the current value, and then calculate and update to the right; if it is x, just count directly."

while(pos < len){

        if (s[pos] =='('){ //Left parenthesis, continue recursion. It is equivalent to stacking

            pos++;

            tmp += dfs();

        } else if (s[pos] ==')'){ //Close parenthesis, return recursively. Equal to pop

            pos++;

            break;

        } else if (s[pos] =='|'){ //check or

            pos++;

            ans = max (ans, tmp);

            tmp = 0;

        } else {//Check X and count the number of X

            pos++;

            tmp++;

        }

    }

    ans = max (ans, tmp);

    return ans; 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 110000;

string s;
int pos=0;//字符串当前指针

int dfs()
{
    int maxx=0;
    int ans=0;
    int len=s.size();
    while(pos<len)
    {
        if(s[pos]=='(')
        {
            pos++;
            maxx+=dfs();//模仿入栈,继续递归
        }
        else if(s[pos]==')')
        {
            pos++;
            break;//模仿出栈,结束循环
        }
        else if(s[pos]=='|')
        {
            pos++;
            ans=max(ans,maxx);//取得最大X,为了统计|之后的X的数量,maxx清零
            maxx=0;
        }
        else if(s[pos]=='x')
        {
            pos++;
            maxx++;//统计X个数
        }
    }
    ans=max(ans,maxx);
    return ans;//返回本层递归最大X个数
}

int main()
{
    cin>>s;
    cout<<dfs();
    return 0;
}


 

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/112984762