codeforces535D:Tavas and Malekas(KMP)

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 sequenceb 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).



这道题的题意真的是理解了相当之久,一个半小时差不多有了,

首先输入n,m,说明原序列长度为n,在原序列中有m个与s相互匹配的串

然后输入子串s

最后m个数,分别带别了这m个与s匹配的子串的其实位置是在哪里

问,在确定了这些位置的字符之后,原串有多少种可能


这道题带我复习了一遍KMP的next数组,上一次做KMP都还是N年以前的事情了


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 1000006
#define mod 1000000007
const int INF = 0x3f3f3f3f;

LL n,m,next[Len],ans,a[Len],len;
char str[Len];
bool hsh[Len];

void get_next()
{
    LL k = -1,j = 0;
    next[0] = -1;
    w(j<len)
    {
        if(k == -1 || str[k] == str[j])
        {
            j++;
            k++;
            next[j] = k;
        }
        else
        {
            k = next[k];
        }
    }
}

void dfs(LL k)
{
    if(k==0) return;
    hsh[len-k+1] = true;
    dfs(next[k]);
}

LL solve(LL k)
{
    int i;
    if(k < 0) return 0;
    LL ret = 1;
    up(i,1,k)
    ret = (ret*26)%mod;
    return ret;
}

int main()
{
    LL i,j,k,flag;
    scanf("%I64d%I64d",&n,&m);
    {
        flag = 1;
        scanf("%s",str);
        len = strlen(str);
        get_next();//求出next数组
        dfs(next[len]);//从最后一个开始,对循环串进行标记
        ans = 1;
        if(m==0)//没有的话,每个位置都是26种可能
        {
            printf("%I64d\n",solve(n));

        }
        else
        {
            scanf("%I64d",&a[1]);
            ans = solve(a[1]-1);//先看a[1]位置之前又几个,每一个都是26种可能
            up(i,2,m)
            {
                scanf("%I64d",&a[i]);
                if(a[i]-a[i-1]<len)//如果前后相隔不大于str,那么我们必须要看是否嵌入了循环体
                {
                    if(!hsh[a[i]-a[i-1]+1])
                    {
                        flag = 0;
                        printf("0\n");
                        break;
                    }
                }
                else//相隔大于len,那么出去str串中间多余的那几个字符每个都是26种可能
                    ans = (ans*solve(a[i]-a[i-1]-len))%mod;
            }
            if(flag)//最后几个字符每个也是26种可能
                printf("%I64d\n",ans*solve(n-a[m]-len+1)%mod);
        }
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值