C++ 中的名称冲突之 "y1"
已经是第二次遇到这个问题了:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 5 const int N(105); 6 7 int dp[N][2][N][2][N]; 8 int x1, x2, y1, y2; 9 10 int dfs(int x1, int s, int x2, int t, int y){ 11 int & now=dp[x1][s][x2][t][y]; 12 if(~now) return now; 13 int sum=0, cnt=0; 14 if(s==0){ 15 sum+=dfs(x1, 1, x2, t, y-1), cnt++; 16 if(t==0) 17 sum+=dfs(x1, s, x2, 1, y-1), cnt++; 18 else if(x2>0) 19 sum+=dfs(x1, 0, x2-1, t, y), cnt++; 20 } 21 else{ 22 if(x1>0) sum+=dfs(x1-1, s, x2, t, y), cnt++; 23 if(t==0) 24 sum+=dfs(x1, s, x2, 1, y-1), cnt++; 25 else if(x2>0) 26 sum+=dfs(x1, s, x2-1, t, y), cnt++; 27 } 28 29 if((y1+1)*(1-s)+(y2+1)*(1-t)< y) //forbidden numbers; 30 sum+=dfs(x1, s, x2, t, y-1), cnt++; 31 32 return now = sum!=cnt; 33 } 34 35 int a[N]; 36 37 int main(){ 38 int T; 39 int n; 40 for(cin>>T; T--; ){ 41 cin>>n; 42 int p; 43 for(int i=0; i<n; i++){ 44 cin>>a[i]; 45 if(a[i]==1) p=i; 46 } 47 x1=0, x2=0, y1=0, y2=0; 48 int i; 49 50 for(i=p-1; i>=0 && a[i]>a[i+1]; i--, x1++); 51 52 for(; i>=0 && a[i]<a[i+1]; i--, y1++); 53 for(i=p+1; i<n && a[i]>a[i-1]; i++, x2++); 54 for(; i<n && a[i]<a[i-1]; i++, y2++); 55 56 memset(dp, -1, sizeof(dp)); 57 x1=max(x1-1, 0), x2=max(x2-1, 0); 58 59 // cout<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<endl; 60 int peak=(p!=0) + (p!=n-1); 61 for(int i=0; i<=n-1-x1-x2-peak; i++) 62 dp[0][1][0][1][i]=1; //error-prone 63 64 int s=p==0, t=p==n-1; //error-prone 65 66 int res=dfs(x1, s, x2, t, n-1-x1-x2); 67 // cout<<res<<endl; 68 puts(res==1?"Alice": "Bob"); 69 } 70 return 0; 71 }
试图在终端编译,运行: (gcc version 4.8.4)
g++ main.cpp -std=c++11 -o main && ./main <in
返回结果:
main.cpp:8:13: error: ‘int y1’ redeclared as different kind of symbol int x1, x2, y1, y2; ^ In file included from /usr/include/features.h:374:0, from /usr/include/assert.h:35, from /usr/include/c++/4.8/cassert:43, from /usr/include/x86_64-linux-gnu/c++/4.8/bits/stdc++.h:33, from main.cpp:1: /usr/include/x86_64-linux-gnu/bits/mathcalls.h:241:1: error: previous declaration of ‘double y1(double)’ __MATHCALL (y1,, (_Mdouble_)); ^
显示变量 y1 和 C++ 标准库中的某个变量名称冲突,这个问题应当引起注意。
另外这不是头文件写成 <bits/stdc++.h> 引起的,即使换成各具体的头文件(<iostream>, <algorithm>, <ctring>)还是会发生这个错误。
具体原因及解决办法还有待研究。