FOJ2030(DFS:括号匹配)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream>
usingnamespace 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;
}
return0;
}
usingnamespace 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;
}
return0;
}