题面

题目描述

给你两个字符串a和b,告诉所有你b在a中一定匹配的位置,求有中不同的字符串a。a的长度为n,b的长度为m,一定匹配的位置有p个。若b在a中的一定匹配的位置为x,说明a[x…x+m-1]=b[1…m]。a和b中只有小写字母。

输入格式

第一行两个字符串n、p;(1<=n<=100000, 0<=p<=n-m+1)

第二行有一个字符串b;(1<=m<=n)

第三行有p个数:这p个一定匹配的位置。

输出格式

一个数:答案。模10^9+7。

样例

Input1

6 2
ioi
1 3

Output1

26

Input2

5 2
ioi
1 2

Output1

0

样例解释

第一个样例中a的前5个字符为”ioioi”,最后一个字符可以是任意的字符,所以答案为26.

第二个样例中a的第二个字符不可能既是i又是o。

题解

相邻出现的串假如没有重叠的化, 则空位直接统计;

假如出现重叠, 则用扩展KMP的\(match\)数组判断是否合法即可.

#include <cstdio>
#include <cstring>
#include <algorithm> const int L = 1000000, MOD = (int)1e9 + 7; int power(int a, int x)
{
int res = 1;
for(; x; x >>= 1, a = (long long)a * a %MOD)
if(x & 1)
res = (long long)res * a % MOD;
return res;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("Tavas.in", "r", stdin);
#endif
int n, P;
scanf("%d%d\n", &n, &P);
if(! P)
{
printf("%d\n", power(26, n));
return 0;
}
static char str[L];
scanf("%s", str);
static int mch[L];
int len = strlen(str);
mch[0] = len - 1;
int p = 1;
mch[p] = -1;
for(; mch[p] + p < len && str[p + mch[p] + 1] == str[mch[p] + 1]; ++ mch[p]);
int mx = mch[p];
for(int i = 2; i < len; ++ i)
{
mch[i] = std::max(0, std::min(mx - i, mch[i - p]));
for(; i + mch[i] < len && str[i + mch[i] + 1] == str[mch[i] + 1]; ++ mch[i]);
if(i + mch[i] > mx)
p = i, mx = i + mch[i];
}
static int pos[L];
for(int i = 0; i < P; ++ i)
scanf("%d", pos + i);
std::sort(pos, pos + P);
int ans = power(26, pos[0] - 1);
for(int i = 1; i < P; ++ i)
{
if(pos[i] - pos[i - 1] >= len)
ans = (long long) ans * power(26, pos[i] - pos[i - 1] - len) % MOD;
else if(pos[i] - pos[i - 1] < len)
if(mch[pos[i] - pos[i - 1]] ^ len - (pos[i] - pos[i - 1]) - 1)
ans *= 0;
}
if(n - pos[P - 1] < len)
ans *= 0;
printf("%d\n", (long long)ans * power(26, n - pos[P - 1] - len + 1) % MOD);
}

最新文章

  1. 搬-Android - Wi-Fi Tutorial[转]
  2. Excellent Articles
  3. ruby HTTPS请求
  4. HDU 1141---Brackets Sequence(区间DP)
  5. ASP.NET 5探险(4):如何把ASP.NET 5从beta4升级到beta5
  6. request.getRequestDispatcher()的两个方法forward()/include()!!!
  7. *[topcoder]AstronomicalRecords
  8. Exception in thread &quot;main&quot; java.io.IOException: Failed to set permissions of path
  9. ios获取相册图片 压缩图片
  10. 51nod 1595 回文度 | 马拉车Manacher DP
  11. ASP.NET根据当前时间获取,本周,本月,本季度等时间段
  12. AngularJS + RequireJS
  13. golang json 读写配置文件
  14. mybatis运行原理学习
  15. AIX X11卡顿严重
  16. Django 学习第七天——Django模型基础第二节
  17. openssl生成签名与验证签名
  18. WdatePicker控件中日期的范围选择
  19. Android Studio常用快捷键 - 转
  20. docker 数据卷 ---- 基础篇

热门文章

  1. LeetCode(205)Isomorphic Strings
  2. DP刷题记录(长期更新)
  3. PAT Basic 1082
  4. PAT Basic 1077
  5. vmware10下载地址
  6. Elasticsearch安装---安装jdk
  7. #2 create and populate a database &amp;&amp; realistic and practical applications
  8. poj2195 bfs+最小权匹配
  9. jQuery ui 百叶窗blind方向设置
  10. 光学字符识别OCR-2