Codeforces 535D - Tavas and Malekas (KMP)

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 condition sxisxi + 1... sxi + |p| - 1 = pis 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 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匹配的子串的其实位置是在哪里。

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

思路:

这题题意对于我这种英语渣渣真是不友好,看都看不懂。这题关键就是输入了m个数后,实际上原子符串会不断改变,所以最后不一定会有m个s串(大于m实际上市可以的)。所以这题的思路就大概是先把输入了m个数后的字符串处理出来,然后就是一个KMP,判断一下字符串中有没有m个s,有的话就统计字符串中还没有被赋值的位置的个数k,ans = 26^k%MOD;否则输出0;这里操作T(处理后的字符串)时有一个细节,就是如果你附一个值就处理一遍T的话就会超时,所以我把m个数都设一个标记,在KMP遍历的时候再进行操作。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6+9;
const int MOD = 1e9+7;
char P[MAX_N];
char T[MAX_N];
bool vec[MAX_N];
int Next[MAX_N];
void Makenext()
{
    int len = strlen(P);
    Next[0] = 0;
    for(int i=1,k=0;i<len;i++)
    {
        while(k>0 && P[k] != P[i])
        {
            k = Next[k-1];
        }
        if( P[k] == P[i])
        {
            k++;
        }
        Next[i] = k;
    }
}
int kmp(int N)
{
    int ans = 0;
    int len1 = N;
    int len2 = strlen(P);
    int point = -MOD;
    for(int i=0,k=0;i<len1;i++)
    {
        if(vec[i])
        {
            point = i;
        }
        if(i>=point && i<point+len2)
        {
            T[i] = P[i-point];
            vec[i] = 1;
        }
        while(k>0 && T[i] != P[k])
        {
            k = Next[k-1];
        }
        if(T[i] == P[k]) k++;
        if(k == len2) ans++;
    }
    return ans ;
}
int main()
{
    int N,M;
    while(cin>>N>>M)
    {
        memset(vec,0,sizeof(vec));
        scanf("%s",P);
        Makenext();
        int len = strlen(P);
        for(int i=0;i<M;i++)
        {
            int point ;
            scanf("%d",&point);
            vec[point-1] = 1;
            //memcpy(T+point-1,P,sizeof(P)); .......这样做会超时间!
            //fill(vec+point-1,vec+point-1+len,1);
        }
        int ans = kmp(N);
        //cout<<T<<endl;
        if(ans >= M)
        {
            long long int res = 1;
            for(int i=0;i<N;i++)
            {
                if(!vec[i])
                {
                    res = (res*26)%MOD;
                }
            }
            printf("%lld\n",res);
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值