      PROGRAM daydream
      IMPLICIT NONE
      CHARACTER (10**5):: s
      INTEGER :: index,Ls(4),N,i
      LOGICAL :: flag
      CHARACTER(7) :: a(4)

      READ*,s

      a = (/'dreamer','dream  ','eraser ','erase  '/)
      Ls = (/7,5,6,5/)

      N = len_trim( s )

      index = 1
      outer:DO while(.TRUE.)
        inner:DO i = 1,4
          IF(index+Ls(i)-1<=N)THEN
            IF( trim(a(i))==s(index:index+Ls(i)-1) )THEN
              index = index + Ls(i)

              IF(index-1==N)THEN
                flag = .true.
                EXIT outer
              END IF
              CYCLE outer

            END IF
          END IF
        END DO inner
        flag = .false.
        EXIT outer

      END DO outer

      IF(flag)THEN
        print'(A)','YES'
      ELSE
        print'(A)','NO'
      END IF

      END PROGRAM


character(10**5) S
character(5)::dream="dream"
character(7)::dreamer="dreamer"
character(5)::erase="erase"
character(6)::eraser="eraser"
integer start
logical check
read*,S
start=1
check=.TRUE.
do
  if(S(start:start+6)==dreamer)then
    start=start+7
  else if(S(start:start+4)==dream)then
    start=start+5
  else if(S(start:start+5)==eraser)then
    start=start+6
  else if(S(start:start+4)==erase)then
    start=start+5
  else
    check=.false.
    exit
  endif
  if(start==len_trim(S)+1)exit
end do
print"(A)",trim(merge("YES","NO ",check))
end

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

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

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


program test
implicit none
integer::l
character::S*100000
read(*,*)S
l=len_trim(S)
do
	if(l==0)then
    print*,'YES'
    stop
	else if(S(l-4:l)=='dream')then
    	l=l-5
   	else if(S(l-6:l)=='dreamer')then
    	l=l-7
    else if(S(l-4:l)=='erase')then
    	l=l-5
    else if(S(l-5:l)=='eraser')then
    	l=l-6
    else
    	print*,'NO'
        stop
    end if
end do
end program test


program tenquestionnine
 implicit none
 character(10**5) :: S
 character(7) :: T(4) = (/"dream  ","dreamer","erase  ","eraser "/)
 integer :: L(4) = (/5,7,5,6/)
 integer i, len
 read(*,*) S
 len=len_trim(S)
 a: do while (len.gt.0)
  do i=1,4
   if((trim(T(i))).eq.(S((len-L(i)+1):len))) then
   len = len - L(i)
   cycle a
   end if
  end do
  write(*,*) "NO"
  return
  end do a
  write(*,*) "YES"
end program


program main
   character(len=7) :: s(4) = (/"dream  ", "dreamer", "erase  ", "eraser "/)
   character(len=100000) :: b
   integer :: L(4) = (/5, 7, 5, 6/)
   read(*, *) b
   N = len_trim(b)
   a: do while(N > 0)
      do i = 1, 4
         if(N < L(i)) cycle
         if(trim(s(i)) == b(N-L(i)+1:N))then
            N = N - L(i)
            cycle a
         endif
      enddo
      write(*, *)"NO"
      stop
   enddo a
   write(*, *) "YES"
end


program main
   character(len=3) :: s(4) = (/"eam", "mer", "ase", "ser"/)
   character(len=100000) :: b
   integer :: L(4) = (/5, 7, 5, 6/)
   read(*, *) b
   N = len_trim(b)
   a: do while(N > 0)
      if (N < 5) then
         write(*, *)"NO"
      stop
      endif
      do i = 1, 4
         if(s(i) == b(N-2:N))then
            N = N - L(i)
            cycle a
         endif
      enddo
      write(*, *)"NO"
      stop
   enddo a
   write(*, *) "YES"
end


implicit none
integer(8):: n,y,i,j,count,s_len
character(10**5+5)::s,s_rev
character(5)::a
character(5)::b
character(6)::c
character(7)::d

a="maerd"
b="esare"
c="resare"
d="remaerd"

s_rev=""

read(5,*) s
s_len=len_trim(s)

do i=1,s_len
    s_rev(s_len+1-i:s_len+1-i)=s(i:i)
enddo

!write(6,*) trim(s_rev)

count=1

do while (count<=s_len)

    if (index(s_rev,a)==1) then
        count=count+len(a)
        s_rev=s_rev(len(a)+1:)

    else if (index(s_rev,b)==1) then
        count=count+len(b)
        s_rev=s_rev(len(b)+1:)

    else if (index(s_rev,c)==1) then
        count=count+len(c)
        s_rev=s_rev(len(c)+1:)

    else if (index(s_rev,d)==1) then
        count=count+len(d)
        s_rev=s_rev(len(d)+1:)

    else
        write(6,*) "NO"
        goto 100

    endif

enddo

write(6,*) "YES"

100 end


implicit none
integer(8):: n,y,i,j,count,s_len
character(10**5+5)::s,s_rev
character(5)::a
character(5)::b
character(6)::c
character(7)::d

a="maerd"
b="esare"
c="resare"
d="remaerd"

s_rev=""

read(5,*) s
s_len=len_trim(s)

do i=1,s_len
    s_rev(s_len+1-i:s_len+1-i)=s(i:i)
enddo

!write(6,*) trim(s_rev)

count=1

do while (count<=s_len)

!    write(6,*) index(s_rev,a),index(s_rev,b),index(s_rev,c),index(s_rev,d)

    if (index(s_rev,a)==1) then
        count=count+5
        s_rev=s_rev(6:)
    else if (index(s_rev,b)==1) then
        count=count+5
        s_rev=s_rev(6:)
    else if (index(s_rev,c)==1) then
        count=count+6
        s_rev=s_rev(7:)
    else if (index(s_rev,d)==1) then
        count=count+7
        s_rev=s_rev(8:)
    else
        write(6,*) "NO"
        goto 100

    endif

!    write(6,*) trim(s_rev)

enddo

write(6,*) "YES"

100 end


implicit none
integer(8):: n,y,i,j,count,s_len
character(10**5+5)::s,s_rev
character(5)::a
character(5)::b
character(6)::c
character(7)::d

a="maerd"
b="esare"
c="resare"
d="remaerd"

s_rev=""

read(5,*) s
s_len=len_trim(s)

do i=1,s_len
    s_rev(s_len+1-i:s_len+1-i)=s(i:i)
enddo

write(6,*) trim(s_rev)

count=1

do while (count<=s_len)

!    write(6,*) index(s_rev,a),index(s_rev,b),index(s_rev,c),index(s_rev,d)

    if (index(s_rev,a)==1) then
        count=count+5
        s_rev=s_rev(6:)
    else if (index(s_rev,b)==1) then
        count=count+5
        s_rev=s_rev(6:)
    else if (index(s_rev,c)==1) then
        count=count+6
        s_rev=s_rev(7:)
    else if (index(s_rev,d)==1) then
        count=count+7
        s_rev=s_rev(8:)
    else
        write(6,*) "NO"
        goto 100

    endif

!    write(6,*) trim(s_rev)

enddo

write(6,*) "YES"

100 end


implicit none
integer(8):: n,y,i,j,count,s_len
character(10**5+5)::s,s_rev
character(5)::a
character(5)::b
character(6)::c
character(7)::d

a="maerd"
b="esare"
c="resare"
d="remaerd"

s_rev=""

read(5,*) s
s_len=len_trim(s)

do i=1,s_len
    s_rev(s_len+1-i:s_len+1-i)=s(i:i)
enddo

count=1

do while (count<=s_len)

    if (index(s_rev,a)==count) then
        count=count+5

    else if (index(s_rev,b)==count) then
        count=count+5

    else if (index(s_rev,c)==count) then
        count=count+6

    else if (index(s_rev,d)==count) then
        count=count+7

    else
        write(6,*) "NO"
        goto 100

    endif

enddo

write(6,*) "YES"

100 end


implicit none
integer(8):: n,y,i,j,count,s_len
character(10**5)::s,s_rev
character(5)::a
character(5)::b
character(6)::c
character(7)::d

a="maerd"
b="esare"
c="resare"
d="remaerd"

s_rev=""

read(5,*) s
s_len=len_trim(s)

do i=1,s_len
    s_rev(s_len+1-i:s_len+1-i)=s(i:i)
enddo

count=1

do while (count<=s_len)

    if (index(s_rev,a)==count) then
        count=count+5

    else if (index(s_rev,b)==count) then
        count=count+5

    else if (index(s_rev,c)==count) then
        count=count+6

    else if (index(s_rev,d)==count) then
        count=count+7

    else
        write(6,*) "NO"
        goto 100

    endif

enddo

write(6,*) "YES"

100 end


program prob4
    implicit none
    integer :: n, i, ans
    character(100005) :: s
    read(*,*) s
    n = len_trim(s)
    ans = 0
    do i = 1, n-1
        if(s(i:i) /= s(i+1:i+1)) then
            ans = ans + 1
        end if
    end do
    write(*,*) ans

    stop
contains
end program prob4

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


program One_dimentional_reverse
    implicit none
    character(100000):: s
    integer(4)ls, i, cnt

    read*, s
    ls = len_trim(s)
    cnt=0
    do i=1, ls-1
        if(s(i:i) /= s(i+1:i+1)) cnt=cnt+1
    end do

    print*, cnt

end program One_dimentional_reverse


implicit none
integer :: i,n,ans
character(100000) :: s
character(1) :: t

read *,s
n = len_trim(s)

t = s(1:1)
ans = 0
do i = 2, n
   if (s(i:i) /= t) then
      ans = ans + 1
      t = s(i:i)
   endif
enddo

print '(i0)',ans
end


   program abc042b
      implicit none
      character(100),allocatable,dimension(:)::s
      integer::n,l
      integer::i

         read *, n,l
         allocate(s(n))
         do i=1,n
            read *, s(i)
         end do

         call sort_bubble(n,s)

         do i=1,n
            write(*,'(A)',advance='no') s(i)(1:l)
         end do
         stop

   contains
      subroutine sort_bubble(n,arr)
       ! 最も直感的
       ! O(N^2)
       ! if want to sort the array of strings, change 't(int)' to 'w(character)'
         implicit none
         integer,intent(in)::n
         character(100),intent(inout)::arr(1:n)
         integer::i,j
         character(100)::t

            do i=1,n-1
               do j=i+1,n
                  if(arr(i) > arr(j))then
                     t=arr(i)
                     arr(i)=arr(j)
                     arr(j)=t
                  end if
               end do
            end do

            return
      end subroutine sort_bubble

   end program abc042b

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

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

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


program main
    implicit none
    integer n, l
    character(:), allocatable, dimension(:) :: s
    read *, n, l
    allocate(character(l) :: s(n))
    read *, s
    call bubblesort(s, smallerDict)
    print *, s
!    write (*, fmt='(a)', advance='no') s

contains
    recursive logical function smallerDict(str1, str2) result(ret)
        character(*), intent(in) :: str1, str2

        if ( len_trim(str2) == 0 ) then
            ret = .false.
        endif

        if ( len_trim(str1) == 0 ) then
            ret = .true.
        endif

        if ( iachar(str1(1:1)) < iachar(str2(1:1)) ) then
            ret = .true.
        else if ( iachar(str1(1:1)) > iachar(str2(1:1)) ) then
            ret = .false.
        else
            ret = smallerDict(str1(2:), str2(2:))
        endif
    end function

    subroutine bubblesort(array, compareFunction)
        character(*), dimension(:), intent(inout) :: array

        interface
            logical function compareFunction(str1, str2)
                implicit none
                character(*), intent(in) :: str1, str2
            end function
        end interface

        integer i
        character(len(array(1))) temp
        logical update

        do
            update = .false.
            do i=1, size(array) - 1
                if ( .not. compareFunction(array(i), array(i + 1)) ) then
                    temp = array(i)
                    array(i) = array(i + 1)
                    array(i + 1) = temp
                    update = .true.
                endif
            end do
            if ( .not. update ) then
                exit
            endif
        end do
    end subroutine
end program main


module ch
  implicit none
  type char
     character(100)      :: chara
  end type char
  type(char),allocatable :: namec(:)
end module ch
program main

  use ch, only : namec

  implicit none
  integer :: n, l
  character(100) :: ans
  integer, allocatable :: s(:)
  integer :: i,j,k

  read(*,*) n, l

  allocate( s(n) )
  allocate( namec(n) )

  do i = 1, n
    read(*,*) namec(i)%chara
  end do

  call quicksort1( 1, n )

  j = 1
  do i = 1, n
    ans(j:j+l) = trim( namec(i)%chara )
    j = j + l
  end do
  ans = ans(1:j)
  print*,ans

end program main
recursive subroutine quicksort1( first, last )
!recursive subroutine quicksort1( a%chara, first, last )

  use ch, only : namec

  implicit none
  integer,intent(in)::first,last
  !type char
  !   character(100) :: chara
  !end type
  !type(char),intent(inout)::a(*)
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(100)::x,t
  integer::i,j

  x = namec((first+last)/2)%chara
  !x = a((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namec(i)%chara ) )
     !do while ( LGT( x, a(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namec(j)%chara, x ) )
     !do while ( LGT( a(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namec(i)%chara
     namec(i)%chara = namec(j)%chara
     namec(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1( first, i-1  )
  if (j+1 < last)  call quicksort1( j+1  , last )

  return

end subroutine quicksort1

!recursive subroutine quicksort1(a, first, last )
!  implicit none
!  integer,intent(inout)::a(*)
!  integer,intent(in)::first,last
!  !
!  ! Original
!  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
!  !
!  integer::x,t
!  integer::i,j
!
!  x = a((first+last)/2)
!  i = first
!  j = last
!  do
!     do while (a(i) < x)
!        i=i+1
!     end do
!     do while (x < a(j))
!        j=j-1
!     end do
!     if (i >= j) exit
!     t = a(i)
!     a(i) = a(j)
!     a(j) = t
!     i=i+1
!     j=j-1
!  end do
!  if (first < i-1) call quicksort1(a, first, i-1)
!  if (j+1 < last)  call quicksort1(a, j+1, last)
!
!  return
!end subroutine quicksort1


program main
implicit none
integer :: n, l
character( len = 100 ), allocatable :: str(:)
character( len = 100 ), allocatable :: res(:)
integer :: i, j

read(*,*) n, l
allocate( str(n) )
allocate( res(n) )
do i = 1, n
   read(*,*) str(i)
end do

do i = 1 , n
   do j = i, n
      if( llt( str(i)(1:l), str(j)(1:l) ) .eqv. .true. ) then
      else
         res(i)(1:l) = str(i)(1:l)
         str(i)(1:l) = str(j)(1:l)
         str(j)(1:l) = res(i)(1:l)
      end if
   end do
end do
do i = 1, n
   write(*,*) str(i)(1:l)
end do

end program main


PROGRAM ATCODER

implicit none
integer :: n, l, i, j
character(len = 100),allocatable :: a(:)
character(len = 100) :: c
character(len = 100*100) :: answer

read(*,*) n, l
allocate (a(n))

do i = 1, n
	read(*,*) a(i)
end do

! ここに計算式
do i = 1, n
	do j = i+1, n
		if (lgt(a(i),a(j)))then
			c = a (i)
			a(i) = a(j)
			a(j) = c
		end if
	end do
end do
! ここに出力結果

answer = trim(a(1))
do i = 2, n
	answer = trim(answer)//trim(a(i))
end do

write(*,'(a)') trim(answer)

END PROGRAM ATCODER


PROGRAM ATCODER

implicit none
integer :: n, l, i, j
character(len = 100),allocatable :: a(:)
character(len = 100) :: c
character(len = 100*100) :: answer

read(*,*) n, l
allocate (a(n))

do i = 1, n
	read(*,*) a(i)
end do

! ここに計算式
do i = 1, n
	do j = i+1, n
		if (lgt(a(i),a(j)))then
			c = a (i)
			a(i) = a(j)
			a(j) = c
        end if
	end do
end do
! ここに出力結果

answer = trim(a(1))
do i = 2, n
	answer = trim(answer)//trim(a(i))
end do

write(*,'(a)') trim(answer)

END PROGRAM ATCODER