题面

题目描述

给你两个字符串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. Python9-loggin模块-day29

    什么叫日志日志 是用来记录用户行为或者代码的执行过程 # import logging # logging.debug('debug message') #低级别的 排除信息 # logging.in ...

  2. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  3. Linux学习-逻辑滚动条管理员 (Logical Volume Manager)

    LVM 可以整合多个实体 partition 在一起, 让这些 partitions 看起来就像是一个磁盘一样!而且,还可以在未来新增或移除其他的实 体 partition 到这个 LVM 管理的磁盘 ...

  4. C语言总结(1)

    1scanf( )和printf( )属于系统的函数,分别表示输入和输出. 2.所有C语言的程序只有一个main( )函数,从这里开始运行. 3.程序先执行main( ),调用scanf( ),最后输 ...

  5. CMMI5

    了解CMMI5是什么? 这种解决问题的思想很有用.

  6. curl post 用json方式

    if(!function_exists('tps_curl_post3')){ function tps_curl_post3($url, $postData) { $postData = json_ ...

  7. curl保存图片

    $url = 'http://p1.qhimg.com/t013dfc89f8a039122c.jpg?size=690x460'; function http_get_data($url) { $c ...

  8. classpath路径

    在项目被集成开发环境编译后,src目录下的东西会编译到WEB-INF/classes目录下,而WEB-INF/classes目录就是所谓的classpath. 将数据库连接配置dataSource.x ...

  9. iOS启动动画效果实现

    原理 在window上加一个UIImageView它的图片和启动图的图片一样,然后再调整动画 运行展示 demo百度云连接:http://pan.baidu.com/s/1c0QcYu0 more:网 ...

  10. Java24种设计模式的优点、缺点和适用环境总结

    一.7个常用的面向对象设计原则 1.单一职责原则: 它是实现高内聚.低耦合的指导方针:一个对象应该只包含单一的职责,并且该职责被完整的封装在一个类中: 2.开闭原则: 指软件实体应尽量在不改变原有的代 ...