This page is still under construction.

Parts of this page are still being built. What you see may change.

Substring

Time limit2sMemory limit512 MB

Summary
A window [l, r] over a string moves by changing one endpoint at a time, and after each of m queries you must count how many distinct substring values have appeared across all windows visited.
Level

Hard8 of 10

Topics
String, Sorting, String matching, Implementation
Solved
No attempts yet

Problem

You are given a string s=s1,s2,…,sns = s_1, s_2, \ldots, s_n of length nn and mm queries. Each query qkq_k (1≤k≤m1 \le k \le m) is one of "L++", "L--", "R++", "R--". For the kk-th query qkq_k, define l[k]l[k] and r[k]r[k] as follows.

  • L++: l[k]=l[k−1]+1l[k] = l[k-1] + 1, r[k]=r[k−1]r[k] = r[k-1]
  • L--: l[k]=l[k−1]−1l[k] = l[k-1] - 1, r[k]=r[k−1]r[k] = r[k-1]
  • R++: l[k]=l[k−1]l[k] = l[k-1], r[k]=r[k−1]+1r[k] = r[k-1] + 1
  • R--: l[k]=l[k−1]l[k] = l[k-1], r[k]=r[k−1]−1r[k] = r[k-1] - 1

Here l[0]=r[0]=1l[0] = r[0] = 1.

Find the number of distinct strings among the mm substrings sl[k],sl[k]+1,…,sr[k]−1,sr[k]s_{l[k]}, s_{l[k]+1}, \ldots, s_{r[k]-1}, s_{r[k]} (1≤k≤m1 \le k \le m).

Input

The input is given in the following format.

n m
s
q1
q2
…
qm

Output

Output the answer on one line.

Constraints

  • The string ss consists of lowercase English letters.
  • 1≤n≤3×1051 \le n \le 3 \times 10^5
  • 1≤m≤3×1051 \le m \le 3 \times 10^5
  • qkq_k (1≤k≤m1 \le k \le m) is one of "L++", "L--", "R++", "R--".
  • 1≤l[k]≤r[k]≤n1 \le l[k] \le r[k] \le n (1≤k≤m1 \le k \le m)

Examples3

  1. Example 1

    Input
    5 4
    abcde
    R++
    R++
    L++
    L--
    
    Expected output
    3
    
  2. Example 2

    Input
    4 6
    abab
    R++
    L++
    R++
    L++
    R++
    L++
    
    Expected output
    4
    
  3. Example 3

    Input
    10 13
    aacacbabac
    R++
    R++
    L++
    R++
    R++
    L++
    L++
    R++
    R++
    L--
    L--
    R--
    R--
    
    Expected output
    11