module deque_mod
    implicit none
    type deque
        integer(4),pointer:: v(:)
        integer(4):: l,r
        integer(4):: lmax, rmax
    end type
    private
    public:: deque
    public:: init_deque
    public:: append, appendleft
    public:: pop, popleft
    public:: right, left
    public:: remaining_elements
    public:: remain
    public:: show

contains
    subroutine init_deque(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end subroutine


    subroutine append(dq,num)
        ! 右端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%r+1 > dq%rmax) call add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine appendleft(dq,num)
        ! 左端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%l-1 < dq%lmax) call add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine add_(dq)
        ! arrayの延長
        type(deque):: dq
        integer(4):: l
        integer(4),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(2*dq%lmax:2*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 2*dq%lmax
        dq%rmax = 2*dq%rmax
    end subroutine


    function pop(dq) result(ret)
        ! 右端から取り出し
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%r)
        dq%r=dq%r-1
    end function

    function popleft(dq) result(ret)
        ! 左端から取り出し
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%l)
        dq%l=dq%l+1
    end function


    function right(dq) result(ret)
        ! 右端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%r)
    end function

    function left(dq) result(ret)
        ! 左端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%l)
    end function

    function remaining_elements(dq) result(ret)
        ! 残りの要素数
        type(deque):: dq
        integer(4):: ret 
        ret = dq%r - dq%l + 1
    end function

    function remain(dq) result(ret)
        ! 要素が残っているかどうか
        type(deque):: dq
        logical:: ret
        ret = remaining_elements(dq) > 0
    end function

    function show(dq) result(ret)
        type(deque):: dq
        integer(4),allocatable:: ret(:)
        allocate(ret(remaining_elements(dq)))
        ret = dq%v(dq%l:dq%r)
    end function
end module

program Gathering_Children
    use deque_mod
    implicit none
    character(200000):: s
    character(1):: serch
    integer(4):: ls,cnt,i, l, nr,nl
    type(deque):: rc, lc
    integer(4),allocatable:: ans(:)

    call init_deque(rc)
    call init_deque(lc)

    read*, s
    ls = len_trim(s)
    allocate(ans(ls))

    serch = 'R'
    cnt=0
    do i=1,ls
        if (s(i:i) == serch) then
            cnt=cnt+1
        else
            if (serch == 'R') then
                call append(rc,cnt)
            else
                call append(lc,cnt)
            end if
            cnt=1
            serch = s(i:i)
        end if
    end do
    call append(lc,cnt)
    l = 0
    ans(:) = 0
    do while (remain(rc))
        nr = popleft(rc)
        nl = popleft(lc)
        l=l+nr
        if (mod(nr+nl,2) /= 0) then
            if (mod(nr,2) /= 0) then
                ans(l) = (nl+nr)/2+1
                ans(l+1) = (nl+nr)/2
            else
                ans(l) = (nl+nr)/2
                ans(l+1) = (nl+nr)/2+1
            end if
        else
            ans(l) = (nr+nl)/2
            ans(l+1) = (nr+nl)/2
        end if
        l=l+nl
    end do

    print*, ans(:)
end program Gathering_Children

Measured memory_bytes: 4756