第一场rank387 题解
A. 阿里的新游戏
1A
1 #include <bits/stdc++.h> 2 using namespace std; 3 int x[100],y[100]; 4 int n,m; 5 bool find(int a,int b) 6 { 7 for(int i=0;i<n;i++) if(x[i]==a && y[i]==b) return true; 8 return false; 9 } 10 int main() 11 { 12 while(scanf("%d%d",&n,&m)==2) { 13 for(int i=0;i<n;i++) scanf("%d%d",x+i,y+i); 14 for(int j=0;j<m;j++) scanf("%*d%*d"); 15 int ans=0; 16 for(int i=0;i<n;i++) 17 { 18 for(int j=1;j<=3;j++) 19 { 20 if(find(x[i]+j,y[i]) && find(x[i]+j+j,y[i]) && !(j==2 && y[i]==0)) ans++; 21 if(find(x[i],y[i]+j) && find(x[i],y[i]+j+j) && !(j==2 && x[i]==0)) ans++; 22 } 23 } 24 printf("%d ",ans); 25 } 26 return 0; 27 }
B. 阿里天池的新任务(简单)
打错模板T了3发
2017 计蒜之道 初赛 第一场 B. 阿里天池的新任务(简单)
第二场rank409 题解
A. 百度的年会游戏
细节没注意WA了4发
1 //WA1 2 #include <bits/stdc++.h> 3 using namespace std; 4 int main() 5 { 6 int a[4]; 7 int k,d; 8 while(scanf("%d%d%d%d",a+1,a+2,a+3,a+0)==4) 9 { 10 scanf("%d%d",&k,&d); 11 if(a[k%4]==d) 12 { 13 puts("0"); 14 continue; 15 } 16 int ans=0; 17 for(int i=1;i<=6;i++) 18 { 19 for(int j=i;j<=6;j++) 20 { 21 int b=(i+j)%4;//num user 22 int c=i+1; 23 for(int l=b,t=3;t;t--) 24 { 25 if(l==k && c==d) 26 { 27 ans++; 28 break; 29 } 30 c+=8; 31 if(c>a[l]) c-=a[l],l=(l+3)%4; 32 } 33 } 34 } 35 printf("%d ",ans); 36 } 37 return 0; 38 } 39 //WA2 40 #include <bits/stdc++.h> 41 using namespace std; 42 int main() 43 { 44 int a[4]; 45 int k,d; 46 while(scanf("%d%d%d%d",a+1,a+2,a+3,a+0)==4) 47 { 48 scanf("%d%d",&k,&d); 49 if(a[k%4]==d) 50 { 51 puts("0"); 52 continue; 53 } 54 int ans=0; 55 for(int i=1;i<=6;i++) 56 { 57 for(int j=i;j<=6;j++) 58 { 59 int b=(i+j)%4;//num user 60 int c=i+1; 61 for(int l=b,t=3;t;t--) 62 { 63 if(l==k && c==d) 64 { 65 ans++; 66 break; 67 } 68 c+=8; 69 while(c>a[l]) c-=a[l],l=(l+3)%4; 70 } 71 } 72 } 73 printf("%d ",ans); 74 } 75 return 0; 76 } 77 //WA3 78 #include <bits/stdc++.h> 79 using namespace std; 80 int main() 81 { 82 int a[4]; 83 int k,d; 84 while(scanf("%d%d%d%d",a+1,a+2,a+3,a+0)==4) 85 { 86 scanf("%d%d",&k,&d); 87 int ans=0; 88 for(int i=1;i<=6;i++) 89 { 90 for(int j=i;j<=6;j++) 91 { 92 int b=(i+j)%4;//num user 93 int c=i+1; 94 for(int l=b,t=3;t;t--) 95 { 96 if(l==k && c==d) 97 { 98 ans++; 99 break; 100 } 101 c+=8; 102 while(c>a[l]) 103 { 104 c-=a[l]; 105 l=(l+3)%4; 106 } 107 } 108 } 109 } 110 printf("%d ",ans); 111 } 112 return 0; 113 } 114 //WA4 115 #include <bits/stdc++.h> 116 using namespace std; 117 int main() 118 { 119 int a[4]; 120 int k,d; 121 while(scanf("%d%d%d%d",a+1,a+2,a+3,a+0)==4) 122 { 123 scanf("%d%d",&k,&d); 124 k%=4; 125 int ans=0; 126 for(int i=1;i<=6;i++) 127 { 128 for(int j=i;j<=6;j++) 129 { 130 int b=(i+j)%4;//num user 131 int c=i+1; 132 for(int l=b,t=3;t;t--) 133 { 134 if(l==k && c==d) 135 { 136 ans++; 137 break; 138 } 139 c+=8; 140 while(c>a[l]) 141 { 142 c-=a[l]; 143 l=(l+3)%4; 144 } 145 } 146 } 147 } 148 printf("%d ",ans); 149 } 150 return 0; 151 } 152 //AC 153 #include <bits/stdc++.h> 154 using namespace std; 155 int main() 156 { 157 int a[4]; 158 int k,d; 159 while(scanf("%d%d%d%d",a+1,a+2,a+3,a+0)==4) 160 { 161 scanf("%d%d",&k,&d); 162 k%=4; 163 int ans=0; 164 for(int i=1;i<=6;i++) 165 { 166 for(int j=i;j<=6;j++) 167 { 168 int b=(i+j)%4;//num user 169 int c=i+1; 170 for(int l=b,t=3;t;t--) 171 { 172 while(c>a[l]) 173 { 174 c-=a[l]; 175 l=(l+3)%4; 176 } 177 if(l==k && c==d) 178 { 179 ans++; 180 break; 181 } 182 c+=8; 183 } 184 } 185 } 186 printf("%d ",ans); 187 } 188 return 0; 189 }
B.百度的科学计算器(简单)
赛后:没注意(-的情况RE了一发,听说场内很多用py eval过的b,c
1 //赛后WA1 2 #include <bits/stdc++.h> 3 using namespace std; 4 map<string,int>op; 5 map<string,string>func; 6 const int N=1e3+10; 7 char s[N],tf1[N],tf2[N]; 8 string getNum(char *s,int &i) 9 { 10 string res=""; 11 while(isdigit(s[i])) res+=s[i],i++; 12 if(s[i]=='.') res+=s[i],i++; 13 else return res; 14 while(isdigit(s[i])) res+=s[i],i++; 15 return res; 16 } 17 string getOp(char *s,int &i) 18 { 19 string res=""; 20 while(isalnum(s[i])) res+=s[i],i++; 21 res+=s[i],i++; 22 return res; 23 } 24 string solve(char *s) 25 { 26 stack<string>ops; 27 stack<string>num; 28 ops.push("#"); 29 int i=0; 30 while(s[i]!='#' || ops.top()!="#") 31 { 32 //printf("%c ",s[i]); 33 if(isdigit(s[i])) 34 { 35 num.push(getNum(s,i)); 36 //cout<<"numtop:"<<num.top()<<endl; 37 continue; 38 } 39 string oper=getOp(s,i); 40 if(ops.top()=="(" && oper==")") 41 { 42 ops.pop(); 43 continue; 44 } 45 if(oper=="(" || op[ops.top()]<op[oper]) 46 { 47 ops.push(oper); 48 continue; 49 } 50 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 51 //cout<<"top"<<num.top()<<endl; 52 string num1=num.top();num.pop(); 53 string num2=num.top();num.pop(); 54 string ope=ops.top();ops.pop(); 55 char num3[100]; 56 if(num1.find('.')==-1 && num2.find('.')==-1) 57 { 58 long long a,b; 59 b=atoi(num1.c_str()); 60 a=atoi(num2.c_str()); 61 //cout<<a<<ope<<b<<endl; 62 long long ans; 63 if(ope=="+") ans=a+b; 64 else if(ope=="-") ans=a-b; 65 else if(ope=="*") ans=a*b; 66 else if(ope=="/") ans=a/b; 67 sprintf(num3,"%lld",ans); 68 } 69 else 70 { 71 double a,b,ans; 72 b=atof(num1.c_str()); 73 a=atof(num2.c_str()); 74 //cout<<a<<ope<<b<<endl; 75 if(ope=="+") ans=a+b; 76 else if(ope=="-") ans=a-b; 77 else if(ope=="*") ans=a*b; 78 else if(ope=="/") ans=a/b; 79 sprintf(num3,"%.6f",ans); 80 } 81 //cout<<"num3:"<<num3<<endl; 82 num.push(num3); 83 } 84 return num.top(); 85 } 86 int main() 87 { 88 op["#"]=0; 89 op["("]=1; 90 op["+"]=2; 91 op["-"]=2; 92 op["*"]=3; 93 op["/"]=3; 94 op[")"]=1; 95 op["int("]=1; 96 op["float("]=1; 97 int n; 98 while(scanf("%d",&n)==1) 99 { 100 for(int i=0;i<n;i++) 101 { 102 scanf("%[^=]=%s",tf1,tf2); 103 tf2[strlen(tf2)]='#'; 104 func[tf1]=tf2; 105 } 106 scanf("%s",s); 107 s[strlen(s)]='#'; 108 string ans=solve(s); 109 cout<<ans<<endl; 110 } 111 return 0; 112 } 113 //赛后AC 114 #include <bits/stdc++.h> 115 using namespace std; 116 map<string,int>op; 117 map<string,string>func; 118 const int N=1e3+10; 119 char s[N],tf1[N],tf2[N]; 120 string getNum(char *s,int &i) 121 { 122 string res=""; 123 while(isdigit(s[i])) res+=s[i],i++; 124 if(s[i]=='.') res+=s[i],i++; 125 else return res; 126 while(isdigit(s[i])) res+=s[i],i++; 127 return res; 128 } 129 string getOp(char *s,int &i) 130 { 131 string res=""; 132 while(isalnum(s[i])) res+=s[i],i++; 133 res+=s[i],i++; 134 return res; 135 } 136 string solve(char *s) 137 { 138 stack<string>ops; 139 stack<string>num; 140 ops.push("#"); 141 int i=0; 142 string neg=""; 143 if(s[i]=='-') neg="-",i++; 144 while(s[i]!='#' || ops.top()!="#") 145 { 146 //printf("%c ",s[i]); 147 if(isdigit(s[i])) 148 { 149 num.push(neg+getNum(s,i)); 150 neg=""; 151 //cout<<"numtop:"<<num.top()<<endl; 152 continue; 153 } 154 string oper=getOp(s,i); 155 if(ops.top()=="(" && oper==")") 156 { 157 ops.pop(); 158 continue; 159 } 160 if(oper=="(" || op[ops.top()]<op[oper]) 161 { 162 ops.push(oper); 163 if(oper=="(") 164 { 165 if(s[i]=='-') neg="-",i++; 166 } 167 continue; 168 } 169 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 170 //cout<<"top"<<num.top()<<endl; 171 string num1=num.top();num.pop(); 172 string ope=ops.top();ops.pop(); 173 string num2=num.top();num.pop(); 174 char num3[100]; 175 if(num1.find('.')==-1 && num2.find('.')==-1) 176 { 177 long long a,b; 178 sscanf(num1.c_str(),"%lld",&b); 179 sscanf(num2.c_str(),"%lld",&a); 180 //cout<<a<<ope<<b<<endl; 181 long long ans; 182 if(ope=="+") ans=a+b; 183 else if(ope=="-") ans=a-b; 184 else if(ope=="*") ans=a*b; 185 else if(ope=="/") ans=a/b; 186 sprintf(num3,"%lld",ans); 187 } 188 else 189 { 190 double a,b,ans; 191 sscanf(num1.c_str(),"%lf",&b); 192 sscanf(num2.c_str(),"%lf",&a); 193 //cout<<a<<ope<<b<<endl; 194 if(ope=="+") ans=a+b; 195 else if(ope=="-") ans=a-b; 196 else if(ope=="*") ans=a*b; 197 else if(ope=="/") ans=a/b; 198 sprintf(num3,"%.6f",ans); 199 } 200 //cout<<"num3:"<<num3<<endl; 201 num.push(num3); 202 } 203 return num.top(); 204 } 205 int main() 206 { 207 op["#"]=0; 208 op["("]=1; 209 op["+"]=2; 210 op["-"]=2; 211 op["*"]=3; 212 op["/"]=3; 213 op[")"]=1; 214 op["int("]=1; 215 op["float("]=1; 216 int n; 217 while(scanf("%d",&n)==1) 218 { 219 for(int i=0;i<n;i++) 220 { 221 scanf("%[^=]=%s",tf1,tf2); 222 tf2[strlen(tf2)]='#'; 223 func[tf1]=tf2; 224 } 225 scanf("%s",s); 226 s[strlen(s)]='#'; 227 string ans=solve(s); 228 cout<<ans<<endl; 229 } 230 return 0; 231 }
C.百度的科学计算器(中等)
赛后:没注意func(x)=x+x+x+1的情况,只考虑func(x)=x+1 RE了4发
1 //RE1 2 #include <bits/stdc++.h> 3 using namespace std; 4 map<string,int>op; 5 map<string,string>func; 6 const int N=1e3+10; 7 char s[N],tf1[N],tf2[N]; 8 string getNum(const char *s,int &i) 9 { 10 string res=""; 11 while(isdigit(s[i])) res+=s[i],i++; 12 if(s[i]=='.') res+=s[i],i++; 13 else return res; 14 while(isdigit(s[i])) res+=s[i],i++; 15 return res; 16 } 17 string getOp(const char *s,int &i) 18 { 19 string res=""; 20 while(isalnum(s[i])) res+=s[i],i++; 21 res+=s[i],i++; 22 return res; 23 } 24 string solve(char *s) 25 { 26 stack<string>ops; 27 stack<string>num; 28 ops.push("#"); 29 int i=0; 30 string neg=""; 31 if(s[i]=='-') neg="-",i++; 32 while(s[i]!='#' || ops.top()!="#") 33 { 34 //printf("%c ",s[i]); 35 if(isdigit(s[i])) 36 { 37 num.push(neg+getNum(s,i)); 38 neg=""; 39 //cout<<"numtop:"<<num.top()<<endl; 40 continue; 41 } 42 string oper=getOp(s,i); 43 if(ops.top().find('(')!=-1 && oper==")") 44 { 45 string ope=ops.top(); 46 if(ope=="int(") 47 { 48 string num1=num.top();num.pop(); 49 int idx; 50 if((idx=num1.find('.'))!=-1) 51 { 52 num1=num1.substr(0,idx); 53 } 54 num.push(num1); 55 } 56 else if(ope=="float(") 57 { 58 string num1=num.top();num.pop(); 59 double a; 60 sscanf(num1.c_str(),"%lf",&a); 61 char tmp[20]; 62 sprintf(tmp,"%.6f",a); 63 num.push(tmp); 64 } 65 else if(ope!="(") 66 { 67 string num1=num.top();num.pop(); 68 string exp=func[ope]; 69 string x=exp.substr(0,exp.find(')')); 70 exp=exp.substr(exp.find('=')+1); 71 string tmp=exp.substr(0,exp.find(x)); 72 tmp+=num1; 73 tmp+=exp.substr(exp.find(x)+x.length()); 74 //cout<<ope<<" "<<exp<<" "<<num1<<" "<<x<<" "<<tmp<<endl; 75 char *exps=new char[N]; 76 strcpy(exps,tmp.c_str()); 77 num.push(solve(exps)); 78 } 79 ops.pop(); 80 continue; 81 } 82 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 83 { 84 ops.push(oper); 85 if(oper.find('(')!=-1) 86 { 87 if(s[i]=='-') neg="-",i++; 88 } 89 continue; 90 } 91 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 92 //cout<<"top"<<num.top()<<endl; 93 string num1=num.top();num.pop(); 94 string ope=ops.top();ops.pop(); 95 string num2=num.top();num.pop(); 96 char num3[100]; 97 if(num1.find('.')==-1 && num2.find('.')==-1) 98 { 99 long long a,b; 100 sscanf(num1.c_str(),"%lld",&b); 101 sscanf(num2.c_str(),"%lld",&a); 102 //cout<<a<<ope<<b<<endl; 103 long long ans; 104 if(ope=="+") ans=a+b; 105 else if(ope=="-") ans=a-b; 106 else if(ope=="*") ans=a*b; 107 else if(ope=="/") ans=a/b; 108 sprintf(num3,"%lld",ans); 109 } 110 else 111 { 112 double a,b,ans; 113 sscanf(num1.c_str(),"%lf",&b); 114 sscanf(num2.c_str(),"%lf",&a); 115 //cout<<a<<ope<<b<<endl; 116 if(ope=="+") ans=a+b; 117 else if(ope=="-") ans=a-b; 118 else if(ope=="*") ans=a*b; 119 else if(ope=="/") ans=a/b; 120 sprintf(num3,"%.6f",ans); 121 } 122 //cout<<"num3:"<<num3<<endl; 123 num.push(num3); 124 } 125 return num.top(); 126 } 127 int main() 128 { 129 op["#"]=0; 130 op["("]=1; 131 op["+"]=2; 132 op["-"]=2; 133 op["*"]=3; 134 op["/"]=3; 135 op[")"]=1; 136 op["int("]=1; 137 op["float("]=1; 138 int n; 139 while(scanf("%d",&n)==1) 140 { 141 for(int i=0;i<n;i++) 142 { 143 getchar(); 144 scanf("%[^(](%s",tf1,tf2); 145 func[string(tf1)+"("]=string(tf2)+"#"; 146 } 147 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 148 scanf("%s",s); 149 s[strlen(s)]='#'; 150 string ans=solve(s); 151 cout<<ans<<endl; 152 } 153 return 0; 154 } 155 //RE2 156 157 #include <bits/stdc++.h> 158 using namespace std; 159 map<string,int>op; 160 map<string,string>func; 161 const int N=1e3+10; 162 char s[N],tf1[N],tf2[N]; 163 string getNum(const char *s,int &i) 164 { 165 string res=""; 166 while(isdigit(s[i])) res+=s[i],i++; 167 if(s[i]=='.') res+=s[i],i++; 168 else return res; 169 while(isdigit(s[i])) res+=s[i],i++; 170 return res; 171 } 172 string getOp(const char *s,int &i) 173 { 174 string res=""; 175 while(isalnum(s[i])) res+=s[i],i++; 176 res+=s[i],i++; 177 return res; 178 } 179 string solve(char *s) 180 { 181 stack<string>ops; 182 stack<string>num; 183 ops.push("#"); 184 int i=0; 185 string neg=""; 186 if(s[i]=='-') neg="-",i++; 187 while(s[i]!='#' || ops.top()!="#") 188 { 189 //printf("%c ",s[i]); 190 if(isdigit(s[i])) 191 { 192 num.push(neg+getNum(s,i)); 193 neg=""; 194 //cout<<"numtop:"<<num.top()<<endl; 195 continue; 196 } 197 string oper=getOp(s,i); 198 if(ops.top().find('(')!=-1 && oper==")") 199 { 200 string ope=ops.top(); 201 if(ope=="int(") 202 { 203 string num1=num.top();num.pop(); 204 int idx; 205 if((idx=num1.find('.'))!=-1) 206 { 207 num1=num1.substr(0,idx); 208 } 209 num.push(num1); 210 } 211 else if(ope=="float(") 212 { 213 string num1=num.top();num.pop(); 214 double a; 215 sscanf(num1.c_str(),"%lf",&a); 216 char tmp[20]; 217 sprintf(tmp,"%.6f",a); 218 num.push(tmp); 219 } 220 else if(ope!="(") 221 { 222 string num1=num.top();num.pop(); 223 string exp=func[ope]; 224 string x=exp.substr(0,exp.find(')')); 225 exp=exp.substr(exp.find('=')+1); 226 string tmp=exp.substr(0,exp.find(x)); 227 tmp+="("+num1+")"; 228 tmp+=exp.substr(exp.find(x)+x.length()); 229 //cout<<ope<<" "<<exp<<" "<<num1<<" "<<x<<" "<<tmp<<endl; 230 char *exps=new char[N]; 231 strcpy(exps,tmp.c_str()); 232 num.push(solve(exps)); 233 } 234 ops.pop(); 235 continue; 236 } 237 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 238 { 239 ops.push(oper); 240 if(oper.find('(')!=-1) 241 { 242 if(s[i]=='-') neg="-",i++; 243 } 244 continue; 245 } 246 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 247 //cout<<"top"<<num.top()<<endl; 248 string num1=num.top();num.pop(); 249 string ope=ops.top();ops.pop(); 250 string num2=num.top();num.pop(); 251 char num3[100]; 252 if(num1.find('.')==-1 && num2.find('.')==-1) 253 { 254 long long a,b; 255 sscanf(num1.c_str(),"%lld",&b); 256 sscanf(num2.c_str(),"%lld",&a); 257 //cout<<a<<ope<<b<<endl; 258 long long ans; 259 if(ope=="+") ans=a+b; 260 else if(ope=="-") ans=a-b; 261 else if(ope=="*") ans=a*b; 262 else if(ope=="/") ans=a/b; 263 sprintf(num3,"%lld",ans); 264 } 265 else 266 { 267 double a,b,ans; 268 sscanf(num1.c_str(),"%lf",&b); 269 sscanf(num2.c_str(),"%lf",&a); 270 //cout<<a<<ope<<b<<endl; 271 if(ope=="+") ans=a+b; 272 else if(ope=="-") ans=a-b; 273 else if(ope=="*") ans=a*b; 274 else if(ope=="/") ans=a/b; 275 sprintf(num3,"%.6f",ans); 276 } 277 //cout<<"num3:"<<num3<<endl; 278 num.push(num3); 279 } 280 return num.top(); 281 } 282 int main() 283 { 284 op["#"]=0; 285 op["("]=1; 286 op["+"]=2; 287 op["-"]=2; 288 op["*"]=3; 289 op["/"]=3; 290 op[")"]=1; 291 op["int("]=1; 292 op["float("]=1; 293 int n; 294 while(scanf("%d",&n)==1) 295 { 296 for(int i=0;i<n;i++) 297 { 298 getchar(); 299 scanf("%[^(](%s",tf1,tf2); 300 func[string(tf1)+"("]=string(tf2)+"#"; 301 } 302 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 303 scanf("%s",s); 304 s[strlen(s)]='#'; 305 string ans=solve(s); 306 cout<<ans<<endl; 307 } 308 return 0; 309 } 310 //RE3 311 312 #include <bits/stdc++.h> 313 using namespace std; 314 map<string,int>op; 315 map<string,string>func; 316 const int N=1e3+10; 317 char s[N],tf1[N],tf2[N]; 318 string getNum(const char *s,int &i) 319 { 320 string res=""; 321 while(isdigit(s[i])) res+=s[i],i++; 322 if(s[i]=='.') res+=s[i],i++; 323 else return res; 324 while(isdigit(s[i])) res+=s[i],i++; 325 return res; 326 } 327 string getOp(const char *s,int &i) 328 { 329 string res=""; 330 while(isalnum(s[i])) res+=s[i],i++; 331 res+=s[i],i++; 332 return res; 333 } 334 string solve(char *s) 335 { 336 stack<string>ops; 337 stack<string>num; 338 ops.push("#"); 339 int i=0; 340 string neg=""; 341 if(s[i]=='-') neg="-",i++; 342 while(s[i]!='#' || ops.top()!="#") 343 { 344 //printf("%c ",s[i]); 345 if(isdigit(s[i])) 346 { 347 num.push(neg+getNum(s,i)); 348 neg=""; 349 //cout<<"numtop:"<<num.top()<<endl; 350 continue; 351 } 352 string oper=getOp(s,i); 353 if(ops.top().find('(')!=-1 && oper==")") 354 { 355 string ope=ops.top(); 356 ops.pop(); 357 if(ope=="int(") 358 { 359 string num1=num.top();num.pop(); 360 int idx; 361 if((idx=num1.find('.'))!=-1) 362 { 363 num1=num1.substr(0,idx); 364 } 365 num.push(num1); 366 } 367 else if(ope=="float(") 368 { 369 string num1=num.top();num.pop(); 370 double a; 371 sscanf(num1.c_str(),"%lf",&a); 372 char tmp[20]; 373 sprintf(tmp,"%.6f",a); 374 num.push(tmp); 375 } 376 else if(ope!="(") 377 { 378 string num1=num.top();num.pop(); 379 string exp=func[ope]; 380 string x=exp.substr(0,exp.find(')')); 381 exp=exp.substr(exp.find('=')+1); 382 string tmp=exp.substr(0,exp.find(x)); 383 tmp+="("+num1+")"; 384 tmp+=exp.substr(exp.find(x)+x.length()); 385 //cout<<ope<<" "<<exp<<" "<<num1<<" "<<x<<" "<<tmp<<endl; 386 char *exps=new char[N]; 387 strcpy(exps,tmp.c_str()); 388 num.push(solve(exps)); 389 } 390 continue; 391 } 392 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 393 { 394 ops.push(oper); 395 if(oper.find('(')!=-1) 396 { 397 if(s[i]=='-') neg="-",i++; 398 } 399 continue; 400 } 401 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 402 //cout<<"top"<<num.top()<<endl; 403 string num1=num.top();num.pop(); 404 string ope=ops.top();ops.pop(); 405 string num2=num.top();num.pop(); 406 char num3[100]; 407 if(num1.find('.')==-1 && num2.find('.')==-1) 408 { 409 long long a,b; 410 sscanf(num1.c_str(),"%lld",&b); 411 sscanf(num2.c_str(),"%lld",&a); 412 //cout<<a<<ope<<b<<endl; 413 long long ans; 414 if(ope=="+") ans=a+b; 415 else if(ope=="-") ans=a-b; 416 else if(ope=="*") ans=a*b; 417 else if(ope=="/") ans=a/b; 418 sprintf(num3,"%lld",ans); 419 } 420 else 421 { 422 double a,b,ans; 423 sscanf(num1.c_str(),"%lf",&b); 424 sscanf(num2.c_str(),"%lf",&a); 425 //cout<<a<<ope<<b<<endl; 426 if(ope=="+") ans=a+b; 427 else if(ope=="-") ans=a-b; 428 else if(ope=="*") ans=a*b; 429 else if(ope=="/") ans=a/b; 430 sprintf(num3,"%.6f",ans); 431 } 432 //cout<<"num3:"<<num3<<endl; 433 num.push(num3); 434 } 435 return num.top(); 436 } 437 int main() 438 { 439 op["#"]=0; 440 op["("]=1; 441 op["+"]=2; 442 op["-"]=2; 443 op["*"]=3; 444 op["/"]=3; 445 op[")"]=1; 446 op["int("]=1; 447 op["float("]=1; 448 int n; 449 while(scanf("%d",&n)==1) 450 { 451 for(int i=0;i<n;i++) 452 { 453 getchar(); 454 scanf("%[^(](%s",tf1,tf2); 455 func[string(tf1)+"("]=string(tf2)+"#"; 456 } 457 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 458 scanf("%s",s); 459 s[strlen(s)]='#'; 460 string ans=solve(s); 461 cout<<ans<<endl; 462 } 463 return 0; 464 } 465 //RE4 466 467 #include <bits/stdc++.h> 468 using namespace std; 469 map<string,int>op; 470 map<string,string>func; 471 const int N=1e3+10; 472 char s[N],tf1[N],tf2[N]; 473 string getNum(const char *s,int &i) 474 { 475 string res=""; 476 while(isdigit(s[i])) res+=s[i],i++; 477 if(s[i]=='.') res+=s[i],i++; 478 else return res; 479 while(isdigit(s[i])) res+=s[i],i++; 480 return res; 481 } 482 string getOp(const char *s,int &i) 483 { 484 string res=""; 485 while(isalnum(s[i])) res+=s[i],i++; 486 res+=s[i],i++; 487 return res; 488 } 489 string solve(char *s) 490 { 491 stack<string>ops; 492 stack<string>num; 493 ops.push("#"); 494 int i=0; 495 string neg=""; 496 if(s[i]=='-') neg="-",i++; 497 while(s[i]!='#' || ops.top()!="#") 498 { 499 //printf("%c ",s[i]); 500 if(isdigit(s[i])) 501 { 502 num.push(neg+getNum(s,i)); 503 neg=""; 504 //cout<<"numtop:"<<num.top()<<endl; 505 continue; 506 } 507 string oper=getOp(s,i); 508 if(ops.top().find('(')!=-1 && oper==")") 509 { 510 string ope=ops.top(); 511 ops.pop(); 512 if(ope=="int(") 513 { 514 string num1=num.top();num.pop(); 515 int idx; 516 if((idx=num1.find('.'))!=-1) 517 { 518 num1=num1.substr(0,idx); 519 } 520 num.push(num1); 521 } 522 else if(ope=="float(") 523 { 524 string num1=num.top();num.pop(); 525 double a; 526 sscanf(num1.c_str(),"%lf",&a); 527 char tmp[20]; 528 sprintf(tmp,"%.6f",a); 529 num.push(tmp); 530 } 531 else if(ope!="(") 532 { 533 string num1=num.top();num.pop(); 534 string exp=func[ope]; 535 string x=exp.substr(0,exp.find(')')); 536 exp=exp.substr(exp.find('=')+1); 537 string tmp; 538 if(exp.find(x)!=-1) 539 { 540 tmp=exp.substr(0,exp.find(x)); 541 tmp+="("+num1+")"; 542 tmp+=exp.substr(exp.find(x)+x.length()); 543 } 544 else tmp=exp; 545 //cout<<ope<<" "<<exp<<" "<<num1<<" "<<x<<" "<<tmp<<endl; 546 char *exps=new char[N]; 547 strcpy(exps,tmp.c_str()); 548 num.push(solve(exps)); 549 } 550 continue; 551 } 552 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 553 { 554 ops.push(oper); 555 if(oper.find('(')!=-1) 556 { 557 if(s[i]=='-') neg="-",i++; 558 } 559 continue; 560 } 561 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 562 //cout<<"top"<<num.top()<<endl; 563 string num1=num.top();num.pop(); 564 string ope=ops.top();ops.pop(); 565 string num2=num.top();num.pop(); 566 char num3[100]; 567 if(num1.find('.')==-1 && num2.find('.')==-1) 568 { 569 long long a,b; 570 sscanf(num1.c_str(),"%lld",&b); 571 sscanf(num2.c_str(),"%lld",&a); 572 //cout<<a<<ope<<b<<endl; 573 long long ans; 574 if(ope=="+") ans=a+b; 575 else if(ope=="-") ans=a-b; 576 else if(ope=="*") ans=a*b; 577 else if(ope=="/") ans=a/b; 578 sprintf(num3,"%lld",ans); 579 } 580 else 581 { 582 double a,b,ans; 583 sscanf(num1.c_str(),"%lf",&b); 584 sscanf(num2.c_str(),"%lf",&a); 585 //cout<<a<<ope<<b<<endl; 586 if(ope=="+") ans=a+b; 587 else if(ope=="-") ans=a-b; 588 else if(ope=="*") ans=a*b; 589 else if(ope=="/") ans=a/b; 590 sprintf(num3,"%.6f",ans); 591 } 592 //cout<<"num3:"<<num3<<endl; 593 num.push(num3); 594 } 595 return num.top(); 596 } 597 int main() 598 { 599 op["#"]=0; 600 op["("]=1; 601 op["+"]=2; 602 op["-"]=2; 603 op["*"]=3; 604 op["/"]=3; 605 op[")"]=1; 606 op["int("]=1; 607 op["float("]=1; 608 int n; 609 while(scanf("%d",&n)==1) 610 { 611 for(int i=0;i<n;i++) 612 { 613 getchar(); 614 scanf("%[^(](%s",tf1,tf2); 615 func[string(tf1)+"("]=string(tf2)+"#"; 616 } 617 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 618 scanf("%s",s); 619 s[strlen(s)]='#'; 620 string ans=solve(s); 621 cout<<ans<<endl; 622 } 623 return 0; 624 } 625 //AC 626 #include <bits/stdc++.h> 627 using namespace std; 628 map<string,int>op; 629 map<string,string>func; 630 const int N=1e3+10; 631 char s[N],tf1[N],tf2[N]; 632 string getNum(const char *s,int &i) 633 { 634 string res=""; 635 while(isdigit(s[i])) res+=s[i],i++; 636 if(s[i]=='.') res+=s[i],i++; 637 else return res; 638 while(isdigit(s[i])) res+=s[i],i++; 639 return res; 640 } 641 string getOp(const char *s,int &i) 642 { 643 string res=""; 644 while(isalnum(s[i])) res+=s[i],i++; 645 res+=s[i],i++; 646 return res; 647 } 648 string replace(string s,string p,string &t) 649 { 650 if (s.find(p)==-1) return s; 651 int idx=s.find(p); 652 string tmp=s.substr(0,idx); 653 tmp+="("+t+")"; 654 tmp+=replace(s.substr(idx+p.length()),p,t); 655 return tmp; 656 } 657 string solve(char *s) 658 { 659 stack<string>ops; 660 stack<string>num; 661 ops.push("#"); 662 int i=0; 663 string neg=""; 664 if(s[i]=='-') neg="-",i++; 665 while(s[i]!='#' || ops.top()!="#") 666 { 667 //printf("%c ",s[i]); 668 if(isdigit(s[i])) 669 { 670 num.push(neg+getNum(s,i)); 671 neg=""; 672 //cout<<"numtop:"<<num.top()<<endl; 673 continue; 674 } 675 string oper=getOp(s,i); 676 if(ops.top().find('(')!=-1 && oper==")") 677 { 678 string ope=ops.top(); 679 ops.pop(); 680 if(ope=="int(") 681 { 682 string num1=num.top();num.pop(); 683 int idx; 684 if((idx=num1.find('.'))!=-1) 685 { 686 num1=num1.substr(0,idx); 687 } 688 num.push(num1); 689 } 690 else if(ope=="float(") 691 { 692 string num1=num.top();num.pop(); 693 double a; 694 sscanf(num1.c_str(),"%lf",&a); 695 char tmp[20]; 696 sprintf(tmp,"%.6f",a); 697 num.push(tmp); 698 } 699 else if(ope!="(") 700 { 701 string num1=num.top();num.pop(); 702 string exp=func[ope]; 703 string x=exp.substr(0,exp.find(')')); 704 exp=exp.substr(exp.find('=')+1); 705 //func(a)=a+a+a+a 706 string tmp=replace(exp,x,num1); 707 //cout<<ope<<" "<<exp<<" "<<num1<<" "<<x<<" "<<tmp<<endl; 708 char *exps=new char[N]; 709 strcpy(exps,tmp.c_str()); 710 num.push(solve(exps)); 711 } 712 continue; 713 } 714 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 715 { 716 ops.push(oper); 717 if(oper.find('(')!=-1) 718 { 719 if(s[i]=='-') neg="-",i++; 720 } 721 continue; 722 } 723 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 724 //cout<<"top"<<num.top()<<endl; 725 string num1=num.top();num.pop(); 726 string ope=ops.top();ops.pop(); 727 string num2=num.top();num.pop(); 728 char num3[100]; 729 if(num1.find('.')==-1 && num2.find('.')==-1) 730 { 731 long long a,b; 732 sscanf(num1.c_str(),"%lld",&b); 733 sscanf(num2.c_str(),"%lld",&a); 734 //cout<<a<<ope<<b<<endl; 735 long long ans; 736 if(ope=="+") ans=a+b; 737 else if(ope=="-") ans=a-b; 738 else if(ope=="*") ans=a*b; 739 else if(ope=="/") ans=a/b; 740 sprintf(num3,"%lld",ans); 741 } 742 else 743 { 744 double a,b,ans; 745 sscanf(num1.c_str(),"%lf",&b); 746 sscanf(num2.c_str(),"%lf",&a); 747 //cout<<a<<ope<<b<<endl; 748 if(ope=="+") ans=a+b; 749 else if(ope=="-") ans=a-b; 750 else if(ope=="*") ans=a*b; 751 else if(ope=="/") ans=a/b; 752 sprintf(num3,"%.6f",ans); 753 } 754 //cout<<"num3:"<<num3<<endl; 755 num.push(num3); 756 } 757 return num.top(); 758 } 759 int main() 760 { 761 op["#"]=0; 762 op["("]=1; 763 op["+"]=2; 764 op["-"]=2; 765 op["*"]=3; 766 op["/"]=3; 767 op[")"]=1; 768 op["int("]=1; 769 op["float("]=1; 770 int n; 771 while(scanf("%d",&n)==1) 772 { 773 for(int i=0;i<n;i++) 774 { 775 getchar(); 776 scanf("%[^(](%s",tf1,tf2); 777 func[string(tf1)+"("]=string(tf2)+"#"; 778 } 779 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 780 scanf("%s",s); 781 s[strlen(s)]='#'; 782 string ans=solve(s); 783 cout<<ans<<endl; 784 } 785 return 0; 786 }
D.百度的科学计算器(困难)
赛后:用拓扑排序处理了一下环的问题,第一次码完交了T了一发,因为是从上到下不断替换的,表达式多了以后这棵树太大就会T,然后改成函数替换,每次访问一个函数后就将这个函数替换成没有嵌套函数的表达式,所以每个函数只会解析一次,码完后交上去各种RE,因为代码结构大变了,改了一波发现不RE也不T了,但是MLE了因为函数不断合并,如果最上面的一路往下嵌套,合并到最上层时会直接就成了一个超长的表达式,直接就爆内存了,然后想着只能在合并表达式时将x+x+1+2+y+4+y合并成一个map,x 2,# 7,y 2,但如果涉及到int(),float(),又会很难处理,这个时候去看了下题解,发现题解是直接从上往下记忆化,直接记函数+参数数值,这样就不存在前面那些难处理的问题了
最后照题解的方法将第一份代码改了下(后面的代码改着改着变化太大了),虽然RE了好几发,还WA了几发(long double),但终于还是A了
注意:new的数据 B. 腾讯狼人杀(简单)记得delete掉
1 //AC 2 #include <bits/stdc++.h> 3 using namespace std; 4 map<string,int>op; 5 map<string,string>func; 6 map<string,int>funcID; 7 map<string,string>calFunc; 8 const int N=1e3+10; 9 char s[N],tf1[N],tf2[N]; 10 int mp[N][N],c[N],topo[N],t,circle[N],n; 11 string getNum(const char *s,int &i) 12 { 13 string res=""; 14 while(isdigit(s[i])) res+=s[i],i++; 15 if(s[i]=='.') res+=s[i],i++; 16 else return res; 17 while(isdigit(s[i])) res+=s[i],i++; 18 return res; 19 } 20 string getOp(const char *s,int &i) 21 { 22 string res=""; 23 while(isalnum(s[i])) res+=s[i],i++; 24 res+=s[i],i++; 25 return res; 26 } 27 string getMark(const char *s,int &i) 28 { 29 if(isdigit(s[i])) return getNum(s,i); 30 string res=""; 31 while(isalpha(s[i])) res+=s[i],i++; 32 return res; 33 } 34 string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数子串问题 35 { 36 //cout<<s<<" "<<p<<" "<<t<<endl; 37 int idx = s.find(p); 38 if (idx == -1) return s; 39 if(idx>0 && isalnum(s[idx-1]) || idx+p.length()<s.length() && isalnum(s[idx+p.length()])) //参数子串 40 return s.substr(0,idx+p.length())+replace(s.substr(idx+p.length()),p,t); 41 string tmp = s.substr(0, idx); 42 if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) 43 tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); 44 else tmp += t; 45 tmp += replace(s.substr(idx + p.length()), p, t); 46 return tmp; 47 } 48 string replacePars(string funcName, const char *s, int &i) 49 { 50 string exp = func[funcName]; 51 //cout<<"before:"<<exp; 52 int eqID = exp.find('='); 53 string par = exp.substr(0, eqID); 54 exp = exp.substr(eqID + 1); 55 string num1 = getMark(s, i); 56 int j = 0; 57 string par1 = getMark(par.c_str(), j); 58 //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; 59 if (num1 != "") exp = replace(exp, par1, num1); 60 while (s[i] != ')') 61 { 62 getOp(s, i); 63 getOp(par.c_str(), j); 64 num1 = getMark(s, i); 65 string par1 = getMark(par.c_str(), j); 66 exp = replace(exp, par1, num1); 67 } 68 //cout<<" after:"<<exp<<endl; 69 return exp; 70 } 71 string cal(string num1,string num2,string ope) 72 { 73 char num3[100]; 74 if(num1.find('.')==-1 && num2.find('.')==-1) 75 { 76 long long a,b; 77 sscanf(num1.c_str(),"%lld",&b); 78 sscanf(num2.c_str(),"%lld",&a); 79 //cout<<a<<ope<<b<<endl; 80 long long ans; 81 if(ope=="+") ans=a+b; 82 else if(ope=="-") ans=a-b; 83 else if(ope=="*") ans=a*b; 84 else if(ope=="/") ans=a/b; 85 sprintf(num3,"%lld",ans); 86 } 87 else 88 { 89 long double a,b,ans; 90 sscanf(num1.c_str(),"%Lf",&b); 91 sscanf(num2.c_str(),"%Lf",&a); 92 //cout<<a<<ope<<b<<endl; 93 if(ope=="+") ans=a+b; 94 else if(ope=="-") ans=a-b; 95 else if(ope=="*") ans=a*b; 96 else if(ope=="/") ans=a/b; 97 sprintf(num3,"%.6Lf",ans); 98 } 99 //cout<<"num3:"<<num3<<endl; 100 return num3; 101 } 102 string solve(char *s) 103 { 104 stack<string>ops; 105 stack<string>num; 106 ops.push("#"); 107 int i=0; 108 string neg=""; 109 if(s[i]=='-') neg="-",i++; 110 while(s[i]!='#' || ops.top()!="#") 111 { 112 //printf("%c ",s[i]); 113 if(isdigit(s[i])) 114 { 115 num.push(neg+getNum(s,i)); 116 neg=""; 117 //cout<<"numtop:"<<num.top()<<endl; 118 continue; 119 } 120 string oper=getOp(s,i); 121 if(ops.top().find('(')!=-1 && oper==")") 122 { 123 string ope=ops.top(); 124 ops.pop(); 125 if(ope=="int(") 126 { 127 string num1=num.top();num.pop(); 128 int idx; 129 if((idx=num1.find('.'))!=-1) 130 { 131 num1=num1.substr(0,idx); 132 } 133 num.push(num1); 134 } 135 else if(ope=="float(") 136 { 137 string num1=num.top();num.pop(); 138 double a; 139 sscanf(num1.c_str(),"%lf",&a); 140 char tmp[100]; 141 sprintf(tmp,"%.6f",a); 142 num.push(tmp); 143 } 144 continue; 145 } 146 if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) 147 { 148 if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") 149 { 150 if(circle[funcID[oper]]) return "No Answer"; 151 if(strchr(s+i,')')==NULL) return "No Answer"; 152 int calIdx=strchr(s+i,')')-s-i; 153 char *exps=new char[N]; 154 memcpy(exps,s+i,calIdx); 155 exps[calIdx]=0; 156 string funcAndPars=oper+exps; 157 //cout<<funcAndPars<<" "<<s+i<<" "<<calIdx<<"..."<<exps<<endl; 158 auto it=calFunc.find(funcAndPars); 159 if(it!=calFunc.end()) 160 { 161 delete []exps; 162 string ans=it->second; 163 if(ans=="No Answer") return ans; 164 num.push(ans); 165 i+=calIdx; 166 getOp(s,i);//')' 167 //cout<<s+i<<endl; 168 continue; 169 } 170 delete exps; 171 string exp=replacePars(oper,s,i); 172 exps=new char[exp.length()+10]; 173 getOp(s,i);//')' 174 //cout<<"after replace:"<<exp<<endl; 175 strcpy(exps,exp.c_str()); 176 string ans=solve(exps); 177 calFunc[funcAndPars]=ans; 178 delete []exps; 179 if(ans=="No Answer") return ans; 180 num.push(ans); 181 continue; 182 } 183 ops.push(oper); 184 if(oper.find('(')!=-1) 185 { 186 if(s[i]=='-') neg="-",i++; 187 } 188 continue; 189 } 190 for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; 191 //cout<<"top"<<num.top()<<endl; 192 string num1=num.top();num.pop(); 193 string ope=ops.top();ops.pop(); 194 string num2=num.top();num.pop(); 195 num.push(cal(num1,num2,ope)); 196 } 197 return num.top(); 198 } 199 bool dfs(int u) 200 { 201 c[u]=-1; 202 for(int v=0;v<n;v++) if(mp[u][v]) { 203 if(c[v]<0) 204 { 205 circle[v]=circle[u]=1; 206 return false; 207 } 208 else if(!c[v] && !dfs(v)) 209 { 210 circle[v]=circle[u]=1; 211 return false; 212 } 213 } 214 c[u]=1; 215 return true; 216 } 217 void toposort() 218 { 219 memset(circle,0,sizeof(circle)); 220 memset(c,0,sizeof(c)); 221 for(int u=0;u<n;u++) if(!c[u]) dfs(u); 222 //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); 223 } 224 void makeMap(int u,string &exp) 225 { 226 int i=exp.find('='); 227 while(i<exp.length()) 228 { 229 if(!isalpha(exp[i])) 230 { 231 i++; 232 continue; 233 } 234 string fun=getOp(exp.c_str(),i); 235 if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; 236 mp[u][funcID[fun]]=1; 237 } 238 } 239 int main() 240 { 241 op["#"]=0; 242 op["("]=1; 243 op["+"]=2; 244 op["-"]=2; 245 op["*"]=3; 246 op["/"]=3; 247 op[")"]=1; 248 op["int("]=1; 249 op["float("]=1; 250 while(scanf("%d",&n)==1) 251 { 252 func.clear(); 253 calFunc.clear(); 254 funcID.clear(); 255 memset(mp,0,sizeof(mp)); 256 for(int i=0;i<n;i++) 257 { 258 getchar(); 259 scanf("%[^(](%s",tf1,tf2); 260 func[string(tf1)+"("]=string(tf2)+"#"; 261 funcID[string(tf1)+"("]=i; 262 } 263 for(auto it=func.begin();it!=func.end();it++) 264 { 265 makeMap(funcID[it->first],it->second); 266 } 267 //for(int i=0;i<n;i++) 268 //{ 269 // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); 270 // printf(" "); 271 //} 272 toposort(); 273 //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; 274 scanf("%s",s); 275 s[strlen(s)]='#'; 276 string ans=solve(s); 277 cout<<ans<<endl; 278 } 279 return 0; 280 }
判题状态 提交时间 运行时间 运行内存 使用语言 正确通过 2017-05-23 14:43 14ms 7060kB c++ 答案错误 2017-05-23 14:35 16ms 6984kB c++ 答案错误 2017-05-23 14:28 14ms 6984kB c++ 运行超时 2017-05-23 14:26 2002ms 6968kB c++ 运行超时 2017-05-23 14:25 2001ms 6964kB c++ 段错误 2017-05-23 14:24 11ms 6988kB c++ 运行超时 2017-05-23 14:23 2000ms 6876kB c++ 段错误 2017-05-23 14:21 13ms 8860kB c++ 段错误 2017-05-23 14:19 11ms 7888kB c++ 段错误 2017-05-23 14:18 14ms 7896kB c++ 段错误 2017-05-23 14:15 11ms 7896kB c++ 段错误 2017-05-23 13:39 12ms 8264kB c++ 段错误 2017-05-23 13:28 13ms 8264kB c++ 答案错误 2017-05-23 13:03 1ms 4244kB c++ 内存超限 2017-05-23 12:54 952ms 268552kB c++ 内存超限 2017-05-23 01:29 949ms 268548kB c++ 内存超限 2017-05-23 01:23 899ms 268544kB c++ 段错误 2017-05-23 00:51 44ms 69640kB c++ 段错误 2017-05-23 00:23 1ms 4244kB c++ 段错误 2017-05-22 23:13 43ms 69640kB c++ 段错误 2017-05-22 22:31 42ms 69640kB c++ 段错误 2017-05-22 22:24 45ms 69640kB c++ 运行超时 2017-05-22 19:28 1001ms 184308kB c++ //---------------------------------------------------------------1 #include <bits/stdc++.h> using namespace std; map<string,int>op; map<string,string>func; map<string,int>funcID; const int N=1e3+10; char s[N],tf1[N],tf2[N]; int mp[N][N],c[N],topo[N],t,circle[N],n; string getNum(const char *s,int &i) { string res=""; while(isdigit(s[i])) res+=s[i],i++; if(s[i]=='.') res+=s[i],i++; else return res; while(isdigit(s[i])) res+=s[i],i++; return res; } string getOp(const char *s,int &i) { string res=""; while(isalnum(s[i])) res+=s[i],i++; res+=s[i],i++; return res; } string getMark(const char *s,int &i) { string res=""; while(isalpha(s[i])) res+=s[i],i++; return res; } string replace(string s,string p,string &t)//替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p)==-1) return s; int idx=s.find(p); string tmp=s.substr(0,idx); if(idx>0 && (tmp[idx-1]=='+' || tmp[idx-1]=='-' || tmp[idx-1]=='*' || tmp[idx-1]=='/')) tmp+="("+t+")";//f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp +=t; tmp+=replace(s.substr(idx+p.length()),p,t); return tmp; } string solve(char *s) { stack<string>ops; stack<string>num; ops.push("#"); int i=0; string neg=""; if(s[i]=='-') neg="-",i++; while(s[i]!='#' || ops.top()!="#") { //printf("%c ",s[i]); if(isdigit(s[i])) { num.push(neg+getNum(s,i)); neg=""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper=getOp(s,i); if(ops.top().find('(')!=-1 && oper==")") { string ope=ops.top(); ops.pop(); if(ope=="int(") { string num1=num.top();num.pop(); int idx; if((idx=num1.find('.'))!=-1) { num1=num1.substr(0,idx); } num.push(num1); } else if(ope=="float(") { string num1=num.top();num.pop(); double a; sscanf(num1.c_str(),"%lf",&a); char tmp[20]; sprintf(tmp,"%.6f",a); num.push(tmp); } continue; } if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) { if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") { if(circle[funcID[oper]]) return "No Answer"; char *exps=new char[N]; string exp=func[oper]; int eqID=exp.find('='); string par=exp.substr(0,eqID); exp=exp.substr(eqID+1); string num1=getNum(s,i); int j=0; string par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); //cout<<"par:"<<par<<" exp:"<<exp<<" num1:"<<num1<<" par1:"<<par1<<endl; if(num1!="") exp=replace(exp,par1,num1); while(s[i]!=')') { getOp(s,i); num1=getNum(s,i); par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); exp=replace(exp,par1,num1); } getOp(s,i); //cout<<"after replace:"<<exp<<endl; strcpy(exps,exp.c_str()); string ans=solve(exps); if(ans=="No Answer") return ans; num.push(ans); continue; } ops.push(oper); if(oper.find('(')!=-1) { if(s[i]=='-') neg="-",i++; } continue; } for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; //cout<<"top"<<num.top()<<endl; string num1=num.top();num.pop(); string ope=ops.top();ops.pop(); string num2=num.top();num.pop(); char num3[100]; if(num1.find('.')==-1 && num2.find('.')==-1) { long long a,b; sscanf(num1.c_str(),"%lld",&b); sscanf(num2.c_str(),"%lld",&a); //cout<<a<<ope<<b<<endl; long long ans; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%lld",ans); } else { double a,b,ans; sscanf(num1.c_str(),"%lf",&b); sscanf(num2.c_str(),"%lf",&a); //cout<<a<<ope<<b<<endl; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%.6f",ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u]=-1; for(int v=0;v<n;v++) if(mp[u][v]) { if(c[v]<0) { circle[v]=circle[u]=1; return false; } else if(!c[v] && !dfs(v)) { circle[v]=circle[u]=1; return false; } } c[u]=1; return true; } void toposort() { memset(circle,0,sizeof(circle)); memset(c,0,sizeof(c)); for(int u=0;u<n;u++) if(!c[u]) dfs(u); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); } void makeMap(int u,string &exp) { int i=exp.find('='); while(i<exp.length()) { if(!isalpha(exp[i])) { i++; continue; } string fun=getOp(exp.c_str(),i); if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; mp[u][funcID[fun]]=1; } } int main() { op["#"]=0; op["("]=1; op["+"]=2; op["-"]=2; op["*"]=3; op["/"]=3; op[")"]=1; op["int("]=1; op["float("]=1; while(scanf("%d",&n)==1) { memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { getchar(); scanf("%[^(](%s",tf1,tf2); func[string(tf1)+"("]=string(tf2)+"#"; funcID[string(tf1)+"("]=i; } for(auto it=func.begin();it!=func.end();it++) { makeMap(funcID[it->first],it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; scanf("%s",s); s[strlen(s)]='#'; string ans=solve(s); cout<<ans<<endl; } return 0; } //------------------------------------------------------------------------2 #include <bits/stdc++.h> using namespace std; map<string, int> op; map<string, string> func; vector<string> vfunc; map<string, int> funcID; const int N = 1e3 + 10; char s[N], tf1[N], tf2[N]; int mp[N][N], c[N], topo[N], t, circle[N], n; string getNum(const char *s, int &i) //num { string res = ""; while (isdigit(s[i])) res += s[i], i++; if (s[i] == '.') res += s[i], i++; else return res; while (isdigit(s[i])) res += s[i], i++; return res; } string getOp(const char *s, int &i) //+ or - or * or / or ( or ) or int( or float( or func( { string res = ""; while (isalnum(s[i])) res += s[i], i++; if(s[i]==0) res+='#',i++; else res += s[i], i++; return res; } string getMark(const char *s, int &i) //num or fun name or par name { string res = ""; while (isalnum(s[i])) res += s[i], i++; return res; } string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p) == -1) return s; int idx = s.find(p); string tmp = s.substr(0, idx); if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp += t; tmp += replace(s.substr(idx + p.length()), p, t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string solve(char *s) { stack<string> ops; stack<string> num; ops.push("#"); int i = 0; string neg = ""; if (s[i] == '-') neg = "-", i++; while (s[i] != '#' || ops.top() != "#") { //printf("%c ",s[i]); if (isdigit(s[i])) { num.push(neg + getNum(s, i)); neg = ""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper = getOp(s, i); if (ops.top().find('(') != -1 && oper == ")") { string ope = ops.top(); ops.pop(); if (ope == "int(") { string num1 = num.top(); num.pop(); int idx; if ((idx = num1.find('.')) != -1) { num1 = num1.substr(0, idx); } num.push(num1); } else if (ope == "float(") { string num1 = num.top(); num.pop(); double a; sscanf(num1.c_str(), "%lf", &a); char tmp[20]; sprintf(tmp, "%.6f", a); num.push(tmp); } continue; } if (oper.find('(') != -1 || op[ops.top()] < op[oper]) { if (oper.find('(') != -1 && oper != "(" && oper != "int(" && oper != "float(") { if (circle[funcID[oper]]) return "No Answer"; char *exps = new char[N]; string exp = replacePars(oper, s, i); getOp(s, i); //")" //cout<<"after replace:"<<exp<<endl; strcpy(exps, exp.c_str()); string ans = solve(exps); if (ans == "No Answer") return ans; num.push(ans); continue; } ops.push(oper); if (oper.find('(') != -1) { if (s[i] == '-') neg = "-", i++; } continue; } for (int j = oper.length() - 1; j >= 0; j--) s[--i] = oper[j]; //cout<<"top"<<num.top()<<endl; string num1 = num.top(); num.pop(); string ope = ops.top(); ops.pop(); string num2 = num.top(); num.pop(); char num3[100]; if (num1.find('.') == -1 && num2.find('.') == -1) { long long a, b; sscanf(num1.c_str(), "%lld", &b); sscanf(num2.c_str(), "%lld", &a); //cout<<a<<ope<<b<<endl; long long ans; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%lld", ans); } else { double a, b, ans; sscanf(num1.c_str(), "%lf", &b); sscanf(num2.c_str(), "%lf", &a); //cout<<a<<ope<<b<<endl; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%.6f", ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u] = -1; for (int v = 0; v < n; v++) if (mp[u][v]) { if (c[v] < 0) { circle[v] = circle[u] = 1; return false; } else if (!c[v] && !dfs(v)) { circle[v] = circle[u] = 1; return false; } } c[u] = 1; return true; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; char lastc=replaceExp[replaceExp.length()-1]; if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } void toposort() { memset(circle, 0, sizeof(circle)); memset(c, 0, sizeof(c)); for (int u = 0; u < n; u++) if (!c[u]) dfs(u); memset(c, 0, sizeof(c)); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); for (int u = 0; u < n; u++) if (!circle[u]) replaceFunc(u); //for (auto it = func.begin(); it != func.end(); it++) cout << it->first << it->second << endl; } void makeMap(int u, string &exp) { int i = exp.find('='); while (i < exp.length()) { if (!isalpha(exp[i])) { i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") continue; mp[u][funcID[fun]] = 1; } } int main() { op["#"] = 0; op["("] = 1; op["+"] = 2; op["-"] = 2; op["*"] = 3; op["/"] = 3; op[")"] = 1; op["int("] = 1; op["float("] = 1; while (scanf("%d", &n) == 1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { getchar(); scanf("%[^(](%s", tf1, tf2); func[string(tf1) + "("] = string(tf2); funcID[string(tf1) + "("] = i; vfunc.push_back(string(tf1) + "("); } //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; for (auto it = func.begin(); it != func.end(); it++) { makeMap(funcID[it->first], it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); scanf("%s", s); s[strlen(s)] = '#'; string ans = solve(s); cout << ans << endl; } return 0; } //----------------------------------------------------------------------------3 #include <bits/stdc++.h> using namespace std; map<string, int> op; map<string, string> func; vector<string> vfunc; map<string, int> funcID; const int N = 1e3 + 10; char s[N], tf1[N], tf2[N]; int mp[N][N], c[N], topo[N], t, circle[N], n; string getNum(const char *s, int &i) //num { string res = ""; while (isdigit(s[i])) res += s[i], i++; if (s[i] == '.') res += s[i], i++; else return res; while (isdigit(s[i])) res += s[i], i++; return res; } string getOp(const char *s, int &i) //+ or - or * or / or ( or ) or int( or float( or func( { string res = ""; while (isalnum(s[i])) res += s[i], i++; if(s[i]==0) res+='#',i++; else res += s[i], i++; return res; } string getMark(const char *s, int &i) //num or fun name or par name { string res = ""; while (isalnum(s[i])) res += s[i], i++; return res; } string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p) == -1) return s; int idx = s.find(p); string tmp = s.substr(0, idx); if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp += t; tmp += replace(s.substr(idx + p.length()), p, t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string solve(char *s) { stack<string> ops; stack<string> num; ops.push("#"); int i = 0; string neg = ""; if (s[i] == '-') neg = "-", i++; s[strlen(s)]='#'; while (s[i] != '#' || ops.top() != "#") { //printf("%c ",s[i]); if (isdigit(s[i])) { num.push(neg + getNum(s, i)); neg = ""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper = getOp(s, i); if (ops.top().find('(') != -1 && oper == ")") { string ope = ops.top(); ops.pop(); if (ope == "int(") { string num1 = num.top(); num.pop(); int idx; if ((idx = num1.find('.')) != -1) { num1 = num1.substr(0, idx); } num.push(num1); } else if (ope == "float(") { string num1 = num.top(); num.pop(); double a; sscanf(num1.c_str(), "%lf", &a); char tmp[20]; sprintf(tmp, "%.6f", a); num.push(tmp); } continue; } if (oper.find('(') != -1 || op[ops.top()] < op[oper]) { if (oper.find('(') != -1 && oper != "(" && oper != "int(" && oper != "float(") { if (circle[funcID[oper]]) return "No Answer"; char *exps = new char[N]; string exp = replacePars(oper, s, i); getOp(s, i); //")" //cout<<"after replace:"<<exp<<endl; strcpy(exps, exp.c_str()); string ans = solve(exps); if (ans == "No Answer") return ans; num.push(ans); continue; } ops.push(oper); if (oper.find('(') != -1) { if (s[i] == '-') neg = "-", i++; } continue; } for (int j = oper.length() - 1; j >= 0; j--) s[--i] = oper[j]; //cout<<"top"<<num.top()<<endl; string num1 = num.top(); num.pop(); string ope = ops.top(); ops.pop(); string num2 = num.top(); num.pop(); char num3[100]; if (num1.find('.') == -1 && num2.find('.') == -1) { long long a, b; sscanf(num1.c_str(), "%lld", &b); sscanf(num2.c_str(), "%lld", &a); //cout<<a<<ope<<b<<endl; long long ans; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%lld", ans); } else { double a, b, ans; sscanf(num1.c_str(), "%lf", &b); sscanf(num2.c_str(), "%lf", &a); //cout<<a<<ope<<b<<endl; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%.6f", ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u] = -1; for (int v = 0; v < n; v++) if (mp[u][v]) { if (c[v] < 0) { circle[v] = circle[u] = 1; return false; } else if (!c[v] && !dfs(v)) { circle[v] = circle[u] = 1; return false; } } c[u] = 1; return true; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; char lastc=replaceExp[replaceExp.length()-1]; if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } void toposort() { memset(circle, 0, sizeof(circle)); memset(c, 0, sizeof(c)); for (int u = 0; u < n; u++) if (!c[u]) dfs(u); memset(c, 0, sizeof(c)); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); for (int u = 0; u < n; u++) if (!circle[u]) replaceFunc(u); //for (auto it = func.begin(); it != func.end(); it++) cout << it->first << it->second << endl; } void makeMap(int u, string &exp) { int i = exp.find('='); while (i < exp.length()) { if (!isalpha(exp[i])) { i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") continue; mp[u][funcID[fun]] = 1; } } int main() { op["#"] = 0; op["("] = 1; op["+"] = 2; op["-"] = 2; op["*"] = 3; op["/"] = 3; op[")"] = 1; op["int("] = 1; op["float("] = 1; while (scanf("%d", &n) == 1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { getchar(); scanf("%[^(](%s", tf1, tf2); func[string(tf1) + "("] = string(tf2); funcID[string(tf1) + "("] = i; vfunc.push_back(string(tf1) + "("); } //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; for (auto it = func.begin(); it != func.end(); it++) { makeMap(funcID[it->first], it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); scanf("%s", s); s[strlen(s)] = '#'; string ans = solve(s); cout << ans << endl; } return 0; } //-------------------------------------------------------------------------------4 #include <bits/stdc++.h> using namespace std; map<string, int> op; map<string, string> func; vector<string> vfunc; map<string, int> funcID; const int N = 1e3 + 10; char s[N], tf1[N], tf2[N]; int mp[N][N], c[N], topo[N], t, circle[N], n; string getNum(const char *s, int &i) //num { string res = ""; while (isdigit(s[i])) res += s[i], i++; if (s[i] == '.') res += s[i], i++; else return res; while (isdigit(s[i])) res += s[i], i++; return res; } string getOp(const char *s, int &i) //+ or - or * or / or ( or ) or int( or float( or func( { string res = ""; while (isalnum(s[i])) res += s[i], i++; res += s[i], i++; return res; } string getMark(const char *s, int &i) //num or fun name or par name { string res = ""; while (isalnum(s[i])) res += s[i], i++; return res; } string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p) == -1) return s; int idx = s.find(p); string tmp = s.substr(0, idx); if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp += t; tmp += replace(s.substr(idx + p.length()), p, t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string solve(char *s) { stack<string> ops; stack<string> num; ops.push("#"); int i = 0; string neg = ""; if (s[i] == '-') neg = "-", i++; s[strlen(s)]='#'; while (s[i] != '#' || ops.top() != "#") { //printf("%c ",s[i]); if (isdigit(s[i])) { num.push(neg + getNum(s, i)); neg = ""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper = getOp(s, i); if (ops.top().find('(') != -1 && oper == ")") { string ope = ops.top(); ops.pop(); if (ope == "int(") { string num1 = num.top(); num.pop(); int idx; if ((idx = num1.find('.')) != -1) { num1 = num1.substr(0, idx); } num.push(num1); } else if (ope == "float(") { string num1 = num.top(); num.pop(); double a; sscanf(num1.c_str(), "%lf", &a); char tmp[20]; sprintf(tmp, "%.6f", a); num.push(tmp); } continue; } if (oper.find('(') != -1 || op[ops.top()] < op[oper]) { if (oper.find('(') != -1 && oper != "(" && oper != "int(" && oper != "float(") { if (circle[funcID[oper]]) return "No Answer"; char *exps = new char[N]; string exp = replacePars(oper, s, i); getOp(s, i); //")" //cout<<"after replace:"<<exp<<endl; strcpy(exps, exp.c_str()); string ans = solve(exps); if (ans == "No Answer") return ans; num.push(ans); continue; } ops.push(oper); if (oper.find('(') != -1) { if (s[i] == '-') neg = "-", i++; } continue; } for (int j = oper.length() - 1; j >= 0; j--) s[--i] = oper[j]; //cout<<"top"<<num.top()<<endl; string num1 = num.top(); num.pop(); string ope = ops.top(); ops.pop(); string num2 = num.top(); num.pop(); char num3[100]; if (num1.find('.') == -1 && num2.find('.') == -1) { long long a, b; sscanf(num1.c_str(), "%lld", &b); sscanf(num2.c_str(), "%lld", &a); //cout<<a<<ope<<b<<endl; long long ans; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%lld", ans); } else { double a, b, ans; sscanf(num1.c_str(), "%lf", &b); sscanf(num2.c_str(), "%lf", &a); //cout<<a<<ope<<b<<endl; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%.6f", ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u] = -1; for (int v = 0; v < n; v++) if (mp[u][v]) { if (c[v] < 0) { circle[v] = circle[u] = 1; return false; } else if (!c[v] && !dfs(v)) { circle[v] = circle[u] = 1; return false; } } c[u] = 1; return true; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } void toposort() { memset(circle, 0, sizeof(circle)); memset(c, 0, sizeof(c)); for (int u = 0; u < n; u++) if (!c[u]) dfs(u); memset(c, 0, sizeof(c)); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); for (int u = 0; u < n; u++) if (!circle[u]) replaceFunc(u); //for (auto it = func.begin(); it != func.end(); it++) cout << it->first << it->second << endl; } void makeMap(int u, string &exp) { int i = exp.find('='); while (i < exp.length()) { if (!isalpha(exp[i])) { i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") continue; mp[u][funcID[fun]] = 1; } } int main() { op["#"] = 0; op["("] = 1; op["+"] = 2; op["-"] = 2; op["*"] = 3; op["/"] = 3; op[")"] = 1; op["int("] = 1; op["float("] = 1; while (scanf("%d", &n) == 1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { getchar(); scanf("%[^(](%s", tf1, tf2); func[string(tf1) + "("] = string(tf2); funcID[string(tf1) + "("] = i; vfunc.push_back(string(tf1) + "("); } //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; for (auto it = func.begin(); it != func.end(); it++) { makeMap(funcID[it->first], it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); scanf("%s", s); s[strlen(s)] = '#'; string ans = solve(s); cout << ans << endl; } return 0; } //--------------------------------------------------------------------------5 #include <bits/stdc++.h> using namespace std; map<string, int> op; map<string, string> func; vector<string> vfunc; map<string, int> funcID; const int N = 1e3 + 10; char s[N], tf1[N], tf2[N]; int mp[N][N], c[N], topo[N], t, circle[N], n; string getNum(const char *s, int &i) //num { string res = ""; while (isdigit(s[i])) res += s[i], i++; if (s[i] == '.') res += s[i], i++; else return res; while (isdigit(s[i])) res += s[i], i++; return res; } string getOp(const char *s, int &i) //+ or - or * or / or ( or ) or int( or float( or func( { string res = ""; while (isalnum(s[i])) res += s[i], i++; res += s[i], i++; return res; } string getMark(const char *s, int &i) //num or fun name or par name { string res = ""; while (isalnum(s[i])) res += s[i], i++; return res; } string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p) == -1) return s; int idx = s.find(p); string tmp = s.substr(0, idx); if(idx>0 && isalnum(s[idx-1]) || idx<s.length()-1 && isalnum(s[idx+1])) return s.substr(0,idx+p.length())+replace(s.substr(idx+p.length()),p,t); if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp += t; tmp += replace(s.substr(idx + p.length()), p, t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string solve(char *s) { stack<string> ops; stack<string> num; ops.push("#"); int i = 0; string neg = ""; if (s[i] == '-') neg = "-", i++; s[strlen(s)]='#'; while (s[i] != '#' || ops.top() != "#") { //printf("%c ",s[i]); if (isdigit(s[i])) { num.push(neg + getNum(s, i)); neg = ""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper = getOp(s, i); if (ops.top().find('(') != -1 && oper == ")") { string ope = ops.top(); ops.pop(); if (ope == "int(") { string num1 = num.top(); num.pop(); int idx; if ((idx = num1.find('.')) != -1) { num1 = num1.substr(0, idx); } num.push(num1); } else if (ope == "float(") { string num1 = num.top(); num.pop(); double a; sscanf(num1.c_str(), "%lf", &a); char tmp[20]; sprintf(tmp, "%.6f", a); num.push(tmp); } continue; } if (oper.find('(') != -1 || op[ops.top()] < op[oper]) { if (oper.find('(') != -1 && oper != "(" && oper != "int(" && oper != "float(") { if (circle[funcID[oper]]) return "No Answer"; char *exps = new char[N]; string exp = replacePars(oper, s, i); getOp(s, i); //")" //cout<<"after replace:"<<exp<<endl; strcpy(exps, exp.c_str()); string ans = solve(exps); if (ans == "No Answer") return ans; num.push(ans); continue; } ops.push(oper); if (oper.find('(') != -1) { if (s[i] == '-') neg = "-", i++; } continue; } for (int j = oper.length() - 1; j >= 0; j--) s[--i] = oper[j]; //cout<<"top"<<num.top()<<endl; string num1 = num.top(); num.pop(); string ope = ops.top(); ops.pop(); string num2 = num.top(); num.pop(); char num3[100]; if (num1.find('.') == -1 && num2.find('.') == -1) { long long a, b; sscanf(num1.c_str(), "%lld", &b); sscanf(num2.c_str(), "%lld", &a); //cout<<a<<ope<<b<<endl; long long ans; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%lld", ans); } else { double a, b, ans; sscanf(num1.c_str(), "%lf", &b); sscanf(num2.c_str(), "%lf", &a); //cout<<a<<ope<<b<<endl; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%.6f", ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u] = -1; for (int v = 0; v < n; v++) if (mp[u][v]) { if (c[v] < 0) { circle[v] = circle[u] = 1; return false; } else if (!c[v] && !dfs(v)) { circle[v] = circle[u] = 1; return false; } } c[u] = 1; return true; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } void toposort() { memset(circle, 0, sizeof(circle)); memset(c, 0, sizeof(c)); for (int u = 0; u < n; u++) if (!c[u]) dfs(u); memset(c, 0, sizeof(c)); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); for (int u = 0; u < n; u++) if (!circle[u]) replaceFunc(u); //for (auto it = func.begin(); it != func.end(); it++) cout << it->first << it->second << endl; } void makeMap(int u, string &exp) { int i = exp.find('='); while (i < exp.length()) { if (!isalpha(exp[i])) { i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") continue; mp[u][funcID[fun]] = 1; } } int main() { op["#"] = 0; op["("] = 1; op["+"] = 2; op["-"] = 2; op["*"] = 3; op["/"] = 3; op[")"] = 1; op["int("] = 1; op["float("] = 1; while (scanf("%d", &n) == 1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { getchar(); scanf("%[^(](%s", tf1, tf2); func[string(tf1) + "("] = string(tf2); funcID[string(tf1) + "("] = i; vfunc.push_back(string(tf1) + "("); } //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; for (auto it = func.begin(); it != func.end(); it++) { makeMap(funcID[it->first], it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); scanf("%s", s); s[strlen(s)] = '#'; string ans = solve(s); cout << ans << endl; } return 0; } //-------------------------------------------------------------------------------------6 #include <bits/stdc++.h> using namespace std; map<string, int> op; map<string, string> func; vector<string> vfunc; map<string, int> funcID; const int N = 1e3 + 10; char s[N], tf1[N], tf2[N]; int mp[N][N], c[N], topo[N], t, circle[N], n; string getNum(const char *s, int &i) //num { string res = ""; while (isdigit(s[i])) res += s[i], i++; if (s[i] == '.') res += s[i], i++; else return res; while (isdigit(s[i])) res += s[i], i++; return res; } string getOp(const char *s, int &i) //+ or - or * or / or ( or ) or int( or float( or func( { string res = ""; while (isalnum(s[i])) res += s[i], i++; res += s[i], i++; return res; } string getMark(const char *s, int &i) //num or fun name or par name { string res = ""; while (isalnum(s[i])) res += s[i], i++; return res; } string replace(string s, string p, string &t) //替换参数时没考虑f(x,xx)=x+xx这种参数子串问题 { //cout<<s<<" "<<p<<" "<<t<<endl; int idx = s.find(p); if (idx == -1) return s; if(idx>0 && isalnum(s[idx-1]) || idx+p.length()<s.length() && isalnum(s[idx+p.length()])) //参数子串 return s.substr(0,idx+p.length())+replace(s.substr(idx+p.length()),p,t); string tmp = s.substr(0, idx); if (idx > 0 && (tmp[idx - 1] == '+' || tmp[idx - 1] == '-' || tmp[idx - 1] == '*' || tmp[idx - 1] == '/')) tmp += "(" + t + ")"; //f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp += t; tmp += replace(s.substr(idx + p.length()), p, t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string solve(char *s) { stack<string> ops; stack<string> num; ops.push("#"); int i = 0; string neg = ""; if (s[i] == '-') neg = "-", i++; s[strlen(s)]='#'; while (s[i] != '#' || ops.top() != "#") { //printf("%c ",s[i]); if (isdigit(s[i])) { num.push(neg + getNum(s, i)); neg = ""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper = getOp(s, i); if (ops.top().find('(') != -1 && oper == ")") { string ope = ops.top(); ops.pop(); if (ope == "int(") { string num1 = num.top(); num.pop(); int idx; if ((idx = num1.find('.')) != -1) { num1 = num1.substr(0, idx); } num.push(num1); } else if (ope == "float(") { string num1 = num.top(); num.pop(); double a; sscanf(num1.c_str(), "%lf", &a); char tmp[20]; sprintf(tmp, "%.6f", a); num.push(tmp); } continue; } if (oper.find('(') != -1 || op[ops.top()] < op[oper]) { if (oper.find('(') != -1 && oper != "(" && oper != "int(" && oper != "float(") { if (circle[funcID[oper]]) return "No Answer"; string exp = replacePars(oper, s, i); char *exps = new char[exp.length()+10]; getOp(s, i); //")" //cout<<"after replace:"<<exp<<endl; strcpy(exps, exp.c_str()); string ans = solve(exps); if (ans == "No Answer") return ans; num.push(ans); continue; } ops.push(oper); if (oper.find('(') != -1) { if (s[i] == '-') neg = "-", i++; } continue; } for (int j = oper.length() - 1; j >= 0; j--) s[--i] = oper[j]; //cout<<"top"<<num.top()<<endl; string num1 = num.top(); num.pop(); string ope = ops.top(); ops.pop(); string num2 = num.top(); num.pop(); char num3[100]; if (num1.find('.') == -1 && num2.find('.') == -1) { long long a, b; sscanf(num1.c_str(), "%lld", &b); sscanf(num2.c_str(), "%lld", &a); //cout<<a<<ope<<b<<endl; long long ans; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%lld", ans); } else { double a, b, ans; sscanf(num1.c_str(), "%lf", &b); sscanf(num2.c_str(), "%lf", &a); //cout<<a<<ope<<b<<endl; if (ope == "+") ans = a + b; else if (ope == "-") ans = a - b; else if (ope == "*") ans = a * b; else if (ope == "/") ans = a / b; sprintf(num3, "%.6f", ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u] = -1; for (int v = 0; v < n; v++) if (mp[u][v]) { if (c[v] < 0) { circle[v] = circle[u] = 1; return false; } else if (!c[v] && !dfs(v)) { circle[v] = circle[u] = 1; return false; } } c[u] = 1; return true; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } void toposort() { memset(circle, 0, sizeof(circle)); memset(c, 0, sizeof(c)); for (int u = 0; u < n; u++) if (!c[u]) dfs(u); memset(c, 0, sizeof(c)); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); for (int u = 0; u < n; u++) if (!circle[u]) replaceFunc(u); //for (auto it = func.begin(); it != func.end(); it++) cout << it->first << it->second << endl; } void makeMap(int u, string &exp) { int i = exp.find('='); while (i < exp.length()) { if (!isalpha(exp[i])) { i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") continue; mp[u][funcID[fun]] = 1; } } int main() { op["#"] = 0; op["("] = 1; op["+"] = 2; op["-"] = 2; op["*"] = 3; op["/"] = 3; op[")"] = 1; op["int("] = 1; op["float("] = 1; while (scanf("%d", &n) == 1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp, 0, sizeof(mp)); for (int i = 0; i < n; i++) { getchar(); scanf("%[^(](%s", tf1, tf2); func[string(tf1) + "("] = string(tf2); funcID[string(tf1) + "("] = i; vfunc.push_back(string(tf1) + "("); } //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; for (auto it = func.begin(); it != func.end(); it++) { makeMap(funcID[it->first], it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); scanf("%s", s); string ans = solve(s); cout << ans << endl; } return 0; } //----------------------------------------------------------------------------7 #include <bits/stdc++.h> using namespace std; map<string,int>op; map<string,string>func; vector<string> vfunc; map<string,int>funcID; const int N=1e3+10; char s[N],tf1[N],tf2[N]; int mp[N][N],c[N],topo[N],t,circle[N],n; string getNum(const char *s,int &i) { string res=""; while(isdigit(s[i])) res+=s[i],i++; if(s[i]=='.') res+=s[i],i++; else return res; while(isdigit(s[i])) res+=s[i],i++; return res; } string getOp(const char *s,int &i) { string res=""; while(isalnum(s[i])) res+=s[i],i++; res+=s[i],i++; return res; } string getMark(const char *s,int &i) { string res=""; while(isalpha(s[i])) res+=s[i],i++; return res; } string replace(string s,string p,string &t)//替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p)==-1) return s; int idx=s.find(p); string tmp=s.substr(0,idx); if(idx>0 && (tmp[idx-1]=='+' || tmp[idx-1]=='-' || tmp[idx-1]=='*' || tmp[idx-1]=='/')) tmp+="("+t+")";//f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp +=t; tmp+=replace(s.substr(idx+p.length()),p,t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; //cout<<exp<<endl; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); if(fun.back()=='#') fun.pop_back(); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } string solve(char *s) { stack<string>ops; stack<string>num; ops.push("#"); int i=0; string neg=""; if(s[i]=='-') neg="-",i++; while(s[i]!='#' || ops.top()!="#") { //printf("%c ",s[i]); if(isdigit(s[i])) { num.push(neg+getNum(s,i)); neg=""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper=getOp(s,i); if(ops.top().find('(')!=-1 && oper==")") { string ope=ops.top(); ops.pop(); if(ope=="int(") { string num1=num.top();num.pop(); int idx; if((idx=num1.find('.'))!=-1) { num1=num1.substr(0,idx); } num.push(num1); } else if(ope=="float(") { string num1=num.top();num.pop(); double a; sscanf(num1.c_str(),"%lf",&a); char tmp[20]; sprintf(tmp,"%.6f",a); num.push(tmp); } continue; } if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) { if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") { if(circle[funcID[oper]]) return "No Answer"; replaceFunc(funcID[oper]); char *exps=new char[N]; string exp=func[oper]; int eqID=exp.find('='); string par=exp.substr(0,eqID); exp=exp.substr(eqID+1); string num1=getNum(s,i); int j=0; string par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); //cout<<"par:"<<par<<" exp:"<<exp<<" num1:"<<num1<<" par1:"<<par1<<endl; if(num1!="") exp=replace(exp,par1,num1); while(s[i]!=')') { getOp(s,i); num1=getNum(s,i); par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); exp=replace(exp,par1,num1); } getOp(s,i); //cout<<"after replace:"<<exp<<endl; strcpy(exps,exp.c_str()); string ans=solve(exps); if(ans=="No Answer") return ans; num.push(ans); continue; } ops.push(oper); if(oper.find('(')!=-1) { if(s[i]=='-') neg="-",i++; } continue; } for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; //cout<<"top"<<num.top()<<endl; string num1=num.top();num.pop(); string ope=ops.top();ops.pop(); string num2=num.top();num.pop(); char num3[100]; if(num1.find('.')==-1 && num2.find('.')==-1) { long long a,b; sscanf(num1.c_str(),"%lld",&b); sscanf(num2.c_str(),"%lld",&a); //cout<<a<<ope<<b<<endl; long long ans; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%lld",ans); } else { double a,b,ans; sscanf(num1.c_str(),"%lf",&b); sscanf(num2.c_str(),"%lf",&a); //cout<<a<<ope<<b<<endl; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%.6f",ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u]=-1; for(int v=0;v<n;v++) if(mp[u][v]) { if(c[v]<0) { circle[v]=circle[u]=1; return false; } else if(!c[v] && !dfs(v)) { circle[v]=circle[u]=1; return false; } } c[u]=1; return true; } void toposort() { memset(circle,0,sizeof(circle)); memset(c,0,sizeof(c)); for(int u=0;u<n;u++) if(!c[u]) dfs(u); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); } void makeMap(int u,string &exp) { int i=exp.find('='); while(i<exp.length()) { if(!isalpha(exp[i])) { i++; continue; } string fun=getOp(exp.c_str(),i); if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; mp[u][funcID[fun]]=1; } } int main() { op["#"]=0; op["("]=1; op["+"]=2; op["-"]=2; op["*"]=3; op["/"]=3; op[")"]=1; op["int("]=1; op["float("]=1; while(scanf("%d",&n)==1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { getchar(); scanf("%[^(](%s",tf1,tf2); func[string(tf1)+"("]=string(tf2)+"#"; funcID[string(tf1)+"("]=i; vfunc.push_back(string(tf1) + "("); } for(auto it=func.begin();it!=func.end();it++) { makeMap(funcID[it->first],it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; scanf("%s",s); s[strlen(s)]='#'; string ans=solve(s); cout<<ans<<endl; } return 0; } //8 内存超限 2017-05-23 01:29 949ms 268548kB c++ #include <bits/stdc++.h> using namespace std; map<string,int>op; map<string,string>func; vector<string> vfunc; map<string,int>funcID; const int N=1e3+10; char s[N],tf1[N],tf2[N]; int mp[N][N],c[N],topo[N],t,circle[N],n; string getNum(const char *s,int &i) { string res=""; while(isdigit(s[i])) res+=s[i],i++; if(s[i]=='.') res+=s[i],i++; else return res; while(isdigit(s[i])) res+=s[i],i++; return res; } string getOp(const char *s,int &i) { string res=""; while(isalnum(s[i])) res+=s[i],i++; res+=s[i],i++; return res; } string getMark(const char *s,int &i) { string res=""; while(isalpha(s[i])) res+=s[i],i++; return res; } string replace(string s,string p,string &t)//替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p)==-1) return s; int idx=s.find(p); string tmp=s.substr(0,idx); if(idx>0 && (tmp[idx-1]=='+' || tmp[idx-1]=='-' || tmp[idx-1]=='*' || tmp[idx-1]=='/')) tmp+="("+t+")";//f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp +=t; tmp+=replace(s.substr(idx+p.length()),p,t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; //cout<<exp<<endl; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); if(fun.back()=='#') fun.pop_back(); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } string solve(char *s) { stack<string>ops; stack<string>num; ops.push("#"); int i=0; string neg=""; if(s[i]=='-') neg="-",i++; while(s[i]!='#' || ops.top()!="#") { //printf("%c ",s[i]); if(isdigit(s[i])) { num.push(neg+getNum(s,i)); neg=""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper=getOp(s,i); if(ops.top().find('(')!=-1 && oper==")") { string ope=ops.top(); ops.pop(); if(ope=="int(") { string num1=num.top();num.pop(); int idx; if((idx=num1.find('.'))!=-1) { num1=num1.substr(0,idx); } num.push(num1); } else if(ope=="float(") { string num1=num.top();num.pop(); double a; sscanf(num1.c_str(),"%lf",&a); char tmp[20]; sprintf(tmp,"%.6f",a); num.push(tmp); } continue; } if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) { if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") { if(circle[funcID[oper]]) return "No Answer"; replaceFunc(funcID[oper]); string exp=func[oper]; int eqID=exp.find('='); string par=exp.substr(0,eqID); exp=exp.substr(eqID+1); string num1=getNum(s,i); int j=0; string par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); //cout<<"par:"<<par<<" exp:"<<exp<<" num1:"<<num1<<" par1:"<<par1<<endl; if(num1!="") exp=replace(exp,par1,num1); while(s[i]!=')') { getOp(s,i); num1=getNum(s,i); par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); exp=replace(exp,par1,num1); } getOp(s,i); //cout<<"after replace:"<<exp<<endl; char *exps=new char[exp.length()+10]; strcpy(exps,exp.c_str()); string ans=solve(exps); if(ans=="No Answer") return ans; num.push(ans); continue; } ops.push(oper); if(oper.find('(')!=-1) { if(s[i]=='-') neg="-",i++; } continue; } for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; //cout<<"top"<<num.top()<<endl; string num1=num.top();num.pop(); string ope=ops.top();ops.pop(); string num2=num.top();num.pop(); char num3[100]; if(num1.find('.')==-1 && num2.find('.')==-1) { long long a,b; sscanf(num1.c_str(),"%lld",&b); sscanf(num2.c_str(),"%lld",&a); //cout<<a<<ope<<b<<endl; long long ans; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%lld",ans); } else { double a,b,ans; sscanf(num1.c_str(),"%lf",&b); sscanf(num2.c_str(),"%lf",&a); //cout<<a<<ope<<b<<endl; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%.6f",ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u]=-1; for(int v=0;v<n;v++) if(mp[u][v]) { if(c[v]<0) { circle[v]=circle[u]=1; return false; } else if(!c[v] && !dfs(v)) { circle[v]=circle[u]=1; return false; } } c[u]=1; return true; } void toposort() { memset(circle,0,sizeof(circle)); memset(c,0,sizeof(c)); for(int u=0;u<n;u++) if(!c[u]) dfs(u); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); } void makeMap(int u,string &exp) { int i=exp.find('='); while(i<exp.length()) { if(!isalpha(exp[i])) { i++; continue; } string fun=getOp(exp.c_str(),i); if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; mp[u][funcID[fun]]=1; } } int main() { op["#"]=0; op["("]=1; op["+"]=2; op["-"]=2; op["*"]=3; op["/"]=3; op[")"]=1; op["int("]=1; op["float("]=1; while(scanf("%d",&n)==1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { getchar(); scanf("%[^(](%s",tf1,tf2); func[string(tf1)+"("]=string(tf2)+"#"; funcID[string(tf1)+"("]=i; vfunc.push_back(string(tf1) + "("); } for(auto it=func.begin();it!=func.end();it++) { makeMap(funcID[it->first],it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; scanf("%s",s); s[strlen(s)]='#'; string ans=solve(s); cout<<ans<<endl; } return 0; } //9 #include <bits/stdc++.h> using namespace std; map<string,int>op; map<string,string>func; vector<string> vfunc; map<string,int>funcID; const int N=1e3+10; char s[N],tf1[N],tf2[N]; int mp[N][N],c[N],topo[N],t,circle[N],n; string getNum(const char *s,int &i) { string res=""; while(isdigit(s[i])) res+=s[i],i++; if(s[i]=='.') res+=s[i],i++; else return res; while(isdigit(s[i])) res+=s[i],i++; return res; } string getOp(const char *s,int &i) { string res=""; while(isalnum(s[i])) res+=s[i],i++; res+=s[i],i++; return res; } string getMark(const char *s,int &i) { string res=""; while(isalpha(s[i])) res+=s[i],i++; return res; } string replace(string s,string p,string &t)//替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p)==-1) return s; int idx=s.find(p); string tmp=s.substr(0,idx); if(idx>0 && (tmp[idx-1]=='+' || tmp[idx-1]=='-' || tmp[idx-1]=='*' || tmp[idx-1]=='/')) tmp+="("+t+")";//f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp +=t; tmp+=replace(s.substr(idx+p.length()),p,t); return tmp; } string replacePars(string funcName, const char *s, int &i) { string exp = func[funcName]; //cout<<"before:"<<exp; int eqID = exp.find('='); string par = exp.substr(0, eqID); exp = exp.substr(eqID + 1); string num1 = getMark(s, i); int j = 0; string par1 = getMark(par.c_str(), j); //cout << "par:" << par << " exp:" << exp << " num1:" << num1 << " par1:" << par1 << endl; if (num1 != "") exp = replace(exp, par1, num1); while (s[i] != ')') { getOp(s, i); getOp(par.c_str(), j); num1 = getMark(s, i); string par1 = getMark(par.c_str(), j); exp = replace(exp, par1, num1); } //cout<<" after:"<<exp<<endl; return exp; } string replaceFunc(int u) { if (circle[u]) return "No Answer"; string funcName = vfunc[u]; string exp = func[funcName]; //cout<<exp<<endl; int i = exp.find('='); i++;//= string replaceExp = exp.substr(0, i); while (i<exp.length()) { if (!isalpha(exp[i])) { replaceExp += exp[i]; i++; continue; } string fun = getOp(exp.c_str(), i); if (fun.find('(') == -1 || fun == "int(" || fun == "float(") { replaceExp += fun; continue; } string funExp = replaceFunc(funcID[fun]); //cout<<"fun1:"<<fun<<" "; fun = replacePars(fun, exp.c_str(), i); if(fun.back()=='#') fun.pop_back(); getOp(exp.c_str(),i);//) //cout<<"fun2:"<<fun<<endl; //char lastc=replaceExp[replaceExp.length()-1]; //if (lastc == '+' || lastc == '-' || lastc == '*' || lastc == '/') replaceExp+="("+fun+")"; //else replaceExp+=fun; } //cout<<replaceExp<<endl; func[funcName] = replaceExp; return replaceExp; } string solve(char *s) { stack<string>ops; stack<string>num; ops.push("#"); int i=0; string neg=""; if(s[i]=='-') neg="-",i++; while(s[i]!='#' || ops.top()!="#") { //printf("%c ",s[i]); if(isdigit(s[i])) { num.push(neg+getNum(s,i)); neg=""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper=getOp(s,i); if(ops.top().find('(')!=-1 && oper==")") { string ope=ops.top(); ops.pop(); if(ope=="int(") { string num1=num.top();num.pop(); int idx; if((idx=num1.find('.'))!=-1) { num1=num1.substr(0,idx); } num.push(num1); } else if(ope=="float(") { string num1=num.top();num.pop(); double a; sscanf(num1.c_str(),"%lf",&a); char tmp[20]; sprintf(tmp,"%.6f",a); num.push(tmp); } continue; } if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) { if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") { if(circle[funcID[oper]]) return "No Answer"; replaceFunc(funcID[oper]); string exp=func[oper]; int eqID=exp.find('='); string par=exp.substr(0,eqID); exp=exp.substr(eqID+1); string num1=getNum(s,i); int j=0; string par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); //cout<<"par:"<<par<<" exp:"<<exp<<" num1:"<<num1<<" par1:"<<par1<<endl; if(num1!="") exp=replace(exp,par1,num1); while(s[i]!=')') { getOp(s,i); num1=getNum(s,i); par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); exp=replace(exp,par1,num1); } getOp(s,i); //cout<<"after replace:"<<exp<<endl; char *exps=new char[exp.length()+10]; strcpy(exps,exp.c_str()); string ans=solve(exps); delete exps; if(ans=="No Answer") return ans; num.push(ans); continue; } ops.push(oper); if(oper.find('(')!=-1) { if(s[i]=='-') neg="-",i++; } continue; } for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; //cout<<"top"<<num.top()<<endl; string num1=num.top();num.pop(); string ope=ops.top();ops.pop(); string num2=num.top();num.pop(); char num3[100]; if(num1.find('.')==-1 && num2.find('.')==-1) { long long a,b; sscanf(num1.c_str(),"%lld",&b); sscanf(num2.c_str(),"%lld",&a); //cout<<a<<ope<<b<<endl; long long ans; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%lld",ans); } else { double a,b,ans; sscanf(num1.c_str(),"%lf",&b); sscanf(num2.c_str(),"%lf",&a); //cout<<a<<ope<<b<<endl; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%.6f",ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u]=-1; for(int v=0;v<n;v++) if(mp[u][v]) { if(c[v]<0) { circle[v]=circle[u]=1; return false; } else if(!c[v] && !dfs(v)) { circle[v]=circle[u]=1; return false; } } c[u]=1; return true; } void toposort() { memset(circle,0,sizeof(circle)); memset(c,0,sizeof(c)); for(int u=0;u<n;u++) if(!c[u]) dfs(u); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); } void makeMap(int u,string &exp) { int i=exp.find('='); while(i<exp.length()) { if(!isalpha(exp[i])) { i++; continue; } string fun=getOp(exp.c_str(),i); if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; mp[u][funcID[fun]]=1; } } int main() { op["#"]=0; op["("]=1; op["+"]=2; op["-"]=2; op["*"]=3; op["/"]=3; op[")"]=1; op["int("]=1; op["float("]=1; while(scanf("%d",&n)==1) { vfunc.clear(); funcID.clear(); func.clear(); memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { getchar(); scanf("%[^(](%s",tf1,tf2); func[string(tf1)+"("]=string(tf2)+"#"; funcID[string(tf1)+"("]=i; vfunc.push_back(string(tf1) + "("); } for(auto it=func.begin();it!=func.end();it++) { makeMap(funcID[it->first],it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; scanf("%s",s); s[strlen(s)]='#'; string ans=solve(s); cout<<ans<<endl; } return 0; } //10 #include <bits/stdc++.h> using namespace std; map<string,int>op; map<string,string>func; map<string,int>funcID; map<string,string>calFunc; const int N=1e3+10; char s[N],tf1[N],tf2[N]; int mp[N][N],c[N],topo[N],t,circle[N],n; string getNum(const char *s,int &i) { string res=""; while(isdigit(s[i])) res+=s[i],i++; if(s[i]=='.') res+=s[i],i++; else return res; while(isdigit(s[i])) res+=s[i],i++; return res; } string getOp(const char *s,int &i) { string res=""; while(isalnum(s[i])) res+=s[i],i++; res+=s[i],i++; return res; } string getMark(const char *s,int &i) { string res=""; while(isalpha(s[i])) res+=s[i],i++; return res; } string replace(string s,string p,string &t)//替换参数时没考虑f(x,xx)=x+xx这种参数字串问题 { if (s.find(p)==-1) return s; int idx=s.find(p); string tmp=s.substr(0,idx); if(idx>0 && (tmp[idx-1]=='+' || tmp[idx-1]=='-' || tmp[idx-1]=='*' || tmp[idx-1]=='/')) tmp+="("+t+")";//f(x,y)=f1(y,x) ==>f(x,y)=f1((num1),(num2)); else tmp +=t; tmp+=replace(s.substr(idx+p.length()),p,t); return tmp; } string solve(char *s) { stack<string>ops; stack<string>num; ops.push("#"); int i=0; string neg=""; if(s[i]=='-') neg="-",i++; while(s[i]!='#' || ops.top()!="#") { //printf("%c ",s[i]); if(isdigit(s[i])) { num.push(neg+getNum(s,i)); neg=""; //cout<<"numtop:"<<num.top()<<endl; continue; } string oper=getOp(s,i); if(ops.top().find('(')!=-1 && oper==")") { string ope=ops.top(); ops.pop(); if(ope=="int(") { string num1=num.top();num.pop(); int idx; if((idx=num1.find('.'))!=-1) { num1=num1.substr(0,idx); } num.push(num1); } else if(ope=="float(") { string num1=num.top();num.pop(); double a; sscanf(num1.c_str(),"%lf",&a); char tmp[20]; sprintf(tmp,"%.6f",a); num.push(tmp); } continue; } if(oper.find('(')!=-1 || op[ops.top()]<op[oper]) { if(oper.find('(') != -1 && oper!="(" && oper!="int(" && oper!="float(") { if(circle[funcID[oper]]) return "No Answer"; int calIdx=strchr(s+i,')')-s-i; char *exps=new char[N]; memcpy(exps,s+i,calIdx); string funcAndPars=exps; auto it=calFunc.find(funcAndPars); if(it!=calFunc.end()) { delete []exps; string ans=it->second; if(ans=="No Answer") return ans; num.push(ans); continue; } string exp=func[oper]; int eqID=exp.find('='); string par=exp.substr(0,eqID); exp=exp.substr(eqID+1); string num1=getNum(s,i); int j=0; string par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); //cout<<"par:"<<par<<" exp:"<<exp<<" num1:"<<num1<<" par1:"<<par1<<endl; if(num1!="") exp=replace(exp,par1,num1); while(s[i]!=')') { getOp(s,i); num1=getNum(s,i); par1=getOp(par.c_str(),j); par1=par1.substr(0,par1.length()-1); exp=replace(exp,par1,num1); } getOp(s,i); //cout<<"after replace:"<<exp<<endl; strcpy(exps,exp.c_str()); string ans=solve(exps); calFunc[funcAndPars]=ans; delete []exps; if(ans=="No Answer") return ans; num.push(ans); continue; } ops.push(oper); if(oper.find('(')!=-1) { if(s[i]=='-') neg="-",i++; } continue; } for(int j=oper.length()-1;j>=0;j--) s[--i]=oper[j]; //cout<<"top"<<num.top()<<endl; string num1=num.top();num.pop(); string ope=ops.top();ops.pop(); string num2=num.top();num.pop(); char num3[100]; if(num1.find('.')==-1 && num2.find('.')==-1) { long long a,b; sscanf(num1.c_str(),"%lld",&b); sscanf(num2.c_str(),"%lld",&a); //cout<<a<<ope<<b<<endl; long long ans; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%lld",ans); } else { double a,b,ans; sscanf(num1.c_str(),"%lf",&b); sscanf(num2.c_str(),"%lf",&a); //cout<<a<<ope<<b<<endl; if(ope=="+") ans=a+b; else if(ope=="-") ans=a-b; else if(ope=="*") ans=a*b; else if(ope=="/") ans=a/b; sprintf(num3,"%.6f",ans); } //cout<<"num3:"<<num3<<endl; num.push(num3); } return num.top(); } bool dfs(int u) { c[u]=-1; for(int v=0;v<n;v++) if(mp[u][v]) { if(c[v]<0) { circle[v]=circle[u]=1; return false; } else if(!c[v] && !dfs(v)) { circle[v]=circle[u]=1; return false; } } c[u]=1; return true; } void toposort() { memset(circle,0,sizeof(circle)); memset(c,0,sizeof(c)); for(int u=0;u<n;u++) if(!c[u]) dfs(u); //for(int i=0;i<n;i++) printf("circle[%d]=%d ",i,circle[i]); } void makeMap(int u,string &exp) { int i=exp.find('='); while(i<exp.length()) { if(!isalpha(exp[i])) { i++; continue; } string fun=getOp(exp.c_str(),i); if(fun.find('(')==-1 || fun=="int(" || fun=="float(") continue; mp[u][funcID[fun]]=1; } } int main() { op["#"]=0; op["("]=1; op["+"]=2; op["-"]=2; op["*"]=3; op["/"]=3; op[")"]=1; op["int("]=1; op["float("]=1; while(scanf("%d",&n)==1) { memset(mp,0,sizeof(mp)); for(int i=0;i<n;i++) { getchar(); scanf("%[^(](%s",tf1,tf2); func[string(tf1)+"("]=string(tf2)+"#"; funcID[string(tf1)+"("]=i; } for(auto it=func.begin();it!=func.end();it++) { makeMap(funcID[it->first],it->second); } //for(int i=0;i<n;i++) //{ // for(int j=0;j<n;j++) printf("%d ",mp[i][j]); // printf(" "); //} toposort(); //for(auto it=func.begin();it!=func.end();it++) cout<<it->first<<it->second<<endl; scanf("%s",s); s[strlen(s)]='#'; string ans=solve(s); cout<<ans<<endl; } return 0; } //11 ...
第三场rank95 题解
A. 腾讯课堂的物理实验
1A
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int L,t,T; 6 while(scanf("%d%d%d",&L,&t,&T)==3) 7 { 8 int a=(T-t)%L; 9 int aa=(T-t)/L; 10 if(!(aa&1)) a=L-a; 11 int b=T%L; 12 int bb=T/L; 13 if(b==0) bb--,b=L; 14 if(bb&1) b=L-b; 15 printf("%d ",max(a,b)-min(b,a)); 16 } 17 return 0; 18 }
B. 腾讯狼人杀(简单)
1A
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=21; 4 int n,m,mp[N][N],v[N],ans; 5 double maxans; 6 void dfs(int x,int sum,int num) 7 { 8 if(x==n+1) 9 { 10 maxans=max(maxans,ans/(1.0*num*(2*n-num))); 11 //printf("%d %d %d %lf ",x,sum,num,maxans); 12 return; 13 } 14 for(int i=1;i<=n;i++) if(mp[i][x] && (sum>>i)%2) ans+=mp[i][x]; 15 dfs(x+1,sum|(1<<x),num+1); 16 for(int i=1;i<=n;i++) if(mp[i][x] && (sum>>i)%2) ans-=mp[i][x]; 17 if(v[x]) return; 18 dfs(x+1,sum,num); 19 return; 20 } 21 int main() 22 { 23 while(scanf("%d%d",&n,&m)==2) 24 { 25 memset(v,0,sizeof(v)); 26 memset(mp,0,sizeof(mp)); 27 for(int i=0,U,V,W;i<m;i++) 28 { 29 scanf("%d%d%d",&U,&V,&W); 30 mp[U][V]=mp[V][U]=W; 31 } 32 for(int i=1;i<=n;i++) scanf("%d",v+i); 33 ans=0; 34 maxans=0; 35 dfs(1,0,0); 36 printf("%.4lf ",maxans); 37 } 38 return 0; 39 }