fork(4) download
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  6.  
  7. int main(int argc, char** argv) {
  8. char str[50];
  9. cin>>str;
  10. int n=strlen(str);
  11. int dp[n+2][n+2];
  12. int i,j;
  13.  
  14. for(i=0;i<n;i++)
  15. {
  16. dp[i][i]=dp[i+1][i]=1L;
  17. }
  18.  
  19. for(i=2;i<=n;i++)
  20. {
  21. for(j=0;j<=n-i;j++)
  22. {
  23. int end= j+i-1;
  24. dp[j][end]=dp[j+1][end];
  25.  
  26. if(str[j]=='(')
  27. {
  28. for(int k=j+1;k<=end ;k++)
  29. {
  30. if(str[k]==')')
  31. dp[j][end]+= dp[j+1][k-1]*dp[k+1][end];
  32. }
  33. }
  34.  
  35. }
  36. }
  37. cout<<dp[0][n-1]<<endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3344KB
stdin
()()()
stdout
14