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+;
const int mod=1e9+;
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();
cin>>n>>m;
cin>>a;
for(int i=;i<m;i++)
{
cin>>b;
vis[b-]=true;
}
//int len=strlen(a);
int len=a.size();
f[]=-;
// memset(f,-1,sizeof(f));
for(int i=;i<len;i++)
{
int j=f[i-];
while((a[i]!=a[j+])&&j>=)
{
j=f[j];
}
if(a[j+]==a[i])
f[i]=j+;
else
f[i]=-;
}
int i=,j=;
int tmp=;
int t=-;
while(i<n)
{
if(vis[i])t=i;
if(t!=-)
{
if(i-t<len)
{
vis1[i]=true;
if(a[i-t]==a[j])
{
j++;
i++;
if(j==len)
{
tmp++;
j=f[j-]+;
}
}
else
{
if(j==)
{
i++;
}
else
j=f[j-]+;
}
}
else
i++; }
else
{
i++;
} }
if(tmp<m)
{
cout<<<<endl;
}
else
{
tmp=;
ll ans=;
for(i=;i<n;i++)
{
if(!vis1[i])ans=(ans*)%mod;
}
cout<<ans<<endl;
}
return ;
}

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

http://codeforces.com/contest/536/problem/B的更多相关文章

  1. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  2. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  3. http://codeforces.com/contest/555/problem/B

    比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...

  4. http://codeforces.com/contest/610/problem/D

    D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. http://codeforces.com/contest/612/problem/D

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. http://codeforces.com/contest/838/problem/A

    A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. http://codeforces.com/contest/402/problem/E

    E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. codeforces.com/contest/251/problem/C

    C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. 关于request和response的中文乱码问题

    相信大家在开发Web项目中都会遇到中文的请求乱码和响应乱码的情况,现在给大家梳理一下并提供解决方案. 1.为什么会出现乱码: 出现乱码的根本原因是浏览器和服务器的解码方式不一致引起的.所以我们统一编码 ...

  2. Wampserver查看php配置信息

    Wampserver安装完成之后输入localhost会有欢迎Wampserver界面. [查看php配置信息]:在页面点击"phpinfo()"进入php配置信息页面. [使用p ...

  3. 使用spring框架处理编码问题

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp90   我们在开发时,经常要对中文字符进行处理,进行处理中文字符的方式也 ...

  4. 通过编译lambda表达式来创建实例(可在反射时候用,效率比反射高一些)

    原文地址:https://rogerjohansson.blog/2008/02/28/linq-expressions-creating-objects/ 据说编译lambda创建实例是比反射快.实 ...

  5. 从web图片裁剪出发:了解H5中的Blob

    刚开始做前端的时候,有个功能卡住我了,就是裁剪并上传头像.当时两个方案摆在我面前,一个是flash,我不会.另一个是通过iframe上传图片,然后再上传坐标由后端裁剪,而我最终的选择是后者.有人会疑惑 ...

  6. MPLS VPN随堂笔记3

    跨域 ASBR之间运行MPBGP 1.配置AS内部IGP保证环回口互相可达,同时建立LDP邻居 (优先启用 mpls label rang 16 100)方便查看实验现象 2.配置PE1-PE2 PE ...

  7. 【1414软工助教】团队作业10——复审与事后分析(Beta版本) 得分榜

    题目 团队作业10--复审与事后分析(Beta版本) 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目2:单元测试 团队作业1:团队展示 团队作业2:需求分析& ...

  8. 201521123075 《Java程序设计》第1周学习总结

    1. 本周学习总结 (1)Java不仅是程序语言,还是一种标准规范,代表着解决问题的方案.Java是一个面向对象的编程语言,熟悉后相对于c++更方便,其一大特色就是能够跨平台运行. (2)Java发展 ...

  9. 201521123033《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. answer; 2. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4- ...

  10. 让SAE下的wordpress支持文件上传

    非PHP程序员照着源码打的小布丁… SAE是不允许本地磁盘读写的,所以使用wordpress撰写文章的时候, 上传控件默认是用不了的,幸好SAE提供了storage服务来存储文件,那就可以修改word ...