Problem:
Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

The input is given from Standard Input in the following format:

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int mod=924844033;
int edgenum,head[4000005],nex[4000005],vet[4000005],num[4000005],siz[4000005];
int fac[4000005],inv[4000005],c[4000005],d[4000005],ans[4000005],re[4000005],G[2];
int n,m,len,pr[4000005];
int ksm(int a,int b,int mod)
{
	int s=1;
	while (b>0)
	{
		if (b&1) s=(long long)s*a%mod;
		a=(long long)a*a%mod,b>>=1;
	}
	return s;
}
void Pre(int N)
{
	fac[0]=1;
	for (int i=1;i<=N;i++) fac[i]=(long long)fac[i-1]*i%mod;
	inv[N]=ksm(fac[N],mod-2,mod);
	for (int i=N-1;i>=0;i--) inv[i]=(long long)inv[i+1]*(i+1)%mod;
}
int C(int a,int b)
{
	return (long long)fac[a]*fac[b]%mod*fac[a-b]%mod;
}
void addedge(int u,int v)
{
	edgenum++,vet[edgenum]=v,nex[edgenum]=head[u],head[u]=edgenum;
}

void dfs(int u,int pre)
{
	siz[u]=1;
	for (int i=head[u];i!=0;i=nex[i])
		if (vet[i]!=pre) dfs(vet[i],u),siz[u]+=siz[vet[i]];
	num[siz[u]]=(num[siz[u]]+mod-1)%mod;
	if (pre!=0) num[n-siz[u]]=(num[n-siz[u]]+mod-1)%mod;
}

int calcg(int p)
{
	int m=p-1,tot=0;
	for (int i=2;i<=sqrt(p);i++)
		if (m%i==0)
		{
			pr[++tot]=i;
			while (m%i==0) m/=i;
		}
	if (m>1) pr[++tot]=m;
	for (int i=2;;i++)
	{
		int flag=0;
		for (int j=1;j<=tot;j++)
			if (ksm(i,(p-1)/pr[j],p)==1) {flag=1;break;}
		if (flag==0) return i;
	}
	return 0;
		
}

void NTT(int *num,int len,int flag)
{
	for (int i=0;i<len;i++)
		re[i]=(re[i>>1]>>1)+(i&1)*(len>>1);
	for (int i=0;i<len;i++)
		if (i<re[i]) swap(num[i],num[re[i]]);
		
	for (int i=1;i<len;i<<=1)
	{
		int w0=ksm(G[flag],(mod-1)/i/2,mod);
		for (int j=0;j<len;j+=i+i)
			for (int r=0,k=1;r<i;r++,k=(long long)k*w0%mod)
			{
				int x=num[j+r],y=(long long)num[i+j+r]*k%mod;
				num[j+r]=(x+y)%mod,num[i+j+r]=(x-y+mod)%mod;
			}
	}
	int invn=ksm(len,mod-2,mod);
	if (flag==1)
		for (int i=0;i<len;i++) num[i]=(long long)num[i]*invn%mod;
}

int main()
{
	Pre(2000000);
	scanf("%d",&n);
	for (int i=1,x,y;i<n;i++)
		scanf("%d%d",&x,&y),addedge(x,y),addedge(y,x);
	dfs(1,0),num[n]=n;
	for (int i=1;i<=n;i++) c[i]=(long long)num[i]*fac[i]%mod;
	for (int i=0;i<=n;i++) d[i]=inv[n-i];
	for (len=1;len<=n+n;len<<=1);
	G[0]=calcg(mod),G[1]=ksm(G[0],mod-2,mod);
	NTT(c,len,0),NTT(d,len,0);
	for (int i=0;i<len;i++) c[i]=(long long)c[i]*d[i]%mod;
	NTT(c,len,1);
	for (int i=1;i<=n;i++)
		ans[i]=(long long)c[i+n]*inv[i]%mod;
	for (int i=1;i<=n;i++)
		printf("%d\n",ans[i]);
	return 0;
}