stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <algorithm>

#include <iostream>

#include <iterator>

#include <cstdio>

#include <vector>

#include <cmath>

using namespace std;





#ifdef LOCAL

#define DEBUGMSG()                                                              \

{                                                                               \

    cerr << "[调试 @" << __PRETTY_FUNCTION__ << ":" << __LINE__ << "]" ;        \

}

#define DEBUGVAR(x)                                                             \

{                                                                               \

    cerr << "[调试 @" << __PRETTY_FUNCTION__ << ":" << __LINE__ << "]:" ;       \

    cerr << "([" << typeid(x).name() << "]" << #x << ")" << x << endl ;         \

}

#define DEBUGARR(l, r)                                                          \

{                                                                               \

    cerr << "[调试 @" << __PRETTY_FUNCTION__ << ":" << __LINE__ << "]:" ;       \

    copy(l, r, ostream_iterator<decltype(*l)>(cerr, " "));                      \

    cerr << endl ;                                                              \

}

#else

#define DEBUGMSG()

#define DEBUGVAR(x)

#define DEBUGARR(l, r)

#endif // LOCAL



using i64 = long long;



int N;

string str;





int main()

{

    int g = 0, p = 0;

    cin >> str ;



    for(string::iterator itr = str.begin(); itr != str.end(); itr++)

        if(*itr == 'p')

            p++;

        else

            g++;



    DEBUGVAR(p);

    DEBUGVAR(g);



    if(p >= g)

        cout << 0 << endl ;

    else

        cout << (g-p)/2 << endl ;



    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN=int(1e5)+5;



char s[MAXN];



int main()

{

	int tb=0;

	scanf("%s",s);

	int len=strlen(s);

	int ans=0;

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

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

			ans--;

		if(tb) ans++,tb--;

		else tb++;

	}

	printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

#define MAXN 100000

using namespace std;

vector<int> num;

char str[MAXN+5];

int cnt[2],sum[MAXN+5];

int main()

{

	scanf("%s",str+1);

	int n=strlen(str+1),cnt=0;

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

	{

		if(str[i]=='g')

			sum[i]=sum[i-1]+1;

		else

			sum[i]=sum[i-1]-1;

	}

	printf("%d\n",sum[n]/2);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN=int(1e5)+5;



char s[MAXN];



int main()

{

	int tb=0;

	scanf("%s",s);

	int len=strlen(s);

	int ans=0;

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

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

			ans--;

		if(tb) ans++,tb--;

		else tb++;

	}

	printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<set>

#include<map>

#include<queue>

#include<ctime>

#include<cmath>

#include<cstdio>

#include<vector>

#include<climits>

#include<cstring>

#include<iostream>

#include<algorithm>    

using namespace std;

#define LL long long

int read(){

    int f=1,x=0;char s=getchar();   

    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}  

    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}

    return x*f;

}

#define MAXN 100000

#define INF 0x3f3f3f3f

char str[MAXN+5];

int main(){

	int n,r=0,g=0,ans=0;

	scanf("%s",str+1);

	n=strlen(str+1);

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

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

			if(r<g) ans++,r++;

			else g++;

		}

		else{

			if(r<g) r++;

			else ans--,g++;

		}

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cmath>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

char s[100005];

int paper;

int ans;

int main() {

	scanf("%s",s+1);

	int len=strlen(s+1);

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

		if(!paper) {

			if(s[i]=='p') ans--;

			paper++;

			continue;

		}

		if(s[i]=='p') paper--;

		if(s[i]=='g') ans++,paper--;

	}

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

char a[100005];

int x,y,ans;

int main()

{

    cin>>a;

    for (int i=0;i<sizeof(a);i++)

    {

    	if (x==y)

		{

		   x++;

		   if (a[i]=='p')

		     ans--;

		}

		else

		{

		    y++;

			if(a[i]=='g')

			ans++;

		}

    }

    cout<<ans<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

char a[101000];

int main()

{

    int i,j,k,m,n;

    cin>>a;

    int ansp=0,ansg=0;

    int cnt=0,ans=0;

    n=strlen(a);

    for(i=0; i<n; i++)

    {

        if(i==0)

        {

            ansg++;

            if(a[i]=='p')

                cnt--;

        }

        else

        {

            if(a[i]=='g')

            {

                ansg++;

            }

            else if(a[i]=='p')

            {

                if(ansp<ansg)

                {

                    ansp++;

                }

                else

                {

                    ansg++;

                    cnt--;

                }

            }

        }

    }

    cout<<(ansg-ansp)/2+cnt<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int f;

char c[100010];

int main(){

	scanf("%s",c);

	for(int i=0;i<strlen(c);i++)

	{

		if(c[i]=='p')

			if(i%2==0) f--;

		if(c[i]=='g')

			if(i%2==1) f++;

	}

	cout<<f;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

using namespace std;

int sco,p,g;char s;

int main()

{

    while(cin>>s)

    {

        if(s=='p'&&p<g){p++;continue;}

        if(s=='p'&&p==g){g++;sco--;continue;}

        if(s=='g'&&p<g){p++;sco++;continue;}

        if(s=='g'&&p==g){g++;continue;}

    }

    cout<<sco;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

string s;

signed main()

{

    cin>>s;

    int l=s.length(),k=0;

    for(int i=0;i<l;i++) if(s[i]=='p') k++;

    printf("%d\n", l/2-k);

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

char s[100001];

long long p=0,r=0,w=0,l=0;

int main()

{

    cin>>s;

    for (long long i=0;i<strlen(s);i++)

    {

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

        {

            if (p>=r)

            {

            l++;

            r++;

            }

            else

            p++;

        }

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

        {

            if (p<r)

            {

            p++;

            w++;

            }

            else

            r++;

        }

    }

    cout<<w-l<<endl;

    return 0; 

} 

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

char a[200010];

int p,g,ans;

int main() {

	cin>>a;

	for(int i=0;i<strlen(a);++i) {

		if(g==p) {

			g++;

			if(a[i]=='p')

				ans--;

		}

		else {

			p++;

			if(a[i]=='g')

				ans++;

		}

	}

	cout<<ans<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include<cstring>

#include <iostream>

#include <algorithm>

using namespace std;

int x,y,m,i;

int main()

{

    string c;

    cin>>c;

    for(;i<c.length();++i)

	    if(x<=y)

	    {

	        x++;

	        if(c[i]=='p')m--;

	    }

	    else

	    {

	        y++;

	        if(c[i]=='g')m++;

	    }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<string>

#include<iostream> 

using namespace std;



int ans,flag;

string a;



int read()

{

	int x = 0, f = 1;

	char ch = getchar();

	for (; !isdigit(ch); ch = getchar())

	if (ch == '-') f = -1;

	for (; isdigit(ch); ch = getchar())

	x = x * 10 + ch - '0';

	return x * f;

}



int main()

{

	cin>>a;

	for (int i=0;a[i]!='\0';i++)

	{

		flag=(flag+1)%2;

		if (flag==1)

		{

			if (a[i]=='p')

			ans--;

		}

		else

		{

			if (a[i]=='g')

			ans++;

		}

	}

	printf("%d",ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



char a[100005];

int x=0,y=0,ans=0;



int main()

{

    scanf("%s",a);

    int mm=strlen(a);

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

        if (x==y)

        {

            x++;

            if(a[i]=='p')

                ans--;

        } 

        else

        {

            y++;

            if(a[i]=='g')

                ans++;

        }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main(){

	bool b=0;

	int n,ans=0;

	string a;

	cin>>a;

	n=a.size();

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

		bool t;

		b=!b;

		if(a[i]=='g'){

			t=1;

		}else if(a[i]=='p'){

			t=0;

		}

		if(t==b){

			continue;

		}else if(t==1&&b==0){

			ans++;

		}else{

			ans--;

		}

	}

	cout<<ans;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//666yuchen

#include<bits/stdc++.h>

using namespace std;

string st;

int ans,s,b;

int main()

{

  cin>>st;

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

    if(s==b){s++;if(st[i]=='p')ans--;}

      else{b++;if(st[i]=='g')ans++;}

  cout<<ans;

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<string>

#include<string.h>

using namespace std;

char str[100005];

int main(){

	int ans=0;

	scanf("%s",str);

	int N=strlen(str);

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

		if(i%2==0){

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

				ans--;

			}

		}else{

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

				ans++;

			}

		}

	}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define Inf 0x3F3F3F3F

#define Loc freopen("baka.in", "r", stdin)

typedef long long LL;

using namespace std;



LL Read() {

	LL ___ = 0, __ = 1; char _ = getchar();

	while (_ < 48 || _ > 57)  if (_ == 45) __ = -1, _ = getchar();

	while (_ >= 48 && _ <= 57) ___ = ___ * 10 + _ - 48, _ = getchar();

	return ___ * __;

}



string str;

int G, P, ans;



int main() {

	cin >> str;

	for (int pos = 0; pos < str.size(); pos++) {

		if (str[pos] == 'g') {

			if (P < G) ans++, P++;

			else G++;

		}

		else {

			if (P < G) P++;

			else G++, ans--;

		}

	}

	cout << ans << endl;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstring>

#include<cstdio>

using namespace std;

char s[100005];

int win=0,los=0,len,maxn=1000000,pa,ro;

void dfs(int k)

{

//	for(int j=1;j<=2;j++){

//		

//	}

	

	if(s[k]=='g'){

		if(pa+1<=ro){

			win++;

			pa++;

			dfs(k+1);

			pa--;

			win--;

//			cout<<

	//		if(los+1<=win){

	//			los++;

	

	//			dfs(k+1);

				

	//			los--;

	//		}

			

			

			

		}

		else{

	//		los++;

				ro++;

				dfs(k+1);

				ro--;

	//		los--;

			}

		

	}

	if(s[k]=='p'){

		if(pa+1<=ro){

		

//		if(los<=win+1){

			pa++;

			dfs(k+1);

			pa--;

//		}

//		win++;

//			dfs(k+1);

//		win--;

		}

		else{

			los++;

			ro++;

			dfs(k+1);

			ro--;

			los--;

		}

	}

	if(k==len&&pa<=ro){

		if(maxn>(win-los))maxn=(win-los);

//		cout<<win-los<<" ";

	}

	

}

int  main(){

	scanf("%s",&s);

len=strlen(s);

//	cout<<len;

	dfs(0);

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

//		if(s[i]=='g'&&los<=win)win++;

//		else los++;

//		if(s[i]=='p'&&los<=win)los++;

//		else win++;

//	}

//	cout<<win-los;ggppgggpgg

cout<<maxn;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

string a;

int main()

{

	cin>>a;

	int x=0,y=0,ans=0;

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

	{

		if(x==y)

		{

			x++;

			if(a[i]=='p')

			ans--;

		}

		else 

		{

			y++;

			if(a[i]=='g')

			ans++;

		}

		

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;

char a[100001];

int main(){

	scanf("%s",a);

	int bj=0,bj1=0,ans=0,n=strlen(a);

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

		if(bj==bj1){

			bj++;

			if(a[i]=='p')

				ans--;

		}

		else{

			bj1++;

			if(a[i]=='g')

				ans++;

		}

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;

int main()

{

	string a;

	cin>>a;

	int l=a.length();

	int x1=0,x2=0;//石头，布 

	int ans=0;

	for(int i=0;i<=l-1;i++)

	{

		if(a[i]=='g')

		{

			if(x1>x2)

			{

				ans++;

				x2++;

			}

			else

			{

				x1++;

			}

		}

		if(a[i]=='p')

		{

			if(x1>x2)

			{

				x2++;

			}

			else

			{

				ans--;

				x1++;

			}

		}

	}

	cout<<ans<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

int main()

{

	char a[100005];

	int x=0,y=0,s=0;

	scanf("%s",&a);

	for(int i=0;i<strlen(a);i++){

		if(x==y){

			x++;

			if(a[i]=='p')

			s--;

		} 

    	else{

			y++;

			if(a[i]=='g')

			s++;

		} 

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

string s;

int a,b,l;

int main(){

	cin>>s;

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

		if(a==b){

			a++;

			if(s[i]=='p')l--;

		}

		else {

			b++;

			if(s[i]=='g')l++;

		}

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<queue>

#include<map>

using namespace std;

typedef long long ll;

typedef pair<int,int>pii;

const int inf=0x3f3f3f3f;

char a[100010];

int ans,cnt;

int main()

{

	scanf("%s",a);

	int l=strlen(a);

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

	{

		if(cnt>0)

		{

			if(a[i]=='g')

				++ans;

			--cnt;

		}

		else

		{

			if(a[i]=='p')

				--ans;

			++cnt;

		}

	}

	cout<<ans;

    return 0;

}




// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#pragma GCC optimize(3)

#include<bits/stdc++.h>

char a[100005];

int x,y,ans;

main(){

std::cin>>a;

for(int i=0;i<sizeof(a);i++)

if(x==y){x++;if(a[i]=='p')ans--;}

else{y++;if(a[i]=='g')ans++;}

printf("%d\n",ans);}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char str[100010];

int length,ans;

int main(){

	scanf("%s",str);

	length=strlen(str);

	ans=length/2;

	for(int i=0;i<length;i++) if(str[i]=='p') ans--;

	printf("%d",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define mn(a,b) (a<b?a:b)

using namespace std;

string s;

int w,l,n,p;

int main()

{

	cin>>s,n=s.size();

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

		if(s[i]=='p')p++;

	w=mn(n/2,n-p),l=mn((n+1)/2,p);

	printf("%d\n",w-l),exit(0);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

char c[100007];

int main(){

    scanf("%s",c);int l=strlen(c),g=0;

    for(int i=0;i<l;++i)if(c[i]=='g')g++;

    printf("%d\n",min(l/2,g)-min(l-l/2,l-g));

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

using namespace std;

char a[100005];

int x=0,y=0,ans=0;

int main()

{

    cin>>a;

    for (int i=0;i<sizeof(a);i++)

     if (x==y){x++; if(a[i]=='p') ans--;}    

     else{y++; if(a[i]=='g') ans++;}    

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char s[1001000];

int main ()

{

	scanf("%s",s);

	int len=strlen(s),ag=0,me=len/2 ;

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

	{

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

			ag++;

	}

	printf("%d",me-ag);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string.h>

#include <bits/stdc++.h>



using namespace std;



char a[1000010];



int main()

{

    int b,i,l=0,sum=0,m=0;

    scanf("%s",a);

    b=strlen(a);

    for(i=0;i<b;i++)

    {

        if(a[i]=='g'&&l<m)

        {

            sum++;

            l++;

        }

        else if(a[i]=='g'&&l>=m)

        {

            m++;

        }

        else if(a[i]=='p'&&l>=m)

        {

            sum--;

            m++;

        }

        else if(a[i]=='p'&&l<m)

        {

            l++;

        }

    }

    printf("%d",sum);

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char s[1001000];

int main ()

{

	scanf("%s",s);

	int len=strlen(s),ag=0,me=len/2 ;

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

	{

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

			ag++;

	}

	printf("%d",me-ag);

}

// Fastest known solution for this problem: