stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1921280307846014x):
#include <iostream>

#include <string>

using namespace std;



char ch[200000];



int main() {

	std::ios::sync_with_stdio(false);

	string str;

	cin>>str;

	int i=0;

	int t=-1;

	int len=str.length();

	int countt=0;

	while(i<len){

		if(str[i]=='S'){

			++t;

			ch[t]='S';

		}

		if(str[i]=='T'){

			if(t==-1){

				++countt;

			}

			if(t>=0){

				--t;

			}

		}

		++i;

	}

	printf("%i\n",t+1+countt);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.2083587688890176x):
#include <stdio.h>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <string>

#include <stack>

using namespace std;

const int N=200500; 

char a[N];

int main() 

{

	stack<char> b;

	scanf("%s",&a);

	int size=strlen(a);

	char ch;

	b.push(a[0]);

	for(int i=1;i<size;i++)

	{

		if(!b.empty())

		{

			ch=b.top();

			if(ch=='S'&&a[i]=='T') 

				b.pop();

			else 

				b.push(a[i]); 

		}

		else b.push(a[i]);	

	}

	int size1=b.size();

	printf("%d\n",size1);

	return 0;

 }
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.3006038376202436x):
#include<stdio.h>

#include<string.h>

#include<vector>

using namespace std;

vector<char> Q;

int main()

{

	char s[200005];

	scanf("%s",s);//字符串

	int len=strlen(s);

	//printf("%d\n",len);

	for(int i=0;i<len;i++){

		if(!Q.empty()&&s[i]=='T'&&Q.back()=='S'){

			//把S拿出来

			Q.pop_back();

		}

		else

			Q.push_back(s[i]);//入数组

	}

	int cnt=0;

	while(!Q.empty()){

		Q.pop_back();

		cnt++;

	}

	printf("%d\n",cnt);

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1956579303261246x):
#include<iostream>

#include<string.h>

using namespace std;

int main()

{

	string s;

	cin>>s;

//	int len=s.length();

	for(int i=0;i<s.size()-1&&s.size()>0;i++)

	//for(int i=0;i<len-1&&len>0;i++)

	{

		if(s[i]=='S'&&s[i+1]=='T')

		{

			s.erase(i,2);

			if(i==0)

			{

				i--;

			}

			else

			{

				i=i-2;

			}

		}

	}

	cout<<s.size()<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.2005574237202148x):
#include <cstdio>

#include <iostream>

#include <stack>

#include <string>

using namespace std;



int main()

{

	stack<char> q;

	string s;

	cin >> s;

	for (int i = 0; i < s.length(); i++)

	{

		if (s[i] == 'S')

		{

			q.push(s[i]);

		}

		else

		{

			if (!q.empty() && q.top() != 'T')

			{

				q.pop();

			}

			else

			{

				q.push(s[i]);

			}

		}

	}

	printf("%d", q.size());





	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.206142373982148x):
#include<iostream>

#include<string>

using namespace std;

const int maxn = 200010;

//定义链表结构体

struct At {

	char c;		//该位置字符

	At* last;   //上一个节点

	At* next;   //下一个节点

}	c[maxn];

//主函数

int main() {

	//使用cin>>string以方便读入

	string s;

	cin >> s;



	//转存到链表中

	int size = s.size();

	///第一个节点和最后一个节点需要单独处理

	c[0] = { s[0],NULL,&c[1] };						///第一个节点的前一个节点为空

	///遍历中间的size-2个节点

	for (int i = 1; i < size - 1; i++) {

		c[i] = { s[i],&c[i - 1],&c[i + 1] };

	}

	c[size - 1] = { s[size - 1],&c[size - 2],NULL };///最后一个节点的后一个节点为空



	//

	At* now = &c[0];

	while (true) {

		//int update = 0;

		At* i = now;

		for (; i->next != NULL; i = i->next) {

			if (i->c == 'S'&&i->next->c == 'T') {

				if (i->last != NULL) {

					now = i->last;

					now->next = i->next->next;

					if (i->next->next != NULL)

						i->next->next->last = now;

				}

				else {

					now = i->next->next;

					if (i->next->next != NULL)

						i->next->next->last = NULL;

				}

				size -= 2;

				//update = 1;

				break;

			}

		}

		if (size == 0 || i->next == NULL || now == NULL)

			break;

	}

	cout << size << endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1967895024884714x):
#include<iostream>

#include<string>

using namespace std;

const int maxn = 200010;

//定义链表结构体

struct At {

	char c;		//该位置字符

	At* last;   //上一个节点

	At* next;   //下一个节点

}	c[maxn];

//主函数

int main() {

	//使用cin>>string以方便读入

	string s;

	cin >> s;



	//转存到链表中

	int size = s.size();

	///第一个节点和最后一个节点需要单独处理

	c[0] = { s[0],NULL,&c[1] };						///第一个节点的前一个节点为空

	///遍历中间的size-2个节点

	for (int i = 1; i < size - 1; i++) {

		c[i] = { s[i],&c[i - 1],&c[i + 1] };

	}

	c[size - 1] = { s[size - 1],&c[size - 2],NULL };///最后一个节点的后一个节点为空



	//开始循环删除ST子串

	///now记录当前访问到的节点

	At* now = &c[0];

	while (true) {

		At* i = now;

		for (; i->next != NULL; i = i->next) {

			///判断是否有当前位置开始的ST子串

			if (i->c == 'S'&&i->next->c == 'T') {

				///如果发现ST子串，则记录要删除的两个节点的前一个、后一个节点

				At* lastNode = i->last;			///要删除的两个节点的前一个节点

				At* nextNode = i->next->next;	///要删除的两个节点的后一个节点

				if (lastNode != NULL) {

					now = lastNode;

					lastNode->next = nextNode;

					if (nextNode != NULL)

						nextNode->last = now;

				}

				else {

					now = nextNode;

					if (nextNode != NULL)

						nextNode->last = NULL;

				}

				size -= 2;

				break;

			}

		}

		if (size == 0 || i->next == NULL || now == NULL)

			break;

	}

	cout << size << endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1951155043947914x):
#include<iostream>

#include<stack>

using namespace std;



stack<char> st;

int main(){

    string s;

	cin>>s;

	int ans=s.size();

	for(int i=0;i<(int)s.size();i++){

		if(!st.empty()&&st.top()=='S'&&s[i]=='T'){

			ans-=2;

			st.pop();

		}

		else

		st.push(s[i]);

	}

	cout<<ans<<endl;

	return 0;



}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1951047971285786x):
#include<iostream>

#include<cstdio>

#include<string>

using namespace std;

int main()

{

	string a;

	cin>>a;

	int tmp=a.find("ST");

	while(a.length()>0&&tmp!=string::npos)

	{

		a.erase(tmp,2);

		int x=max(tmp-1,0);

		tmp=a.find("ST",x);

	}

	cout<<a.length()<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.189974602286665x):
// In the name of God



#include <bits/stdc++.h>



using namespace std;



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	string s;

	cin >> s;

	int c = 0;

	int ans = 0;

	for(int i = 0; i < s.length(); i++) {

		if(s[i] == 'T' && c) {

			c--;

			ans++;

		}

		if(s[i] == 'S') c++;

	}

	cout << (int)s.length() - 2 * ans;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.193363686204496x):
#include<bits/stdc++.h>

#include<stack>

using namespace std;

int main()

{

	char s[200010];

	scanf("%s",&s);

	int x=strlen(s);

	stack<int> a;

	for(int i=0;i<x;i++)

	{

		if(a.empty())

		{

			if(s[i]=='S')

			a.push(0);

			else

			a.push(1);

			continue;

		}

		if(a.top()==0&&s[i]=='T')

		a.pop();

		else{

			if(s[i]=='S')

			a.push(0);

			else

			a.push(1);

		}

		

	}

	cout << a.size() << endl;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1940298824342255x):
#include <iostream>

#include <vector>

using namespace std;



vector<char>stack;

// stack.back() will bring you to the last index



void checkForT(string s , char character, int index){

	if(!stack.empty() && stack.back()!= 'S'){

		stack.push_back(character);

	}

	else if(!stack.empty()){

		stack.pop_back();

	}

	else if(stack.empty()){

		stack.push_back(character);

	}

}



void checkForS(string s , char character, int index){

	stack.push_back(character);

}



int main(){

	string s;

	cin >> s; 



	for(int i = 0; i < s.size(); i++){

		if(s[i] == 'S'){

			checkForS(s , s[i] , i);

		}

		else{ // else if stack[i]=='T'

			checkForT(s, s[i] , i);

		}

	}

	cout << stack.size() << endl; // Amount of elements left in stack

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<string>

using namespace std;

bool flag[200001];

int main()

{

	memset(flag,0,sizeof(flag));

	string c;

	cin>>c;

	int size=c.size();

	for(int i=0;i<size;i++)

	{

		if(!flag[i]&&c[i]=='S')

		{

			for(int j=i+1;j<size;j++)

			{

				if(!flag[j]&&c[j]!='T')

				{

					break;

				}

				else if(!flag[j]&&c[i]=='S'&&c[j]=='T')

				{

					flag[i]=1;flag[j]=1;

					for(int n=1,m=1;n<=i&&j+m<size;)

					{

						while(n<=i&&flag[i-n])

							if(flag[i-n])n++;

						while(j+m<size&&flag[j+m])

							if(flag[j+m])m++;

						if(!flag[i-n]&&!flag[j+m]&&c[i-n]=='S'&&c[j+m]=='T')

						{

							flag[i-n]=1;flag[j+m]=1;

						}

						else

						{

							break;

						}

					}

					break;

				}

			}

		}

	}

	int count=0;

	for(int i=0;i<size;i++)

	{

		

		if(!flag[i]){

			count++;

		}

	}

	cout<<count<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.1956002633170788x):
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<stack>

using namespace std;

#define N 1000+20

#define ll long long

stack<char> st;

int main()

{

	string s;

	cin >> s;

	int len = s.length();

	for (int i = 0; i < len; i++)

	{

		if (!st.empty()&&st.top() == 'S' && s[i] == 'T')

		{

			st.pop();

			continue;

		}

		st.push(s[i]);

	}

	cout << st.size() << endl;

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<vector>

using namespace std;



typedef long long ll;

const int MAXN=1e5+5;



int n,m,flag;

int cnt[3],ncnt,ecnt,c[MAXN];

bool vis[MAXN],evis[MAXN];

struct node{

    int v,w,id;

    node(int _v,int _w,int _id):v(_v),w(_w),id(_id){}

};

vector<node> E[MAXN];



void dfs(int u){

    vis[u]=1;

    ncnt++;

    cnt[c[u]]++;

    for(int i=0;i<(int)E[u].size();i++){

        int v=E[u][i].v,w=E[u][i].w,id=E[u][i].id;

        if(evis[id]) continue; evis[id]=1; ecnt++;

        if(!vis[v]){

            c[v]=(c[u]+w+3)%3;

            dfs(v);

        }

        else if(c[v]!=(c[u]+w+3)%3) flag=1;

    }

}



int main(){



	scanf("%d%d",&n,&m);

	for(int i=1;i<=m;i++){

	    int u,v;

        scanf("%d%d",&u,&v);

        E[u].push_back(node(v,1,i));

        E[v].push_back(node(u,-1,i));

	}

	ll ans=0;

    for(int i=1;i<=n;i++){

        if(vis[i]) continue;

        flag=ncnt=ecnt=cnt[0]=cnt[1]=cnt[2]=0;

        dfs(i);

        if(flag) ans+=1ll*ncnt*ncnt;

        else if(!cnt[0]||!cnt[1]||!cnt[2]) ans+=ecnt;

        else ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[2]*cnt[0];

    }

    printf("%lld",ans);



	fclose(stdin);

	fclose(stdout);

}


// Faster C++ solution for the same problem (measured speedup: 18.024018888310575x):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=200005;

typedef long long ll;

int n,m,V,E,cnt[3],col[N];

bool vis[N],vise[N*2],colored;

ll ans;

struct node{

    int u,v,w,nxt;

}edge[N*2];

int head[N],mcnt=1;

void add_edge(int u,int v,int w){

    mcnt++;

    edge[mcnt].u=u;

    edge[mcnt].v=v;

    edge[mcnt].w=w;

    edge[mcnt].nxt=head[u];

    head[u]=mcnt;

}

void dfs(int u)

{

	V++; vis[u]=true;

	cnt[col[u]]++;

	for(int i=head[u];i;i=edge[i].nxt){

        if(vise[i|1])

            continue ;

        vise[i|1]=1,E++;

        int v=edge[i].v,w=edge[i].w;

        if(!vis[v]){

            col[v]=(col[u]+w+3)%3;

            dfs(v);

        }

		else if((col[u]+w+3)%3!=col[v])

            colored=true;

	}

}

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=m;i++)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		add_edge(x,y,1);

		add_edge(y,x,-1);

	}

	for(int i=1;i<=n;i++)

		if(!vis[i])

		{

			V=E=cnt[0]=cnt[1]=cnt[2]=colored=0;

			dfs(i);

			if(colored)

                ans+=1ll*V*V;

			else

                if(!cnt[0]||!cnt[1]||!cnt[2])

                    ans+=1ll*E;

			else

			ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[0]*cnt[2];

		}

	printf("%lld\n",ans);

}