分析文法发现,
字母A左边的A的个数 * 字母P,T间的A的个数 = 字母T右边的A的个数
源码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string s;
cin >> n;
while(n --)
{
cin >> s;
int p_index=-1, t_index=-1;
bool more_than_one = false, non_pat = false;
for (size_t i = 0; i <s.size(); ++ i)
{
if (s[i] == 'P')
{
if (p_index == -1)
{
p_index = i;
} else
{
more_than_one = true;
break;
}
} else if (s[i] == 'T')
{
if (t_index == -1)
{
t_index = i;
} else
{
more_than_one = true;
break;
}
} else if (s[i] != 'A')
{
non_pat = true;
}
}
if (non_pat == true
|| p_index == -1
|| t_index == -1
|| p_index+1 >= t_index
|| more_than_one==true
|| p_index*(t_index-p_index-1) != (s.size()-1-t_index))
{
// cout << p_index << " " << t_index << endl;
cout << "NO" << endl;
} else
{
cout << "YES" << endl;
}
}
return 0;
}