[Bzoj 3620] seems to have seen in the Dream "KMP"

Source: Internet
Author: User

Title Link: BZOJ-3620

Problem analysis

This problem can be done by using KMP to do O (n^2) violence.

First, we enumerate the left endpoint L of the string in turn, and then start the KMP from the left end.

Then we enumerate right endpoint R, and the right endpoint of the qualifying R is s[l. R] This section has the same prefix and suffix, and this part of the length x to meet K <= x < (r-l + 1)/2.

We'll use the Next array of KMP to find this prefix, and the worst case will be turned into O (n^3) directly from next[r], so we should optimize it:

If the Next array is treated as an Father array, then it is a tree. We are asking for a node to the root of the path there is no interval [k, (R-L+1)/2) number.

We ask Next for the leaves from the root, so we can simultaneously maintain the root to a node J of all the numbers, greater than or equal to the smallest of the number of K.

Then we are for each right endpoint, as long as the point to the root of the path is greater than or equal to the smallest number of k is less than (r-l + 1)/2 on it.

Code
#include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> #include <cstring > #include <cstring>using namespace std;const int maxn = 15000 + 5, INF = 999999999;inline int gmin (int a, int b) {return a < b? A:b;} int L, K, Ans;int NEXT[MAXN], Num[maxn];char s[maxn];int main () {scanf ("%s", S + 1), L = strlen (s + 1); scanf ("%d", &k); A NS = 0;for (int i = 1; I <= l; ++i) {memset (next, 0, sizeof (next)); int j = 0; NEXT[1] = 0; Num[0] = inf;for (int p = 2; p <= l-i + 1; ++p) {while (J > 0 && s[i + j + 1-1]! = s[i + p-1]) j = Next [J];if (s[i + j + 1-1] = = S[i + p-1]) ++j; NEXT[P] = J;if (J < k) Num[j] = Inf;else Num[j] = Gmin (J, Num[next[j]]); if ((Num[j] << 1) < p) ++ans;  }} printf ("%d\n", Ans); return 0;}

  

[Bzoj 3620] seems to have seen in the Dream "KMP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.