【扩展KMP 组合数学 滑动窗口】P9149 串串题|省选-
本文涉及知识点
组合数学汇总
较难理解的字符串查找算法KMP
C++算法:滑动窗口及双指针总结
P9149 串串题
题目描述
给定长度分别为 n , m n,m n,m 的整数序列 A , B A,B A,B 和常数 W , d W,d W,d,序列从 1 1 1 开始标号,保证 A i , B i ∈ [ 1 , W ] A_i,B_i \in [1,W] Ai,Bi∈[1,W]。
容易发现,我们有 ( W d ) \binom{W}{d} (dW) 种方案选择 [ 1 , W ] [1,W] [1,W] 中的 d d d 个互不相同的整数。
对于每一种选择的方案,我们删去 A A A 中出现的对应的 d d d 种整数,令此时序列 B B B 在序列 A A A 中的出现次数为这次选择方案的权值。
你需要求所有的选择方案的权值和,对 10 9 + 7 {10}^9+7 109+7 取模。
若对题意有疑问,请阅读样例及样例解释。
注: ( a b ) \binom{a}{b} (ba) 表示组合数,含义为在 a a a 个物品中无序地选择出 b b b 个物品的方案数。
请注意:我们并不会删除序列 B \bm{B} B 中出现的对应整数。
输入格式
本题有多组数据。
第一行,一个正整数 T T T,表示数据组数。对于每组数据:
第一行,四个正整数 n , m , W , d n, m, W, d n,m,W,d,保证 d ≤ W d \le W d≤W。
第二行, n n n 个正整数 A 1 , A 2 , … , A n A_1, A_2, \ldots, A_n A1,A2,…,An,表示序列 A A A。
第三行, m m m 个正整数 B 1 , B 2 , … , B m B_1, B_2, \ldots, B_m B1,B2,…,Bm,表示序列 B B B。
输出格式
对于每组数据,输出一个整数表示答案对 10 9 + 7 {10}^9+7 109+7 取模的结果。
输入输出样例 #1
输入 #1
2
4 2 3 1
1 1 2 1
1 1
8 3 4 1
1 2 3 1 2 3 1 2
1 2 1
输出 #1
3
2
说明/提示
【样例解释】
在样例的第一组数据中:
- 如果我们选择删去 A A A 中的字符 1 1 1, A A A 将变为 { 2 } \{2\} {2},此时 B B B 在 A A A 中的出现次数为 0 0 0。
- 如果我们选择删去 A A A 中的字符 2 2 2, A A A 将变为 { 1 , 1 , 1 } \{1,1,1\} {1,1,1},此时 B B B 在 A A A 中的出现次数为 2 2 2。
- 如果我们选择删去 A A A 中的字符 3 3 3, A A A 将变为 { 1 , 1 , 2 , 1 } \{1,1,2,1\} {1,1,2,1},此时 B B B 在 A A A 中的出现次数为 1 1 1。
因此,第一组数据的答案为 0 + 2 + 1 = 3 0+2+1=3 0+2+1=3。
再次提醒:我们并不会删除序列 B \bm{B} B 中出现的对应整数。
【数据范围】
对于 100 % 100\% 100% 的数据, 1 ≤ n , m , W ≤ 10 6 1 \le n,m,W \le {10}^6 1≤n,m,W≤106, 1 ≤ d , A i , B j ≤ W 1 \le d, A_i, B_j \le W 1≤d,Ai,Bj≤W, 1 ≤ T ≤ 5 1 \le T \le 5 1≤T≤5。
本题采用捆绑测试且开启子任务依赖!
| 子任务 | n ≤ n \le n≤ | m ≤ m \le m≤ | W ≤ W \le W≤ | 特殊性质 | 分数 | 依赖 |
|---|---|---|---|---|---|---|
| 1 | 10 10 10 | 10 10 10 | 5 5 5 | 10 10 10 | \ | |
| 2 | 1000 1000 1000 | 1000 1000 1000 | 5 5 5 | 20 20 20 | 子任务 1 | |
| 3 | A | 15 15 15 | \ | |||
| 4 | B | 25 25 25 | \ | |||
| 5 | 30 30 30 | 子任务 1、2、3、4 |
特殊性质 A:保证 d = 1 d=1 d=1。
特殊性质 B:令 c c c 表示仅在序列 A A A 中出现,而不在序列 B B B 中出现的数字总数。保证 c ≤ 5 c \le 5 c≤5。
扩展KMP 组合数学 滑动窗口
B中存在的数字成为关键数字,A只保留关键数字形成的数列是C。令B的长度是m。
性质一:A删除任意关键数字后,一定不包括B。故只需要考虑删除的是非关键数字。
令C[i]对应A[j],A删除若干数字后形成数列D,令D[k]对应A[j],如果D[k…k + m - 1]等于B,则其在A中的下标和C[i…i + m - 1]在A中的下标相同。因为关键数字删除后,不会相等。
如果C[i…i + m - 1]等于B,令j1是c[i + m - 1]在A中对应的下标,则d[i]记录A[j…j1]中非关键数字的数量,可能为0。为0时:A[j…j + m - 1]等于B。如果不能等于B,则d[i] = -1。
对于每个d[i]必须选择指定的d[i]个数,其它任意选择非关键数字,每种方案权重都 + 1。C[i…i + m - 1]是否和B相等,用z函数。z是B + C的z函数,z[i + m] >= m等效于C[i…i + m - 1]等于B。
f[i]记录C[i]在A[i]中的原始下标。利用滑动窗口求d[i]。随着滑动窗口的左边界增加,右边界一定增加。
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<long long MOD = 1000000007, class T1 = int, class T2 = long long>
class C1097Int
{
public:
C1097Int(T1 iData = 0) :m_iData(iData% MOD)
{
}
C1097Int(T2 llData) :m_iData(llData% MOD) {
}
C1097Int operator+(const C1097Int& o)const
{
return C1097Int(((T2)m_iData + o.m_iData) % MOD);
}
C1097Int& operator+=(const C1097Int& o)
{
m_iData = ((T2)m_iData + o.m_iData) % MOD;
return *this;
}
C1097Int& operator-=(const C1097Int& o)
{
m_iData = ((T2)MOD + m_iData - o.m_iData) % MOD;
return *this;
}
C1097Int operator-(const C1097Int& o)
{
return C1097Int(((T2)MOD + m_iData - o.m_iData) % MOD);
}
C1097Int operator*(const C1097Int& o)const
{
return((T2)m_iData * o.m_iData) % MOD;
}
C1097Int& operator*=(const C1097Int& o)
{
m_iData = ((T2)m_iData * o.m_iData) % MOD;
return *this;
}
C1097Int operator/(const C1097Int& o)const
{
return *this * o.PowNegative1();
}
C1097Int& operator/=(const C1097Int& o)
{
*this /= o.PowNegative1();
return *this;
}
bool operator==(const C1097Int& o)const
{
return m_iData == o.m_iData;
}
bool operator<(const C1097Int& o)const
{
return m_iData < o.m_iData;
}
C1097Int pow(T2 n)const
{
C1097Int iRet = (T1)1, iCur = *this;
while (n)
{
if (n & 1)
{
iRet *= iCur;
}
iCur *= iCur;
n >>= 1;
}
return iRet;
}
C1097Int PowNegative1()const
{
return pow(MOD - 2);
}
T1 ToInt()const
{
return ((T2)m_iData + MOD) % MOD;
}
private:
T1 m_iData = 0;;
};
template<class T >
class CFactorial
{
public:
CFactorial(int n) :m_res(n + 1) {
m_res[0] = 1;
for (int i = 1; i <= n; i++) {
m_res[i] = m_res[i - 1] * i;
}
}
T Com(int iSel, int iCanSel)const {
return m_res[iCanSel] / m_res[iSel] / m_res[iCanSel - iSel];
}
T Com(const vector<int>& cnt)const {
T biRet = 1;
int iCanSel = std::accumulate(cnt.begin(), cnt.end(), 0);
for (int j = 0; j < cnt.size(); j++) {
biRet *= Com(cnt[j], iCanSel);
iCanSel -= cnt[j];
}
return biRet;
}
vector<T> m_res;
};
template<class T>
class KMPEx
{
public:
static vector<int> ZFunction(const T* p, int n) {
vector<int> z(n);
z[0] = n;
for (int i = 1, left = 0, r = 0; i < n; ++i) {
if (i <= r) {//如果此if,r-i+1可能为负数
z[i] = min(z[i - left], r - i + 1);
}
while ((i + z[i] < n) && (p[z[i]] == p[i + z[i]])) {
z[i]++;
}
if (i + z[i] - 1 > r) left = i, r = i + z[i] - 1;
}
return z;//z[i] 表示S与其后缀S[i,n]的最长公共前缀(LCP)的长度
}
static vector<int> ZFunction(string s) {
return ZFunction(s.c_str(), s.length());
}
static int MinCyc(const string& str, int unit = 1) {
const int N = str.length();
auto z = ZFunction(str);
auto Is = [&](int k) {
for (int i = k; i < N; i <<= 1) {
if (z[i] < min(N - i, i)) { return false; }
}
return true;
};
for (int k = unit; k < N; k += unit) { if (Is(k))return k; }
return N;
}
};
template<class KEY>
class CKeyCount
{
public:
void Add(const KEY& key, int iCount)
{
Cnt[key] += iCount;
if (0 == Cnt[key])
{
Cnt.erase(key);
}
}
std::unordered_map<KEY, int> Cnt;
};
typedef C1097Int<> BI;
class Solution {
public:
int Ans(const int W, const int D, vector<int>& A, vector<int>& B) {
const int N2 = unordered_set<int>(B.begin(), B.end()).size();
const int M = B.size();
CFactorial<BI> fac(W - N2);
vector<bool> key(W);
for (const auto& i : B) { key[i] = true; }
vector<int> inxs, C;
for (int i = 0; i < A.size(); i++) {
if (key[A[i]]) { C.emplace_back(A[i]); inxs.emplace_back(i); }
}
auto BC = B;
BC.insert(BC.end(), C.begin(), C.end());
auto z = KMPEx<int>::ZFunction(BC.data(), BC.size());
CKeyCount<int> cnt;
BI ans;
for (int i = 0, left = 0, r = 0; i + M <= C.size(); i++) {
if (z[M + i] < M) { continue; }
while ((r <= inxs[i + M - 1])) {
cnt.Add(A[r], 1); r++;
}
while ((left < inxs[i])) {
cnt.Add(A[left], -1); left++;
}
const int sel = D - (cnt.Cnt.size() - N2);
const int can = W - N2 - (cnt.Cnt.size() - N2);
if (can < sel) { continue; }
if (sel < 0) { continue; }
ans += fac.Com(sel, can);
}
return ans.ToInt();
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
int T,N,M,W,D;
cin >> T;
for (int i = 0; i < T; i++)
{
cin >> N >> M >> W >> D;
auto A = Read<int>(N);
auto B = Read<int>(M);
#ifdef _DEBUG
printf("W=%d,D=%d,", W,D);
Out(A, "A=");
Out(B, "B=");
//Out(strs2, ",strs2=");
//Out(que, ",que=");
/*Out(que, "que=");*/
#endif // DEBUG
auto res = Solution().Ans(W,D,A,B);
cout << res << "\n";
}
return 0;
}
单元测试
int W, D;
vector<int> A, B;
TEST_METHOD(TestMethod1)
{
W = 3, D = 1, A = { 1,1,2,1 }, B = { 1,1 };
auto res = Solution().Ans(W, D, A, B);
AssertEx(3, res);
}
TEST_METHOD(TestMethod2)
{
W = 4, D = 1, A = { 1,2,3,1,2,3,1,2 },B = { 1,2,1 };
auto res = Solution().Ans(W,D,A,B);
AssertEx(2, res);
}
扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。
更多推荐
所有评论(0)