Program exawizardsC

integer N,Q
character(200000) s 
character,allocatable::t(:),d(:)
integer, allocatable::a(:)

read(*,*) N,Q,s
allocate(t(Q))
allocate(d(Q))
allocate(a(n+2))
do i=1,Q
 read(*,*) t(i),d(i)
end do
a(1)=0
a(N+2)=0
do i=2,N+1
 a(i)=1
end do

do i=1,Q
 if(d(i)=='L') then
  do j=1,N
   if(t(i)==s(j:j)) then
    a(j)=a(j)+a(j+1)
    a(j+1)=0
   end if
  end do
 else
  do j=N,1,-1
   if(t(i)==s(j:j)) then
    a(j+2)=a(j+2)+a(j+1)
    a(j+1)=0
   end if
  end do
 end if
 a(1)=0
 a(N+2)=0
end do
 
write(*,*) sum(a)

stop
end