KMP||扩展KMP(Codeforces 535D - Tavas and Malekas )

D. Tavas and Malekas

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 condition sxisxi + 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 and 0 ≤ 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.

Sample test(s)
Input
6 2
ioi
1 3
Output
26
Input
5 2
ioi
1 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).



题意:现在已知串p,还知道串s中与p匹配的都有那些位置,问这样的s串共有多少个

思路:这个题就是直接用p把匹配的位置填上就可以了,没有填的位置就是可以变换的,假设有x个位置那么答案就是26^x。需要注意的是重复的地方,如果暴力填的话,复杂度太大,所以可以先求KMP,或者扩展KMP,然后判断重叠的部分会不会有冲突

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000100;
const int MOD=1000000007;
typedef long long LL;
char s[maxn];
int N,M;
char p[maxn];
int a[maxn];
int next[maxn];
bool vis[maxn],vis1[maxn];
void getnext(char *s,int len)
{
    next[0]=-1;
    int i=0,j=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j])
        {
            i++,j++;
            next[i]=j;
        }
        else j=next[j];
    }
    int r=len;
    memset(vis1,0,sizeof(vis1));
    while(r!=-1)
    {
        vis1[r]=1;
        r=next[r];
    }
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        scanf("%s",s);
        int len=strlen(s);
        getnext(s,len);
        for(int i=1;i<=M;i++)scanf("%d",&a[i]);
        sort(a+1,a+1+M);
        memset(vis,0,sizeof(vis));
        int flag=true;
        for(int i=1;i<=M;i++)
        {
            if(i==1||a[i]-a[i-1]>=len)
            {
                for(int j=a[i];j<a[i]+len;j++)
                    vis[j]=1;
            }
            else
            {
                if(vis1[a[i-1]+len-a[i]])
                {
                    for(int j=a[i-1]+len;j<a[i]+len;j++)
                        vis[j]=1;
                }
                else {flag=false;break;}
            }
        }
        if(!flag)printf("0\n");
        else
        {
            LL cnt=1;
            for(int i=1;i<=N;i++)
                if(!vis[i])cnt=(cnt*26)%MOD;
            cout<<cnt<<endl;
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值