Wpp


私信TA

用户名:AbertW

访问量:3952

签 名:

等  级
排  名 842
经  验 3491
参赛次数 1
文章发表 8
年  龄 0
在职情况 学生
学  校 北京工商大学
专  业

  自我简介:

解题思路:

多重嵌套的括号与dfs递归处理很类似,处理过程:函数用count变量记录当前x长度,max_count保存遇到'|'

时的最大值,当遇到'('时,交给下一个函数递归解决;遇到')'时返回该层()内x的正则长度;遇到x时用count

累加,遇到'|'时用max_count暂时记录当前最大值,count重置0。

注意事项:

下标index全局更新。
参考代码:

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;

string str;
int index = 0,length;

int dfs()
{
	int count = 0, max_count = 0;
	for( ; index<length; index++ )
	{
		switch( str[index] )
		{
			case '(':
				index++; //+1交给下一轮处理 
				count += dfs(); 
				break;
			case ')':
				return max( max_count, count ); //该层()结束 
			case 'x':
				count++;
				break;
			case '|':
				max_count = max( max_count, count ); //暂存最大值 
				count = 0;
				break;
		}
	}
	return max( max_count, count );
} 

void solve()
{
	length = str.length();
	index = 0;
	printf("%d\n",dfs());	
} 

int main()
{
	cin>>str;
	
	solve();
	
	return 0;
}
 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

为什么显示编译错误哇
2022-03-13 21:03:18
  • «
  • 1
  • »