P3719 [AHOI2017初中组]rexp

P3719 [AHOI2017初中组]rexp
一开始想的是类似计算式子的值的东西,用栈。然后发现处理最大值很麻烦,因为处理的很像子过程,所以考虑递归来做。碰到'('就递归一次,
碰到'|'就取最大值再递归一次。
if(a=='(')
{
sum+=work(0);
}
要在
if(a==')')
{
return sum;
}
之后。
因为当递归返回时,a==')'
然后到
sum+=work(0);
}
下一步是
if(a==')')
{
return sum;
}
就回去了。
就没法往下读了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<cstring>
 8 #define inf 2147483647
 9 #define For(i,a,b) for(register int i=a;i<=b;i++)
10 #define p(a) putchar(a)
11 #define g() getchar()
12 //by war
13 //2017.10.23
14 using namespace std;
15 char a;
16 void in(int &x)
17 {
18     int y=1;
19     char c=g();x=0;
20     while(c<'0'||c>'9')
21     {
22     if(c=='-')
23     y=-1;
24     c=g();
25     }
26     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
27     x*=y;
28 }
29 void o(int x)
30 {
31     if(x<0)
32     {
33         p('-');
34         x=-x;
35     }
36     if(x>9)o(x/10);
37     p(x%10+'0');
38 }
39 
40 int work(int sum)
41 {
42     while(cin>>a)
43     {
44         
45     if(a==')') 
46     {
47     return sum;    
48     }
49     
50      if(a=='(')
51     {
52     sum+=work(0);    
53     }
54     
55     if(a=='a')
56     {
57     sum++;    
58     }
59     
60     
61     if(a=='|')
62     {
63     sum=max(sum,work(0));
64     break;    
65     }    
66     
67     
68     }
69     return sum;
70 }
71 
72 int main()
73 {
74   //  freopen("t.in","r",stdin);
75     o(work(0));
76      return 0;
77 }


原文地址:https://www.cnblogs.com/war1111/p/7715985.html