Codeforces —— Tavas and Malekas(535D)

input

6 2
ioi
1 3

output

26

input

5 2
ioi
1 2

output

0

题意

给定原始串的长度以及一个模板串p。给出模板串在原始串中出现的位置,问有多少种情况是满足条件的;

思路

求出在原始串中模板串和模板串之间可能叠加的位置 ———— 也就是两个模板串同时出现在原始串中,前面一个串的后缀
与后一个串的前缀重合的部分是否冲突,这里很容易想到用next数组从最后跑一边,看有哪些长度是满足该长度后缀也是
串的前缀就可以

代码

#pragma GCC optimize(2)
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define rush() int Case = 0; int T; scanf("%d", &T);  while(T--)
#define rep(i, a, for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)efine reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define sqr(x) x*x
#define rs x<<1|1
#define y second
#define ls x<<1
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
char s[N];
int ne[N], a[N], l;
bool vis[N];
void next()
{
    for(int i = 2, j = 0; s[i]; i ++)
    {
        while(j && s[i] != s[j+1])  j = ne[j];
        if(s[i] == s[j+1]) j ++;
        ne[i] = j;
    }
    for(int p = l; p; p = ne[p])    vis[p] = true;
}
int qim(int a, int b)
{
    int res = 1;
    while(b)
    {
        if(b&1) res = 1ll * res * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return res;
}
int main()
{
    Buff;
    int n, m;
    cin >> n >> m;
    cin >> (s+1);
    if(!m)
    {
        cout << qim(26, n) <<endl;
        return 0;
    }
//    cout << (s+1) <<endl;
    l = strlen(s+1);    next();
    rep(i, 1, m)    cin >> a[i]; a[m+1] = n+1;
    int res = n;    bool flag = true;
    per(i, m, 1)
    {
        int t = a[i+1] - a[i];
        if(t >= l)  res -= l;
        else
        {
            if(!vis[l-t])
            {
                flag = false;
                break;
            }
            res -= t;
        }
    }
    if(!flag)   cout << 0 << endl;
    else        cout << qim(26, res) <<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/Farrell-12138/p/13806356.html