蓝桥杯 PREV-35 正则问题

题目链接:

PREV-35 正则问题

思路:

题目不解释一下压根不知道x () |这些符号是做什么的…
简单解释:
x就是代表一个字符,题目要求最长字符数;
()就是起到计算中的优先级作用;
|代表或,既然取最长我们就需要找到或运算左右最长的字符;
举例:
1.xxx|xx就是3;
2.(xxx)xx是5;
3.(xxx|x)xx也是5;
知道意思之后使用递归即可求得最长字符串;

代码:

#include<bits/stdc++.h>

using namespace std;

int p;
char s[105];
int dfs() {
    
    
	int best = 0, cnt = 0;
	for(; p < strlen(s); p++) {
    
    
		if(s[p] == ')') return max(best, cnt);
		if(s[p] == '(') {
    
     ++p; cnt += dfs(); continue; }
		if(s[p] == '|') best = max(best, cnt), cnt = 0;
		else ++cnt;
		
	}
	return max(best, cnt);
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	scanf("%s", s);
	cout << dfs();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104562839