j,b;B(char*S){char*s=calloc(strlen(S)+2,b=1)+1;for(j=0;S[j];b*=(S[j]<62||*--s==60)*(S[j++]-41||*--s==40))S[j]==60?*s++=60:0,S[j]<41?*s++=40:0;return*s>0&*--s<1&b;}f(S,n,k)char*S;{if(n<strlen(S))for(k=2;k--;)S[n]==46-k-k?S[n]=40+k*20,f(S,n+1),S[n]=41+k*21,f(S,-~n),S[n]=46-k-k:0;else B(S)&&puts(S);}F(int*S){f(S,0);}
在线尝试!
j,b;B(char*S){char*s=calloc(strlen(S)+2,1)+1;for(b=1,j=0;S[j];j++){if(S[j]==60)*s++=60;if(S[j]<41)*s++=40;b*=!(S[j]>61&&*--s!=60)*!(S[j]==41&&*--s!=40);}return*s>0&*--s<1&b;}f(S,n,k)char*S;{if(n>=strlen(S))return B(S)&&puts(S);for(k=0;k<2;k++)S[n]==46-k-k&&(S[n]=40+k*20,f(S,n+1),S[n]=41+k*21,f(S,-~n),S[n]=46-k-k);}F(char*S){f(S,0);}
在线尝试!
说明(旧版本)
j,b;B(char*S){ // determine if string is balanced
char*s=calloc(strlen(S)+2,1)+1; // array to store matching brackets
for(b=1,j=0;S[j];j++){ // loop through string (character array)
if(S[j]==60)*s++=60; // 60 == '<', opening bracket
if(S[j]<41)*s++=40; // 40 == '(', opening bracket
b*=!(S[j]>61&&*--s!=60)* // 62 == '>', closing bracket
!(S[j]==41&&*--s!=40);} // 41 == ')', closing bracket
return*s>0&*--s<1&b;} // no unmatched brackets and no brackets left to match
f(S,n,k)char*S;{ // helper function, recursively guesses brackets
if(n>=strlen(S)) // string replaced by possible bracket layout
return B(S)&&puts(S); // print if balanced, return in all cases
for(k=0;k<2;k++) // 46 == '.', guess 40 == '(',
S[n]==46-k-k&&(S[n]=40+k*20, // guess 41 == '(', restore
f(S,n+1),S[n]=41+k*21, // 44 == ',', guess 60 == '<',
f(S,-~n),S[n]=46-k-k);} // guess 62 == '>', restore
F(char*S){f(S,0);} // main function, call helper function
在线尝试!