Harmonic Operations

시간 제한1초메모리 제한1024 MB

요약
주어진 문자열에 역전과 회전 연산의 부분 리스트를 적용했을 때 문자열이 그대로 유지되는 (i, j) 쌍의 개수를 센다.
난이도

어려움10점 중 8점

유형
문자열, 해시맵, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

In this problem we consider three types of operations that can be applied to any 00-based string tt of length ∣t∣≥2|t| ≥ 2.

I(t)I(t): Reverses tt.

R(t,D)R(t, D): Rotates tt to the right DD positions, for some positive integer D<∣t∣D < |t|. That is, for each 0≤i<∣t∣0 ≤ i < |t|, the character at position (i+D) mod ∣t∣(i+D) \bmod |t| in R(t,D)R(t, D) is the character at position ii in tt.

L(t,D)L(t, D): Analogous to R(t,D)R(t, D), but rotates tt to the left instead of to the right.

For example, I(I(“pda”)=) = “adp”, R(R(“pda”,2)=, 2) = “dap”, and L(L(“pda”,2)=, 2) = “apd”. Note that for any tt and any DD it holds that ∣I(t)∣=∣R(t,D)∣=∣L(t,D)∣=∣t∣|I(t)| = |R(t, D)| = |L(t, D)| = |t|.

When a list of the above operations is applied to a string, it is done sequentially in list order. That is, the first operation of the list is applied to the original string, the second operation is applied to the result after having applied the first operation, the third operation is applied to the result after having applied the first two operations, and so on.

You are given a string SS consisting of lowercase letters, and a list of KK operations F_1,F_2,…,F_KF\_1, F\_2, \dots , F\_K. Your task is to find out how many pairs of indices (i,j)(i, j) there are such that 1≤i≤j≤K1 ≤ i ≤ j ≤ K, and applying the sublist of operations F_i,F_i+1,…,F_jF\_i , F\_{i+1}, \dots , F\_j to SS yields SS as the final result.

Consider for instance S=S = “pda”, K=2K = 2, F_1=R(t,2)F\_1 = R(t, 2) and F_2=L(t,2)F\_2 = L(t, 2). The result of applying the sublist F_1F\_1 to SS is R(R(“pda”,2)=, 2) = “dap”, which is different from SS. The result of applying the sublist F_1F\_1, F_2F\_2 to SS is L(R(L(R(“pda”,2),2)=L(, 2), 2) = L(“dap”,2)=, 2) = “pda” =S= S. Finally, the result of applying the sublist F_2F\_2 to SS is L(L(“pda”,2)=, 2) = “apd”, which is different from SS. Thus, in this example the answer is 11.

입력

The first line contains a string SS (2≤∣S∣≤2⋅1052 ≤ |S| ≤ 2 \cdot 10^5) which is made up of lowercase letters.

The second line contains an integer KK (1≤K≤2⋅1051 ≤ K ≤ 2 \cdot 10^5) indicating the number of operations in the list of operations that is being considered.

Operations are described in the next KK lines, in the order they appear in the list, one operation per line. If the operation is I(t)I(t), the line contains the uppercase letter “I”. If the operation is R(t,D)R(t, D), the line contains the uppercase letter “R” and the integer DD (1≤D<∣S∣1 ≤ D < |S|). Finally, if the operation is L(t,D)L(t, D), the line contains the uppercase letter “L” and the integer DD (1≤D<∣S∣1 ≤ D < |S|).

출력

Output a single line with an integer indicating the requested number of pairs.

예제3

  1. 예제 1

    입력
    pda
    2
    R 2
    L 2
    
    예상 출력
    1
    
  2. 예제 2

    입력
    aaa
    4
    R 1
    I
    I
    R 1
    
    예상 출력
    10
    
  3. 예제 3

    입력
    caso
    6
    L 1
    I
    I
    R 1
    I
    I
    
    예상 출력
    4