The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 10<sup class="upper-index">6</sup> characters long.
#include <bits/stdc++.h>
using namespace std;
char s[1000005] = {'\0'};
char s1[1000005] = {'\0'};
int cnt = 0;
int main()
{
	scanf("%s", s);
	for (int i = 0; s[i]; i++) cnt += s[i] == 'Q';
	int Cnt = sqrt(cnt);
	while (Cnt * Cnt > cnt) Cnt--;
	while (Cnt * Cnt < cnt) Cnt++;
	if (Cnt * Cnt != cnt)
	{
		puts("No");
		return 0;
	}
	else cnt = Cnt;
	int l = strlen(s);
	if (cnt == 0)
	{
		puts("Yes");
		return 0;
	}
	if ((l + cnt) % (cnt + 1))
	{
		puts("No");
		return 0;
	}
	int l1 = (l + cnt) / (cnt + 1), st = 0;
	for (int i = 0; i < l; i++)
		if (s[i] == 'Q')
		{
			st = i / 2;
			break;
		}
	int tot = 0;
	for (int i = st; i < st + l1; i++)
		if (s[i] == 'H') s1[tot++] = 'H';
		else for (int j = st; j < st + l1; j++)
			s1[tot++] = s[j];
	if (tot != l)
	{
		puts("No");
		return 0;
	}
	for (int i = 0; i < tot; i++)
		if (s[i] != s1[i])
		{
			puts("No");
			return 0;
		}
	puts("Yes");
	return 0;
}