Codeforces Round #299 (Div. 2) D. Tavas and Malekas KMP+预处理、string suffix structures

来源:互联网 发布:网络端口怎么修 编辑:程序博客网 时间:2024/05/05 11:24

D. Tavas and Malekas
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string s of length n comes out from Tavas' mouth instead.

Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on s. Malekas has a favorite string p. He determined all positions x1 < x2 < ... < xk where p matches s. More formally, for each xi (1 ≤ i ≤ k) he conditionsxisxi + 1... sxi + |p| - 1 = p is fullfilled.

Then Malekas wrote down one of subsequences of x1, x2, ... xk (possibly, he didn't write anything) on a piece of paper. Here a sequence b is a subsequence of sequence a if and only if we can turn a into b by removing some of its elements (maybe no one of them or all).

After Tavas woke up, Malekas told him everything. He couldn't remember string s, but he knew that both p and s only contains lowercase English letters and also he had the subsequence he had written on that piece of paper.

Tavas wonders, what is the number of possible values of s? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him.

Answer can be very large, so Tavas wants you to print the answer modulo 109 + 7.

Input

The first line contains two integers n and m, the length of s and the length of the subsequence Malekas wrote down (1 ≤ n ≤ 106 and0 ≤ m ≤ n - |p| + 1).

The second line contains string p (1 ≤ |p| ≤ n).

The next line contains m space separated integers y1, y2, ..., ym, Malekas' subsequence (1 ≤ y1 < y2 < ... < ym ≤ n - |p| + 1).

Output

In a single line print the answer modulo 1000 000 007.

Examples
input
6 2ioi1 3
output
26
input
5 2ioi1 2
output
0
Note

In the first sample test all strings of form "ioioi?" where the question mark replaces arbitrary English letter satisfy.

Here |x| denotes the length of string x.

Please note that it's possible that there is no such string (answer is 0).



Source

Codeforces Round #299 (Div. 2)


My Solution

题意:给一个字符串p,然后一个长为n的字符串s上,对于m个yi,每个yi,有syi-1syi-1+1... syi-1 + |p| - 1 = p,如果s是可能存在的求出s的可能方案数%1e9+7,否则输出0.


KMP+预处理、string suffix structures

搞出字符串p的next数组nxt[i] = max{ k | p[0..k-1] == p[i - k..i-1] },然后预处理出p的next[sz]的所有 {k | p[1..k] == p[i - k + 1..i]},放在bool check[k] == true里。

然后扫一遍y[],对于相邻的2个y 有y[i] - y[i-1] >= sz 时 true。

                                                         有y[i] - y[i-1] < sz  时 如果 check[ sz - (y[i] - y[i - 1]) ] == true,则true,否则 false。

如果答案是存在的,就是区间覆盖问题了,用l,r,然后扫一遍所有[ yi, yi + sz -1] 区间,就可以找出空开的位子的个数cnt,

然后求 ans =   (26^cnt) % (1e9+7) 即可。 //如果cnt == 0,则ans = 1 了

复杂度 O(n)                                                                             


#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;const LL MOD = 1000000007;string s;int nxt[maxn], y[maxn];bool check[maxn];void get_next(const string &p){    int sz = p.size();    for(int i = 1, j; i < sz; i++){        j = i;        while(j > 0){            j = nxt[j];            if(p[j] == p[i]){                nxt[i + 1] = j + 1;                break;            }        }    }}int main(){    #ifdef LOCAL    freopen("d.txt", "r", stdin);    //freopen("d.out", "w", stdout);    int T = 4;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    LL n, m, ans = 1;    cin >> n >> m;    cin >> s;    get_next(s);    int sz = s.size();    /*测试next数组    for(int i = 1; i <= sz; i++){        cout << nxt[i] << " ";    }    cout << endl;    */    int j = sz;    while(j > 0){        j = nxt[j];        check[j] = true;        //cout << j << " ";    }    //cout << endl;    bool flag = true;    cin >> y[0]; y[0]--;    for(int i = 1, j; i < m; i++){        cin >> y[i];        y[i]--;                 //1-based to 0-based        if(y[i] - y[i - 1] >= sz) ;        else if(check[ sz - (y[i] - y[i - 1]) ]){            ;        }        else{            flag = false;            break;        }    }    if(y[m - 1] + sz - 1 >= n) flag = false;    if(flag){        int cnt = 0,l = 0, r;        for(int i = 0; i < m; i++){            if(i == 0){                cnt += y[i] - 0;                l = y[i] + sz;         //l所在的位置是空的            }            else{                cnt += max(0, y[i] - l);                l = y[i] + sz;            }        }        cnt += max((LL)0, n - l);        if(cnt == 0) cout << 1 << endl;  //!        else{            //此处也可用快速幂来求,化O(cnt) 为 O(logcnt),由于此处复杂度够用,故直接求了^_^            while(cnt--){                ans = (ans * 26) % MOD;            }            cout << ans << endl;        }    }    else cout << 0 << endl;    #ifdef LOCAL    memset(check, false, sizeof check);    cout << endl;    }    #endif // LOCAL    return 0;}

  Thank you!

                                                                                                                                               ------from ProLights 

0 0
原创粉丝点击