FOJ2030(DFS:括号匹配)

View Code
#include < iostream >
using namespace std;
char ch[ 101 ];
int mark = 0 , cnt;
int len;
void dfs( int x , int y , int i )
{
if ( x < y )
{
return ; }
if ( y == x && i == len )
{
cnt
++ ;
}

if (ch[i] == ' ? ' )
{
dfs(x
+ 1 , y , i + 1 );
dfs(x, y
+ 1 , i + 1 );
}

if (ch[i] == ' ( ' )
{

dfs( x
+ 1 , y , i + 1 );

}

if (ch[i] == ' ) ' )
{
dfs( x , y
+ 1 , i + 1 );
}

}
int main()
{
while (cin >> ch)
{
cnt
= 0 ; len = strlen( ch );
dfs(
0 , 0 , 0 );

if (mark == 1 ) cout << " 0 " << endl;

else cout << cnt << endl;
}
return 0 ;

}

你可能感兴趣的:(DFS)