http://codeforces.com/contest/536/problem/B

B. 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的字符串,和字符子串b,以及子串出现的位置,问你原字符串有多少种可能性?

题解:第一个KMP,先用一个vis的数组标记子串第个字符出现的位置,用kmp,去验证子串是否出现了M次,少于的话就是0;验证的过程中再用以个vis1数组标记子串

出现过的位置(两个vis这样可以免去赋值很巧妙有没有,我学别人的http://www.cnblogs.com/widsom/p/7364861.html)最后求出没有被标记的个数ans,26^ans%mod就是答案。代码如下:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm> 
#include<string> 
#define ll long long 
using namespace std;
const int maxn=1e6+5; 
const int mod=1e9+7; 
bool vis[maxn],vis1[maxn]; 
int b,f[maxn],n,m; 
//char a[maxn]; 
string a; 
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    cin>>a;
    for(int i=0;i<m;i++)
    {
        cin>>b; 
        vis[b-1]=true; 
    }
    //int len=strlen(a); 
    int len=a.size(); 
    f[0]=-1;
//    memset(f,-1,sizeof(f)); 
    for(int i=1;i<len;i++)
    {
        int j=f[i-1];
        while((a[i]!=a[j+1])&&j>=0)
        {
            j=f[j]; 
        }
        if(a[j+1]==a[i])
            f[i]=j+1;
        else 
            f[i]=-1; 
    } 
        int i=0,j=0;
        int tmp=0; 
        int t=-1; 
    while(i<n)
    {
        if(vis[i])t=i;
        if(t!=-1)
        {
            if(i-t<len)
            {
                vis1[i]=true; 
                if(a[i-t]==a[j])
                {
                    j++;
                    i++;
                    if(j==len)
                    {
                        tmp++; 
                        j=f[j-1]+1; 
                    } 
                }
                else 
                {
                    if(j==0)
                    {
                        i++; 
                    }
                    else
                    j=f[j-1]+1; 
                } 
            }
            else
                i++; 
            
        }
        else
        {
            i++; 
        } 
        
    } 
    if(tmp<m)
    {
        cout<<0<<endl; 
    }
    else
    {
         tmp=0;
         ll ans=1; 
         for(i=0;i<n;i++)
         {
             if(!vis1[i])ans=(ans*26)%mod; 
         } 
         cout<<ans<<endl; 
    } 
    return 0; 
} 

学习kmp这篇博客不错博主用心了http://www.cnblogs.com/SYCstudio/p/7194315.html

原文地址:https://www.cnblogs.com/lhclqslove/p/7375355.html