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

原题链接

D. 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

在字符串s中, s[v1..v1+p-1]是p字符串, s[v2..v2+p-1]也是p字符串,v2 可能会小于等于v1+p-1,那么现在就是要判断s[v2..v1+p-1]是s的前缀也是后缀,用KMP判断即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#define maxn 1000005
#define MOD 1000000007
using namespace std;
typedef long long ll;

int n, m, num[maxn];
char str[maxn];
int vis[maxn], next[maxn];
void KMP(){
	int i = 0, k = -1;
	next[0] = -1;
	while(str[i]){
		if(k == -1 || str[i] == str[k]){
			i++;
			k++;
			next[i] = k;
		}
		else
		 k = next[k];
	}
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	scanf("%d%d", &n, &m);
	scanf("%s", str);
	for(int i = 0; i < m; i++)
	 scanf("%d", num+i);
	KMP();
	int len = strlen(str);
	int pos = len;
	while(pos){
		vis[next[pos]] = 1;
		pos = next[pos];
	}
	int pre = -1, ans = 0;
	for(int i = 0; i < m; i++){
		if(num[i] > pre){
			ans += len;
			pre = num[i] + len - 1;
		}
		else{
			int d = pre - num[i] + 1;
			if(vis[d] == 0){
				puts("0");
				return 0;
			} 
			ans += len - d;
			pre = num[i] + len - 1;
		}
	} 
	n -= ans;
	ll p = 1;
	while(n--){
		(p *= 26) %= MOD;
	}
	printf("%I64d\n", p);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值