题面

题目描述

给你两个字符串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);
}

Tavas and Malekas的更多相关文章

  1. Codeforces 535D - Tavas and Malekas

    535D - Tavas and Malekas 题目大意:给你一个模板串,给你一个 s 串的长度,告诉你 s 串中有 m 个模板串并告诉你,他们的其实位置, 问你这样的 s 串总数的多少,答案对1e ...

  2. Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp

    题目链接: http://codeforces.com/problemset/problem/535/D D. Tavas and Malekas time limit per test2 secon ...

  3. D. Tavas and Malekas 解析(字串匹配)

    Codeforce 535 D. Tavas and Malekas 解析(字串匹配) 今天我們來看看CF535D 題目連結 題目 給你一個字串$p$和一些$index$代表字串$p$在哪些位置會和長 ...

  4. codeforces 535D. Tavas and Malekas KMP

    题目链接 又复习了一遍kmp....之前都忘光了 #include<bits/stdc++.h> using namespace std; #define pb(x) push_back( ...

  5. CF #299 div1 B. Tavas and Malekas KMP-next数组

    题目链接:http://codeforces.com/contest/536/problem/B 一个原始字符串,一个未知字符串,每一次从pos[i]开始覆盖未知字符串,问最后字符串的形式,以及判断过 ...

  6. Codeforces Round #299 (Div. 2)D. Tavas and Malekas

    KMP,先预处理按每个节点标记,扫一遍更新每个匹配位置,最后kmp判断是否有重合而且不相同的地方 注意处理细节,很容易runtime error #include<map> #includ ...

  7. D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash

    http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...

  8. 【Codeforces Round #299 (Div. 2) D】Tavas and Malekas

    [链接] 我是链接,点我呀:) [题意] 给你n个位置,然后让你从某些位置开始的|p|个位置,填上p这个字符串. 问你填的时候是否会发生冲突->输出0 否则输出最终n个位置组成的可能的字符串的总 ...

  9. [CF535D]Tavas and Malekas 题解

    题意简述 有一个空着的长度为\(n\)的字符串\(ans\),再给出一个长度为\(m\)的序列\(a\),现要在序列中每个元素\(a_i\)的位置开始都规定必须为一个给定的字符串\(s\).问字符串\ ...

随机推荐

  1. 通过SWD J-Link使用J-Link RTT Viewer来查看打印日志

    详细的说明可以参考:https://www.cnblogs.com/iini/p/9279618.html sdk版本: 15.2.0 例程目录:\nRF5_SDK_15.2.0_9412b96\ex ...

  2. The 2018 ACM-ICPC Chinese Collegiate Programming Contest Moving On

    Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each city has a r ...

  3. 强大的with语句

    上下文管理器对象存在的目的是管理 with 语句,就像迭代器的存在是为了管理 for 语句一样. with 语句的目的是简化 try/finally 模式.这种模式用于保证一段代码运行完毕后执行某项操 ...

  4. 光学字符识别OCR-5 文本切割

    经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...

  5. AngularJS自定义指令directive:scope属性 (转载)

    原文地址:http://blog.csdn.net/VitaLemon__/article/details/52213103 一.介绍: 在AngularJS中,除了内置指令如ng-click等,我们 ...

  6. Oracle 表空间的日常维护与管理

    目录 Oracle 表空间的日常维护与管理 1.创建数据表空间 2.创建临时表空间 3.创建 UNDO 表空间 4.表空间的扩展与修改大小 5.表空间重命名 6.表空间的删除 7.更改表空间的读写模式 ...

  7. 初学-BeautifulSoup爬取豆瓣页面

    # -*- coding: utf-8 -*-import osimport urllibimport urllib2from bs4 import BeautifulSoup headers = { ...

  8. bat 中的特殊符号输出问题

    系统关键字(感叹号!)冲突 由于是自动化部署,因此需要使用到循环,这里就不可避免的用到了延迟变量(setlocal enabledelayedexpansion) 有关延迟变量的知识,大家可以通过这篇 ...

  9. [python学习篇][书籍学习][python standrad library][内建类型]之数值

    数值类型 — int, float, long, complex 有四种不同的数值类型:普通整数.长整数.浮点数和复数 普通整数(或者简称整数)使用C中的long实现,其精度至少为32位(sys.ma ...

  10. CF878D D. Magic Breeding bitset

    D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...