原题
原题链接点击此处
题意
用 ’ ( ’ 和 ’ ) '将A , B , C 替代,并且A , B , C 只能用一种括号替代;
实质为括号匹配 ;
题解思路
枚举;
ABC替换为’ ( ’ 和 ’ ) ’ 共有 2^3 = 8 种情况
而其中ABC全为’ ( ’ 或 ’ ) ’ 的情况有2种,所有一共有6种情况;
然后可设’ ( ’ = 1 , ’ ) ’ = -1 , 进行枚举;
代码
#include<iostream>
using namespace std ;
int x[6][3] = {1,-1,1, 1,-1,-1, 1,1,-1, -1,-1,1, -1,1,-1, -1,1,1 };
int main()
{
int t ;
cin >> t ;
while( t -- )
{
string str ;
int a = 0 , b = 0 ;
cin >> str ;
int len = str.length() ;
for( int i = 0 ; i < 6 ; i ++ )
{
int ans = 0 , j ;
for( j = 0 ; j < len && ans <= 0 ; j ++ )
ans += x[i][str[j] - 'A'] ;
if( ans == 0 && j == len )
{
a = 1 ;
break ;
}
}
if( a == 1 )
cout << "YES" << endl ;
else
cout << "NO" << endl ;
}
return 0 ;
}