大大出的题
大大经常吐槽没有人补,所以我决定做一个康康
A. APA of Orz Pandas
题意:给你一个包含+-*/%和()的表达式,让你把它转化成java里BigInteger的形式
大概就像这样 "a.add(b).remainder(M).multiply(d.substract(e.multiply(f)).add(g.divide(h))).multiply (BigInteger.ValueOf(233)) ... ..."
有意思的模拟题,不是很好写
首先要看清题,a+(b+c)应该输出为a.add(b.add(c))而不是a.add(b).add(c)
我之前看错题,使得这个题难度高了一点,但是那样也可以做,只需预处理无用括号即可
观察可以发现,不存在(a.add(b)).add(c)这种说法,也就是说,括号的本质作用是改变运算顺序,然后把括号里的表达式作为一个对象参与运算
那么就可以把括号里的东西看成一个变量,然后问题就转化为没有括号的表达式的处理
对于括号中的括号,可以把输出写成参数l和r的函数,功能为输出下标为[l,r]的子区间,然后递归处理
接下来的问题是找到输出序列中括号的包含范围,例如a+b*c要表示为a.add(b.multiply(c)),如何找到add右括号的位置
可以开一个栈
左括号可以直接压入
当压入右括号时,可以把左右括号中的所有运算符,包括括号全部湮灭
当压入+-时,可以把所有栈头的运算符湮灭,并令其右括号的位置在此运算符之前,但应在遇到左括号时停止
当压入*/%时,可以把栈头的*/%运算符湮灭,并令其右括号的位置在此运算符之前,但应遇到左括号或+-为止
代码:

1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<cmath>
6 using namespace std;
7 char fc[5][10]={
8 {'a','d','d',0},
9 {'s','u','b','s','t','r','a','c','t',0},
10 {'m','u','l','t','i','p','l','y',0},
11 {'d','i','v','i','d','e',0},
12 {'r','e','m','a','i','n','d','e','r',0}
13 };
14 int fs[256];
15 char s[11000]; int n;
16 char q[11000]; int p[11000],hd=0;
17 bool flg[11000];
18 int nxt[11000];
19 bool chck(char x){ return x=='+'||x=='-'||x=='*'||x=='/'||x=='%'||x=='('||x==')';}
20 bool chck1(char x){ return x=='+'||x=='-'||!x;}
21 bool chck2(char x){ return x=='*'||x=='/'||x=='%'||!x;}
22 void ot(int x,int y){
23 for(int i=x;i<=y;++i)if(s[i] && s[i]!='(' && s[i]!=')'){
24 if(!chck(s[i])) printf("%c",s[i]);
25 else{
26 printf(".");
27 for(int j=0;fc[fs[(int)s[i]]][j];++j) printf("%c",fc[fs[(int)s[i]]][j]);
28 printf("("); ot(i+1,nxt[i]); printf(")");
29 i=nxt[i];
30 }
31 }
32 }
33 void prvs(){
34 fs['+']=0,fs['-']=1,fs['*']=2,fs['/']=3,fs['%']=4;
35 hd=0;
36 for(int i=1;i<=n;++i) flg[i]=false;
37 s[0]=0,s[n+1]=0;
38 for(int i=1;i<=n;++i) nxt[i]=0;
39 }
40 int main(){
41 //freopen("ddd.in","r",stdin);
42 while(scanf("%s",s+1)!=EOF){
43 n=strlen(s+1); prvs();
44 for(int i=1;i<=n;++i)if(chck(s[i])){
45 if(s[i]==')'){
46 bool mk1=false,mk2=false;
47 for(;q[hd]!='(';--hd){
48 if(q[hd]=='+'||q[hd]=='-') mk1=true;
49 if(q[hd]=='*'||q[hd]=='/'||q[hd]=='%') mk2=true;
50 }
51 if((chck2(s[i+1])||chck2(s[p[hd]-1])) && !mk1){
52 if(!mk2) flg[i]=true,flg[p[hd]]=true;
53 if(s[p[hd]-1]!='/'&&s[p[hd]-1]!='%') flg[i]=true,flg[p[hd]]=true;
54 }
55 if(s[i+1]==')' && s[p[hd]-1]=='(')
56 flg[i]=true,flg[p[hd]]=true;
57 if(chck1(s[i+1]) && chck1(s[p[hd]-1]))
58 flg[i]=true,flg[p[hd]]=true;
59 nxt[p[hd]]=i;
60 --hd;
61 }
62 else q[++hd]=s[i]; p[hd]=i;
63 }
64 hd=0;
65 for(int i=1;i<=n;++i){
66 //if(flg[i]) s[i]=0;
67 if(s[i] && chck(s[i])){
68 if(s[i]=='(') q[++hd]=s[i],p[hd]=i;
69 if(s[i]==')'){
70 for(;q[hd]!='(';--hd) nxt[p[hd]]=i-1;
71 --hd;
72 }
73 if(chck1(s[i])){
74 for(;hd && q[hd]!='(';--hd) nxt[p[hd]]=i-1;
75 q[++hd]=s[i],p[hd]=i;
76 }
77 if(chck2(s[i])){
78 for(;hd && q[hd]!='(' && !chck1(q[hd]);--hd) nxt[p[hd]]=i-1;
79 q[++hd]=s[i],p[hd]=i;
80 }
81 }
82 }
83 for(;hd;--hd) nxt[p[hd]]=n;
84 /*for(int i=1;i<=n;++i) cout<<i<<" ";
85 cout<<endl;
86 for(int i=1;i<=n;++i) cout<<s[i]<<" ";
87 cout<<endl;
88 for(int i=1;i<=n;++i) cout<<nxt[i]<<" ";
89 cout<<endl;*/
90 ot(1,n); printf("\n");
91 }
92 return 0;
93 }
94
95
