bzoj3620 似乎在梦中见过的样子

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3620

【题解】

这个n<=15000给人一个O(n^2)能过的感觉(事实就是这样)

我们先学一发kmp吧(这博客还没有kmp教程药丸)

这个kmp非常excited的地方是什么呢?

有一个nex[i]数组,他能告诉你匹配串到i,但是i+1失败的时候要跳到哪里去。

这个怎么处理呢?把匹配串和自己匹配啊

好了我们讲完kmp啦!

这题跟kmp有啥关系呢?

我们发现kmp的nex数组讲的就是ABA的事情跟这题怎么这么像啊

那么我们枚举起点l

对于s[l...n]求nex数组

当然这个nex只要一直nex递归下去,存在一个nex[j']=p,其中p*2<len即可。当然我们把p<k的先排除掉。

这个可以用一个数组ok[i]=j记录nex值为i的最小位置,然后直接做即可。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 15000 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

char str[M];
int m, n, ans = 0; 
int nex[M], ok[M]; 

int main() {
    scanf("%s", str+1); 
    n = strlen(str+1);
    cin >> m;
    for (int i=1; i<=n; ++i) {
         memset(nex, 0, sizeof nex);
        int j = 0; 
        nex[1] = 0; ok[0] = 1e9; 
        for (int k=2; k<=n-i+1; ++k) {
            while(j && str[i-1+j+1] != str[i-1+k]) j = nex[j];
            if(str[i-1+j+1] == str[i-1+k]) ++j; 
            nex[k] = j;
            if(j < m) ok[j] = 1e9;
            else ok[j] = min(j, ok[nex[j]]);
            if((ok[j] << 1) < k) ++ans;
        }
    }
    cout << ans << endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/galaxies/p/bzoj3620.html