Problem
给你两个字符串,你可以在第一个字符串中去掉任意长度的头和尾,使得第一个字符串中包含子序列为第二个字符串
问你有多少种方法
Solution
我们只需要预处理出每个位置上,往前的最后一个a~z字符的位置
然后枚举后面删到哪里,在O(M)求出前面删到哪个位置
用乘法原理统计答案
Notice
有重复的情况,我们需要记录上一次删到的最后位置
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 301013;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
char Derec[N + 5], Emerald[205];
int T[N + 5][26];
int sqz()
{
freopen("substring.in", "r", stdin);
freopen("substring.out", "w", stdout);
scanf("%s", Derec + 1);
int len = strlen(Derec + 1);
rep(i, 0, 25) T[1][i] = 0;
rep(i, 2, len)
{
rep(j, 0, 25) T[i][j] = T[i - 1][j];
T[i][Derec[i - 1] - 'a'] = i - 1;
}
ll ans = 0;
int last = len + 1;
scanf("%s", Emerald + 1);
int t = strlen(Emerald + 1);
per(i, len, 1)
{
if (Derec[i] != Emerald[t]) continue;
int now = i, flag = 1;
per(j, t - 1, 1)
{
now = T[now][Emerald[j] - 'a'];
if (now == 0)
{
flag = 0;
break;
}
}
if (flag)
{
ans = ans + (last - i) * now;
last = i;
}
}
write(ans); puts("");
return 0;
}
浙公网安备 33010602011771号