Micul String

Time limit: 2s Memory limit: 512MB Input: Output:

A subsequence of a string of characters ss is a string of characters tt with the property that there exist two indices ii and jj with 0≀i≀j<∣s∣0 \leq i \leq j < |s| such that t=sisi+1si+2...sjβ€Ύt=\overline{s_is_{i+1}s_{i+2}...{s_j}}.

For a tuple of strings (s0,s1,s2,...,sKβˆ’1)(s_0, s_1, s_2, ..., s_{K-1}) we define f(s0,s1,s2,...,sKβˆ’1)f(s_0,s_1,s_2,...,s_{K-1}) as the lexicographically smallest1^1 string that can be obtained through the following procedure:

  • For each string sis_i, choose a nonempty substring tit_i of it;
  • Concatenate t0,t1,t2,...,tKβˆ’1t_0, t_1, t_2,..., t_{K-1}, the resulting string being the outcome of the procedure.

1^1 A string a0a1…anβˆ’1β€Ύ\overline{a_0a_1 \ldots a_{n-1}} is lexicographically smaller than another string b0b1…bmβˆ’1β€Ύ\overline{b_0b_1 \ldots b_{m-1}} if and only if:

  • There exists an index ii (0≀i<min⁑(n,m)0 \le i < \min(n,m)) for which a0a1…aiβˆ’1β€Ύ=b0b1…biβˆ’1β€Ύ\overline{a_0a_1 \ldots a_{i-1}}=\overline{b_0b_1 \ldots b_{i-1}} and ai<bia_i<b_i; or
  • n<mn<m and a0a1…anβˆ’1β€Ύ=b0b1…bnβˆ’1β€Ύ\overline{a_0a_1 \ldots a_{n-1}}=\overline{b_0b_1 \ldots b_{n-1}}.

Requirement

You are given a natural number NN, a string of length NN denoted ww, a natural number KK, and a sequence of KK nonzero natural numbers l0,l1,l2,...,lKβˆ’1l_0, l_1, l_2,..., l_{K-1}. Count how many tuples of KK strings, (s0,s1,s2,...,sKβˆ’1)(s_0, s_1, s_2, ..., s_{K-1}) satisfy the following conditions:

  • All strings contain only lowercase letters of the English alphabet
  • ∣si∣=li|s_i|=l_i, βˆ€1≀i≀K\forall 1 \leq i \leq K
  • f(s0,s1,s2,...,sKβˆ’1)=wf(s_0, s_1, s_2, ..., s_{K - 1})=w

Since this number can be large, output its remainder modulo 998Β 244Β 353998 \ 244 \ 353.

Implementation Details

You must implement a single function:

int solve(int N, int K, std::string w, std::vector<int> l);

which receives as parameters:

  • NN, the number of characters in the string ww
  • KK, the number of strings in a tuple
  • The string ww (indexed from 00)
  • ll, representing the lengths of the strings (indexed from 00)

The solve function will be called exactly once.

Constraints

  • 1≀K≀N≀2001 \leq K \leq N \leq 200
  • 1≀li≀2001 \leq l_i \leq 200, 0≀i≀Nβˆ’10 \leq i \leq N - 1
  • The string ww consists only of lowercase letters of the English alphabet
  • We denote by wiw_i the ii-th character of the string ww
# Score Constraints
1 7 1≀K≀N≀71\leq K \leq N \leq 7, βˆ‘0Kβˆ’1li≀8\sum_0^{K-1}l_i \leq 8 and ww contains only the characters a, b, c, d
2 8 1≀K≀N≀251 \leq K \leq N \leq 25 and li=3l_i=3, βˆ€Β 0≀i<N\forall \ 0 \leq i < N
3 11 K=2K=2
4 14 1≀K≀N≀1201 \leq K \leq N \leq 120, 1≀li≀1201 \leq l_i \leq 120 and wi≀wi+1,βˆ€Β 0≀i<Nβˆ’1w_i \leq w_{i+1}, \forall \ 0 \leq i < N - 1 (the string ww is non-decreasing)
5 23 1≀K≀N≀901\leq K \leq N \leq 90 and 1≀li≀901 \leq l_i \leq 90, βˆ€Β 0≀i<N\forall \ 0 \leq i < N
6 21 1≀K≀N≀1501\leq K \leq N \leq 150 and 1≀li≀1501 \leq l_i \leq 150, βˆ€Β 0≀i<N\forall \ 0 \leq i < N
7 16 No additional restrictions.

Example 1

input

4 3
babz
1 2 1

output

1

Explanation

For the first example, the only valid tuple is ("b", "ab", "z").

Example 2

input

4 3
babz
1 3 1

output

26

Example 3

input

6 4
abcbzz
3 3 3 3

output

1849

Log in or sign up to be able to send submissions!