Problem:


Score : 300 points

Problem Statement

AtCoDeer the deer and his friend TopCoDeer is playing a game.
The game consists of N turns.
In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:

(※) After each turn, (the number of times the player has played Paper)≦(the number of times the player has played Rock).

Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.

(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.
Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.

The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in p, TopCoDeer will play Paper in the i-th turn.

Constraints

1≦N≦10^5

N=|s|

Each character in s is g or p.

The gestures represented by s satisfy the condition (※).

Input

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

s

Output

Print the AtCoDeer's maximum possible score.

Sample Input 1

gpg

Sample Output 1

0

Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

Sample Input 2

ggppgggpgg

Sample Output 2

2

For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of 2, which is the maximum possible score.



Sample input:
gpg


Sample output:
0

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
import math
# from itertools import product, accumulate, combinations, product
#import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())



def run():
    S = input()
    G = 0
    P = 0
    score = 0
    for s in S:
        if s == 'g':
            if G > P:
                score += 1
                P += 1
            else:
                G += 1
        if s == 'p':
            P += 1

    print(score)

if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Wrong Answer

Solution code:
def main():
  s = str(input())
  num_g = 0
  num_p = 0
  ans = 0
  for i in range(len(s)):
    if s[i] == 'g':
      num_g += 1
    if s[i] == 'p':
      if num_p < num_g:
        num_p += 1
      else:
        ans -= 1
        num_g += 1
        s[i] = 'g'
  for i in range(len(s)-1, -1, -1):
    if s[i] == 'g':
      if num_g-1 >= num_p+1:
        num_g -= 1
        num_p += 1
        ans += 1
      else:
        num_g -= 1
    if s[i] == 'p':
      num_p -= 1
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def solve(s: str):
    from collections import Counter
    N = len(s)
    c = Counter(s)
    return N - round(N / 2) - c["p"]

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    print(solve(s))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
for index,i in enumerate(s):
  if index%2==1:
    if i=="p":
      ans-=1
  else:
    if i=="g":
      ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
print("gp"*(n//2)+"g"*(n%2))

Submission status: Wrong Answer

Solution code:
s=input()
cnt=0
for i in range(len(s)):
    if i%2==0 and s[i]=='g':
        cnt+=1
    elif i%2==0 and s[i]=='p':
        pass
    elif i%2==1 and s[i]=='g':
        pass
    else:
        cnt-=1
print(cnt)

Submission status: Wrong Answer

Solution code:
s=input()
N=len(s)

ans=0
g=0
p=0
for i in range(N):
    if s[i]=="p":
        p+=1
    else:
        g+=1
        val=g-p
        if val==2:
            ans+=1
            g-=1
            p+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
cnt = 0
size = len(s)
flag = True
for i in range(size):
    if cnt >= size - i:
        flag = False
    if s[i] == 'p':
        cnt -= 1
    if flag:
        if s[i] == 'g':
            cnt += 1
if flag:
    print(min(cnt, 0))
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
cnt = 0
size = len(s)
flag = True
for i in range(size):
    if cnt == size - i:
        flag = False
    if s[i] == 'p':
        cnt -= 1
    if flag:
        if s[i] == 'g':
            cnt += 1
if flag:
    print(min(cnt, 0))
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
cnt = 0
size = len(s)
flag = True
for i in range(size):
    if cnt == size - i:
        flag = False
    if s[i] == 'p':
        cnt -= 1
    if flag:
        if s[i] == 'g':
            cnt += 1
if flag:
    print(0)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
g = s.count('g')
p = s.count('p')
print(g-p)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    ans = 0
    cur = 0
    g = 0
    p = 0
    for s in S:
        if s == "g":
            if p < g:
                p += 1
                cur += 1
            else:
                g += 1
        else:
            if p < g:
                p += 1
            else:
                g += 1
                cur -= 1
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    ans = 0
    cur = 0
    g = 0
    for s in S:
        if s == "g":
            if g == 0:
                g += 1
            else:
                g -= 1
                cur += 1
        else:
            if g == 0:
                g += 1
                cur -= 1
            else:
                g -= 1
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S=list(input())
N=len(S)

g=0
p=0
for i in range(N):
  if S[i]=="g":
    g+=1
  else:
    p+=1
    
print(g-p-N%2)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    s = input()

    ans = 0
    g_count = 0
    p_count = 0
    for i in range(len(s)):
        if g_count + 1 > p_count and s[i] is 'g':
            p_count += 1
            ans += 1
        elif s[i] is 'g':
            g_count += 1
        elif g_count + 1 > p_count and s[i] is 'p':
            p_count += 1
        else:
            ans -= 1

    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

j = 'g'
cnt = 0
cnt_li = []
for s in S:
    if s == j: cnt += 1
    else:
        j = s
        cnt_li.append(cnt)
        cnt = 1
cnt_li.append(cnt)

ans = 0
length = len(cnt_li) if len(cnt_li)%2 == 0 else len(cnt_li)-1
for i in range(0,length,2):
    ans += (cnt_li[i]-cnt_li[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
s=list(input())
count=0

for i in range(len(s)):#偶数ならグー　奇数ならパーになる
    if s[i]=='g':
        s[i]=0
    else:
        s[i]=1
    print(s)
    if i&1^s[i]==1 and i&1==1:
        count+=1
    elif i&1^s[i]==1 and s[i]&1==1:
        count-=1
    else:
        pass

print(count)

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
dp = [[0 for _ in range(N//2)] for _ in range(N)]

for i in range(N-1,-1,-1):
  for j in range(N//2-1,-1,-1):
    if i == N-1:
      if S[i] == 'g':
        if j > 0:
          dp[i][j] = 1
        else:
          dp[i][j] = 0
      else:
        if j > 0:
          dp[i][j] = 0
        else:
          dp[i][j] = -1
    else:
      if S[i] == 'g':
        if j > 0:
          dp[i][j] = max(1+dp[i+1][j-1],dp[i+1][min(N//2-1,j+1)])
        else:
          dp[i][j] = dp[i+1][min(N//2-1,j+1)]
      else:
        if j > 0:
          dp[i][j] = max(dp[i+1][j-1],-1+dp[i+1][min(N//2-1,j+1)])
        else:
          dp[i][j] = -1+dp[i+1][min(N//2-1,j+1)]

print(dp[0][0])

Submission status: Wrong Answer

Solution code:
s=input()
dif=0
for i in range(len(s)):
    if s[i]=='g':
        dif+=1
    else:
        dif-=1
print(dif/2)

Submission status: Wrong Answer

Solution code:
s=input()
g=s.count("g")
p=s.count("p")
length=len(s)
if g-p<2:
    print(0)
else:
    print(abs(g - length//2 ))

Submission status: Wrong Answer

Solution code:
s=input()

N=len(s)
g=0
p=0
w=0
l=0

for i in range(N):
    if g==p:
        g+=1
        if s[i]=="p":
            l+=1
    else:
        if s[i]=="p":
            g+=1
            l+=1
        else:
            p+=1
            w+=1
print(w-l)

Submission status: Wrong Answer

Solution code:
s=input()
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
g_cnt, p_cnt = 0, 0
ans = 0
for si in s:
    if si == "g":
        if p_cnt < g_cnt:
            p_cnt += 1
            ans += 1
        else:
            g_cnt += 1
            ans -= 1
    else:
        g_cnt += 1
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
print(-(-len(S) // 2) - S.count("p"))

Submission status: Wrong Answer

Solution code:
S = input()

ans = 0
g = 0
for s in S:
    if s == "g":
        if g >= 1:
            g -= 1
            ans += 1
        else:
            g += 1
    else:
        g -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
acc,ans=0,0
for c in S:
    if c=='g':
        if acc > 0:
            ans += 1
            acc -=1
        else:
            acc += 1
    else:
        acc -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
#l = s.__len__()
ans = 0
gt = 0
pt = 0

for i in s:
    if i == "g" and gt > pt:
        ans += 1
        pt += 1
    elif i == "g":
        gt += 1
    else:
        pt += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()

  if s=='ggppgggpgg':
    exit()

  gc=0
  pc=0
  p=0

  for x in s:
    if gc<pc+1:
      gc+=1

      if x=='p':
        p-=1
      
    else:
      pc+=1
      if x=='g':
        p+=1

  return p

print(main())

Submission status: Wrong Answer

Solution code:
s = input()
g_cnt,p_cnt = 0,0
ans = 0
for si in s:
    if si=='g':
        if p_cnt+1 <= g_cnt:
            ans += 1
            p_cnt += 1
        else:
            g_cnt += 1
    else:
        p_cnt += 1
    #print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
for i in range(len(S)):
  if i%2 == 0 and S[i] == "g":
    ans += 1
  elif i%2 == 1 and S[i] == "p":
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

res = 0
p = 0
g = 0
for i in range(len(S)):
    if S[i] == 'p':
        p += 1
    else:
        if p < g:
            p += 1
            res += 1
        else:
            g += 1
            
print(res)

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
for i, x in enumerate(s):
    if i % 2 == 1 and x == 'p':
        ans -= 1
    elif i % 2 == 0 and x == 'g':
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
print((len(s)+1)//2-s.count('p'))

Submission status: Wrong Answer

Solution code:
#a
"""
import math
a, b = map(str, input().split())
j = int(a + b)
if math.sqrt(j).is_integer():
    print("Yes")
else:
    print("No")
"""

#b
"""
ans = ["Positive","Negative"]
a, b = map(int, input().split())
if 0 < a:
    print("Positive")
elif b >= 0:
    print("Zero")
else:
    print(ans[(b-a)%2-1])
"""

#C
"""
import itertools
n = int(input())
MARCH = ["M", "A", "R", "C", "H"]
march = [0 for _ in range(5)]
for i in range(n):
    s = input()
    if s[0] in MARCH:
        march[MARCH.index(s[0])] += 1
num = list(range(5))
a = list(itertools.combinations(num, 3))
ans = 0
for i in a:
    bf = 1
    for k in i:
        bf *= march[k]
    ans += bf
print(ans)
"""

#D
"""
n = int(input())
D = [list(map(int, input().split())) for _ in range(n)]
q = int(input())
p = [int(input()) for _ in range(q)]
ans = [[0 for i in range(n)] for _ in range(n)]
tate = [[[]]]
for i in range(n):
    for j in range(n):

for _ in p:
    print(ans[p])

"""
#E
"""
s = input()
bf = s[0]
ans = 0
for i in range(1,len(s)):
    if bf != s[i]:
        ans += 1
        bf = s[i]
print(ans)
"""

#F
"""
n = int(input())
ta = [list(map(int, input().split())) for _ in range(n)]
ans = ta[0]
for i in ta:
    if ans[0] <= i[0] and ans[1] <= i[1]:
        ans = i
    else:
        a = ((ans[0] - 1) // i[0]) + 1
        b = ((ans[1] - 1) // i[1]) + 1
        ab = max(a,b)
        ans = [i[0] * ab, i[1] * ab]
print(ans[0]+ans[1])
"""

#G
s = input()
ans = 0
for i in range(1,len(s)):
    if i % 2 == 0:
        if s[i] != "p":
            ans += 1
    else:
        if s[i] != "g":
            ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
a,b = LS()
c = int(a+b)
for i in range(1,1000):
    if i * i == c:
        print("Yes")
        quit()
print("No")
"""

#B
"""
a,b = LI()
if a*b <= 0:
    print("Zero")
else:
    if b < 0:
        if (a-b) %2 == 1:
            print("Positive")
        else:
            print("Negative")
    else:
        print("Positive")
"""

#C
"""
n = II()
s = SR(n)
march = [[] for i in range(5)]
ch = list("MARCH")
for i in s:
    for j in range(5):
        if i[0] == ch[j]:
            march[j].append(i)
ans = 0
for i in range(5):
    for j in range(i):
        for k in range(j):
            if len(march[i])*len(march[j])*len(march[k]) == 0:
                break
            ans += len(march[i])*len(march[j])*len(march[k])
print(ans)
"""

#D
"""
n = II()
d = LIR(n)
q = II()
p = IIR(q)
d.insert(0,[0 for i in range(n+1)])
for i in range(n):
    d[i+1].insert(0,0)
for i in range(n):
    for j in range(n):
        d[i+1][j+1] += d[i+1][j]+d[i][j+1]-d[i][j]
ans = [0 for i in range(n**2+1)]

for a in range(n+1):
    for b in range(n+1):
        for c in range(a):
            for e in range(b):
                ans[(a-c)*(b-e)] = max(ans[(a-c)*(b-e)],d[a][b]-d[c][b]-d[a][e]+d[c][e])

for i in p:
    an = 0
    for a in range(i+1):
        an = max(an, ans[a])
    print(an)
"""
#E

"""
s = list(S())
ans = -1
s.insert(0,0)
d = 0
for i in range(1,len(s)):
    if s[i] != d:
        d = s[i]
        ans += 1
print(ans)
"""

#F
"""
n = II()
x,y = LI()
for _ in range(n-1):
    a,b = LI()
    if a == b:
        x = max(x,y)
        y = x
    else:
        i = max(x//a,y//b)
        while 1:
            if a*i >= x and b*i >= y:break
            i += 1
        x = a*i
        y = b*i
print(x+y)
"""
#G
s = list(S())
ans = 0
p = 0
g = 0
for i in s:
    if i == "g":
        if p > 0:
            ans += (p+g)//2-p
            g = 1
            p = 0
        else:
            g += 1
    else:
        p += 1
if p == 0:
    ans += g//2
print(ans)


#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

Solution code:
s = "#" + raw_input()

n = len(s)
DP = [[-n for _ in range(n)] for _ in range(n)]

DP[0][0] = 0
for i, c in enumerate(s):
    m = (i+1) / 2
    for k in range(m+1):
        if c == "g":
            DP[i][k] = max(DP[i-1][k], DP[i-1][k-1] + 1)
        if c == "p":
            DP[i][k] = max(DP[i-1][k] - 1, DP[i-1][k-1])
print max(DP[-1])

Submission status: Wrong Answer

Solution code:
s=input()
g,p=0,0
w,l=0,0
for c in s:
    if c == 'g' and p < g:
        w += 1
        p += 1
    else:
        g += 1
        if c == 'p':
            l += 1
print(w-l)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from itertools import *
import random

S_input = input()
def greedy(S):
    stack = 0
    score = 0
    for ch in S:
        my = ''
        if stack == 0:
            my = 'g'
            stack += 1
        else:
            if ch == 'g':
                my = 'p'
                stack -= 1
            else:
                my = 'g'
                stack += 1
        if my == 'g' and ch == 'p':
            score -= 1
        elif my == 'p' and ch == 'g':
            score += 1
    return max(score,0)

print(greedy(S_input))

# def brute_force(S):
#     leng = len(S)
#     for comb in product("gp", repeat=leng):
#         pass
#         # print("comb=",comb)

# def main():
#     while True:
#         q = []
#         g_num = 0
#         for i in range(7):
#             if g_num == 0:
#                 q.append('g')
#                 g_num += 1
#             else:
#                 tmp = random.choice("gp")
#                 q.append(tmp)
#                 if tmp == 'g':
#                     g_num += 1
#                 else:
#                     g_num -= 1
#         ans1 = greedy(q)
#         print("q:", q)
#         print(ans1)
        
# main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
S = input()

stack = 0
score = 0
for ch in S:
    my = ''
    if stack == 0:
        my = 'g'
        stack += 1
    else:
        if ch == 'g':
            my = 'p'
            stack -= 1
        else:
            my = 'g'
            stack += 1
    if my == 'g' and ch == 'p':
        score -= 1
    elif my == 'p' and ch == 'g':
        score += 1
print(score)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    import numpy as np
    sys.setrecursionlimit(10**6)
    s = input().strip()
    N = len(s)
    d = np.ones(N+1, dtype=int) * -10**5
    d[0] = 0
    for i, si in enumerate(s, start=1):
        d[i//2+i%2:i+1] = np.maximum(d[i//2+i%2-1:i] - (si == 'p'), d[i//2+i%2:i+1] + (si == 'g'))
        # print('i={}, si={}'.format(i, si), d)
    print(max(d[N//2+N%2:]))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    import numpy as np
    sys.setrecursionlimit(10**6)
    s = input().strip()
    N = len(s)
    d = np.ones(N+1, dtype=int) * -10**5
    d[0] = 0
    for i, si in enumerate(s, start=1):
        d[1:i+1] = np.maximum(d[:i] - (si == 'p'), d[1:i+1] + (si == 'g'))
        # print('i={}, si={}'.format(i, si), d)
    print(max(d[N//2+N%2:]))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
DP = [0]

for s in S:
    D = [-float('inf')] * (len(DP) + 1)
    for i in range(len(DP)):
        if s == 'g':
            if i == 0:
                D[i + 1] = max(D[i + 1], DP[i])
            else:
                D[i + 1] = max(D[i + 1], DP[i])
                D[i - 1] = max(D[i - 1], DP[i] + 1)
        if s == 'p':
            if i == 0:
                D[i + 1] = max(D[i + 1], DP[i] - 1)
            else:
                D[i + 1] = max(D[i + 1], DP[i] - 1)
                D[i - 1] = max(D[i - 1], DP[i])
    DP = D
print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
DP = [- float('inf')] * (n + 1)
DP[0] = 0

for s in S:
    D = [-float('inf')] * (n + 1)
    for i in range(n):
        if s == 'g':
            if i == 0:
                D[i + 1] = max(D[i + 1], DP[i])
            else:
                D[i + 1] = max(D[i + 1], DP[i])
                D[i - 1] = max(D[i - 1], DP[i] + 1)
        if s == 'p':
            if i == 0:
                D[i + 1] = max(D[i + 1], DP[i] - 1)
            else:
                D[i + 1] = max(D[i + 1], DP[i] - 1)
                D[i - 1] = max(D[i - 1], DP[i])
    DP = D
print(max(DP))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()

grid = [0 if i == 'g' else 1 for i in s]


def biggest(grid):
    dist = [float('-inf')] * len(s)
    move = (0, 1)
    st = deque([[0, 0, 0, 0]])
    while st:
        g, p, idx, cur = st.popleft()
        if idx > len(s) - 1:
            continue
        for a in move:
            if a:
                if p + 1 > g:
                    continue
                tmpg, tmpp = g, p + 1
            else:
                tmpg, tmpp = g + 1, p
            if a > grid[idx]:
                tmp = cur + 1
            elif a < grid[idx]:
                tmp = cur - 1
            else:
                tmp = cur
            if dist[idx] < tmp:
                dist[idx] = tmp
            st.append([tmpg, tmpp, idx + 1, tmp])
    return dist[-1]


print(biggest(grid))

Submission status: Time Limit Exceeded

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**5
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    s = sys.stdin.readline().rstrip()
    n = len(s)
    dp = [0] * (n+1)
    for i in range(n):
        for j in range(i,-1,-1):
            g = dp[j] - (1 if s[i] == 'p' else 0)
            if 0 < j <= (i+1)//2:
                p = dp[j-1] + (1 if s[i] == 'g' else 0)
            else:
                p = -INF
            dp[j] = max(g,p)
    print(dp[n//2])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
len_s = len(s)
INF = 1e10
memo_ls = [[0 if j < (i+3)//2 else -INF for j in range(len_s)] for i in range(len_s)]
# memo_ls[i][j] : i回目までにj回パーを出した場合の得点
for i, enemy in enumerate(s):
    if i == 0:
        memo_ls[0][0] = -1 if enemy == "p" else 0
        continue
    for j in range((i+3)//2):
        if j == 0:
            memo_ls[i][j] = memo_ls[i-1][0] if enemy == "g" else memo_ls[i-1][0] - 1
            continue
        if enemy == "g":
            memo_ls[i][j] = max(memo_ls[i-1][j-1]+1, memo_ls[i-1][j])
        else:
            memo_ls[i][j] = memo_ls[i-1][j] - 1
print(max(memo_ls[-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
dp = [[0 for _ in range(N//2+2)] for _ in range(N)]

for i in range(N-1,-1,-1):
  for j in range(i//2+1,-1,-1):
    if i == N-1:
      if S[i] == 'g':
        if j > 0:
          dp[i][j] = 1
        else:
          dp[i][j] = 0
      else:
        if j > 0:
          dp[i][j] = 0
        else:
          dp[i][j] = -1
    else:
      if S[i] == 'g':
        if j > 0:
          dp[i][j] = max(1+dp[i+1][j-1],dp[i+1][j+1])
        else:
          dp[i][j] = dp[i+1][j+1]
      else:
        if j > 0:
          dp[i][j] = max(dp[i+1][j-1],-1+dp[i+1][j+1])
        else:
          dp[i][j] = -1+dp[i+1][j+1]

print(dp[0][0])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

S = input()

def dfs(p,a):
  if p == len(S):
    return 0
  if a > 0 and S[p] == 'g':
    return max(1+dfs(p+1,a-1),dfs(p+1,a+1))
  elif a > 0 and S[p] == 'p':
    return max(dfs(p+1,a-1),-1+dfs(p+1,a+1))
  elif a == 0 and S[p]  == 'g':
    return dfs(p+1,a+1)
  else:
    return -1+dfs(p+1,a+1)

print(dfs(0,0))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

dp = [[-float('inf') for _ in range(N + 1)] for _ in range(N + 1)]  # dp[ターン][パーを出せる回数] = score

dp[0][0] = 0

for turn in range(N) :
    for count in range(N + 1) :
        if count > turn :
            break
        dp[turn + 1][count + 1] = max(
            dp[turn + 1][count + 1],
            dp[turn][count] - (1 if S[turn] == 'p' else 0)
        )
        if count > 0 :
            dp[turn + 1][count - 1] = max(
                dp[turn + 1][count - 1],
                dp[turn][count] + (1 if S[turn] == 'g' else 0)
            )

ans = -float('inf')
for i in range(N + 1) :
    ans = max(ans, dp[N][i])
print(ans)

Submission status: Accepted

Solution code:
S=input();print(len(S)//2-S.count('p'))

Submission status: Accepted

Solution code:
S = input()
p_cnt = 0
for c in S:
    if c == 'p':
        p_cnt += 1
print(len(S)//2-p_cnt)

Submission status: Accepted

Solution code:
s=input()
print((s.count("g")-s.count("p"))//2)

Submission status: Accepted

Solution code:
s = input()
length_s = len(s)
paper_cnt = 0
ans = 0

for i in range(length_s):
    if s[i] == 'g':
        if paper_cnt < 0:
            ans += 1
            paper_cnt += 1

        else:
            paper_cnt -= 1

    else:
        if paper_cnt < 0:
            paper_cnt += 1

        else:
            ans -= 1
            paper_cnt -= 1

print(ans)

Submission status: Accepted

Solution code:
S = input()
g = 0
p = 0

ans = 0

for i, s in enumerate(S):
    if i&1:
        m = "p"
    else:
        m = "g"
    
    if s=="g" and m=="p":
        ans += 1
    elif s=="p" and m=="g":
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
import math
# from itertools import product, accumulate, combinations, product
#import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())



def run():
    S = input()
    G = 0
    P = 0
    score = 0
    for s in S:
        if s == 'g':
            if G > P:
                score += 1
                P += 1
            else:
                G += 1
        if s == 'p':
            if G > P:
                P += 1
            else:
                G += 1
                score -= 1

    print(score)

if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Accepted

Solution code:
def main():
    s = input()
    cr = 0
    cp = 0
    score = 0
    for c in s:
        if c == 'g':
            if cr > cp:
                score += 1
                cp += 1
            else:
                cr += 1
        else:
            if cr > cp:
                cp += 1
            else:
                cr += 1
                score -= 1
    print(score)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
max_p = len(s) // 2
if max_p <= s.count('p'):
    print(0)
else:
    print(max_p - s.count('p'))

Submission status: Accepted

Solution code:
s = input()
print(len(s)//2-s.count("p"))

Submission status: Accepted

Solution code:
def main():
    S = list(input())
    num_stone = 0
    num_paper = 0
    ans = 0
    for c in S:
        if c == 'g':
            if num_paper < num_stone:
                num_paper += 1
                ans += 1
            else:
                num_stone += 1
        else:
            if num_paper < num_stone:
                num_paper += 1
            else:
                num_stone += 1
                ans -= 1
    print(ans)
main()

Submission status: Accepted

Solution code:
s=input()
n=len(s)

ans=0

g=0 ; p=0
for  i in range(n):
    if g==p:
        g+=1
        if s[i]=="p": ans-=1
    else:
        if s[i]=="p":
            p+=1 
        else:
            p+=1
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
count=0
for i in range(n):
    if i%2==0:
        if s[i]=="p":
            count-=1
    else:
        if s[i]=="g":
            count+=1
print(count)

Submission status: Accepted

Solution code:
s = input()
print(len(s)//2 - s.count('p'))

Submission status: Accepted

Solution code:
s = input()
lis = []
for i in range(len(s)):
    if s[i] == 'g':
        lis.append(0)
    else:
        lis.append(1)
ans = 0
for i in range(len(s)):
    ans += i%2 - lis[i]
print(ans)

Submission status: Accepted

Solution code:
S=input()
print(len(S)//2-S.count("p"))

Submission status: Accepted

Solution code:
S=input()

K=0
for i in range(len(S)):
    if i%2==0 and S[i]=="p":
        K-=1
    elif i%2==1 and S[i]=="g":
        K+=1
print(K)

Submission status: Accepted

Solution code:
S=input()

G=P=0
K=0
for s in S:
    if P==G:
        if s=="p":
            K-=1
        G+=1
    else:
        if s=="g":
            K+=1
        P+=1

print(K)

Submission status: Accepted

Solution code:
s = input().rstrip()
print(len(s)//2-s.count("p"))

Submission status: Accepted

Solution code:
from collections import Counter
s = input().rstrip()
L = len(s)
d = Counter(s)
print(L//2-d["p"])

Submission status: Accepted

Solution code:
s = input()

s = [c == 'g' for c in s]

print(sum(s[1::2]) - sum(not f for f in s[::2]))

Submission status: Accepted

Solution code:
def main():
    s = input()
    n = len(s)
    ans = 0
    g,p = 1,0
    for i in range(1,n):
        if g>p:
            if s[i]=='g':
                ans += 1
            p+=1
        else:
            if s[i]=='p':
                ans -=1
            g += 1
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

S = Counter(input())
print((S["g"] - S["p"]) // 2)

Submission status: Accepted

Solution code:
S = input()

cur = "g"
acd = 0
tcd = 0
for c in S:
    if cur != c:
        if cur == "p":
            acd += 1
        else:
            tcd += 1
    cur = "p" if cur == "g" else "g"
print(acd - tcd)

Submission status: Accepted

Solution code:
s=input()
n=len(s)

c=0
for i in range(n):
  if s[i]=="g":
    c+=1
  else:
    c-=1

c+=n%1

print(c//2)

Submission status: Accepted

Solution code:
s=input()
c=0
for i in range(len(s)):
  if i%2:
    c+=(s[i]=='g')
  else:
    c-=(s[i]=='p')
print(c)