【题解】P10976 统计重复个数 | 非倍增优化 DP 的做法

· · 题解

update 26.3.15: 修正 hack 数据

这道题目已有的题解中全部都是用倍增优化 DP 做的。但我觉得有个更自然的思路是,考虑循环节。

由于我的语言表述能力实在太差,本篇文章使用了 Deepseek 辅助完成。

为方便后文描述,我们定义匹配:如果 B 可以从 A 获得,则称 A 可以匹配出 B。

核心思路

首先考虑暴力,显然可以用贪心的思想用 O(|str1|) 的复杂度计算出 m:

int m=0,p=0;
for(int i=0;i<str1.size();i++){
    if(str1[i]==str2[p]){
        if(p+1==s2.size())m++,p=0;
        else p++;
    }
}

但 str_1 和 str_2 太长了,复杂度过不了。

因为 str_2=[s_2,n_2],自然我们要考虑一下 s_2 和 str_1 与 m 有什么关系,不难发现:设 M 为最大的使得 [s_2,M] 可以从 str_1 获得的整数,则 m = \left\lfloor \frac{M}{n_2} \right\rfloor。

于是我们将问题转换为:在 [s_1, n_1] 中最多能匹配出多少个 s_2。

``` str1: cbacab cbacab cbacab cbacab cbacab cbacab s2: a b c a b c a b c a b c a b c ``` 于是我就开始考虑循环,然后得出一个结论:如果在 $[s_1,\infty]$ 中匹配 $s_2$,则匹配过程会形成一个**循环**。证明如下: 每次匹配完一个 $s_2$ 后,我们都会在对应的 $s_1$ 的某个位置结束。由于 $s_1$ 长度有限,这些位置会重复出现。一旦位置重复,后续的匹配模式就会重复。 --- 于是我们可以按照如下步骤暴力寻找循环节: 1. 定义一个指针 $p$,初始指向 $s_1$ 的末尾 2. 每次完整匹配一个 $s_2$,记录: - 当前在 $s_1$ 中的位置 $p
  1. 当发现某个位置 p 再次出现时,说明找到了循环

我已经尽力表达了……如果不理解的话可以看代码:

//需要先判断 s2 中的每个字符是否在 s1 中出现
//若否,则显然 m=0
using tp=tuple<int,int>;
int l1=s1.size(),l2=s2.size();
vector<tp>a(l1);//记录每个位置 p 第一次作为结尾时的 t1 和 t2
int p=l1-1,t1=0,t2=0;
while(1){
    t2++;
    for(int i=0;i<l2;i++){//O(|s2|)遍历
        if(p+1==l1)p=0,t1++;else p++;
        while(s1[p]!=s2[i]){
            if(p+1==l1)p=0,t1++;else p++;
        }//O(|s1|)找到第一个 s2[i],为了代码直观这里没有用预处理优化
    }
    if(a[p]==tp{0,0})a[p]={t1,t2};
    else break;//如果这个位置之前已经作为结尾过了,说明找到循环
}

显然除最后一次循环外,每次最外层的 while 循环时 a 数组一定会有一个没被赋值过的位置被赋值,当遇到被赋值的位置时退出循环。因此 while 循环最多执行 |s_1| 遍。

这个没有优化的代码复杂度为 O(|s_1|^2|s_2|)。

找到循环后,我们需要知道:

用以下样例举例:

abc 1
cab 4

显然我们会这样比配:

str1: cab cab cab cab
  s2:  ab cab cab c

而不是:

str1: cab cab cab cab
  s2:  ab c    ab c

通过一坨代码可以计算出这些。

有了循环参数,我们可以把 n_1 个周期分成两部分:

  1. 整周期部分:前 \lfloor \frac{n_1-y}{T_1-y}\rfloor 个周期
  2. 余数部分:最后剩下的 (n_1 - y) \bmod (T_1 - y)+y 个 s_1

得到总匹配数:

M = \underbrace{\lfloor \frac{n_1 - y}{T_1 - y} \rfloor \times T_2}_{\text{整周期部分}} + \underbrace{\max_{a_i.t_1 \leq (n_1 - y) \bmod (T_1 - y)+y} a_i.t_2}_{\text{余数部分}}

正确性证明

循环存在性证明:

公式正确性证明:

复杂度分析

代码

懒得写预处理优化了。

#include<bits/stdc++.h>
using namespace std;
#define dbg(x) cerr<<#x<<':'<<(x)<<' '
#define dbe(x) cerr<<#x<<':'<<(x)<<endl
#define eb emplace_back
#define ep emplace
#define endl '\n'
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
using tp=tuple<int,int>;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    string s1,s2;
    int n1,n2;
    while(cin>>s2>>n2>>s1>>n1){
        int l1=s1.size(),l2=s2.size();

        vi bt(26);
        for(auto c:s1)bt[c-'a']=1;
        bool sf=true;
        for(auto c:s2)if(bt[c-'a']==0){sf=false;break;}
        if(!sf){cout<<0<<endl;continue;}

        vector<tp>a(l1);
        int p=l1-1,t1=0,t2=0,sc_p=-1;
        while(1){
            sc_p=p,t2++;
            for(int i=0;i<l2;i++){
                if(p+1==l1)p=0,t1++;else p++;
                while(s1[p]!=s2[i]){
                    if(p+1==l1)p=0,t1++;else p++;
                }
            }
            if(a[p]==tp{0,0})a[p]={t1,t2};
            else break;
        }
        tie(t1,t2)=a[sc_p];
        p=sc_p+1;
        while(t2--){
            for(int i=l2-1;i>=0;i--){
                if(p-1==-1)p=l1-1,t1--;else p--;
                while(s1[p]!=s2[i]){
                    if(p-1==-1)p=l1-1,t1--;else p--;
                }
            }
        }
        tie(t1,t2)=a[sc_p];
        bool y=(sc_p<p);
        ll M=(ll)(n1-y)/(t1-y)*t2,sx1=(n1-y)%(t1-y)+y,sx2=0;
        for(auto [t1,t2]:a){
            if(t1!=0&&t1<=sx1&&t2>sx2)sx2=t2;
        }
        M+=sx2;
        cout<<M/n2<<endl;
    }
    return 0;
}

hack 数据

这个思路细节有点多,提供一组 hack 数据:

in:

abc 3
abcabcab 4
abd 3
abcabcab 4
aa 10
aaaa 4
cca 2
aac 10
cbbc 5
bbcabc 6
de 1
edededd 7

ans:

2
0
0
2
1
20

对拍好闪,拜谢对拍。

反正我把这些数据都调对了就 AC 了。