題目鏈接:
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;
}