Codeforces 432d| Prefixes and suffixes| kmp| Dynamic Programming __codeforces

Source: Internet
Author: User
The main effect of the topic

The number of the substring of S-prefix and S-suffix for different lengths

Consider the KMP next array.
Because Next[j] represents the LCP (S[1..J],S[1..K]) (K<J) LCP (S[1..J],S[1..K)) (K.
So S[1..J] and S[1..next[j] "the relationship between the latter is the longest suffix of the former, because the longest, so certainly will not leak, so the latter is the number of occurrences of the former +1 (the former last occurrence of the suffix) ...
So if you start from S[1..N], then s[1..next[n] must be the prefix of the string, S[1..next[next[n] must be s[1..next[n], and it must be the prefix of the string, and so on, and then the DP equation will be OK.
Dp[next[i]] + = Dp[i]
Finally, there are dp[i]+1 to show that the last time.

#include <cstdio>
#include <cstring>
const int N = 100005;
Char P[n];
int next[n], dp[n], len[n];

int main () {
    int i, j, n, tot = 0;
    scanf ("%s", p + 1);
    n = strlen (p + 1);
    for (i = 0, j = 2; J <= N; ++j) {While
        (i && p[j]!= p[i + 1]) i = next[i];
        if (p[i + 1] = = P[j]) ++i;
        NEXT[J] = i;
    }
    for (i = n; i = Next[i]) Len[++tot] = i;
    for (i = n; i; i) dp[next[i]] + + = ++dp[i];
    printf ("%d\n", tot);
    for (i = tot i; i) printf ("%d%d\n", Len[i], dp[len[i]);

    return 0;
}
D. Prefixes and suffixes Description

You have a string s = s1s2...s|s|, where |s| Is the length of a string s, and si its i-th character.
Let ' s introduce several definitions:
A substring s[i. J] (1≤i≤j≤|s|) of string s is String Sisi + 1...sj.
The prefix of string s of length L (1≤l≤|s|) is string S[1..L].
The suffix of string s of length L (1≤l≤|s|) is string s[|s|-L + 1..| S|].
Your task is, for any prefix of string s which matches a suffix of-s, print the number of times it occurs in string s as a substring. Input

The single line contains a sequence of characters s1s2...s|s| (1≤|s|≤105)-string S. The string only consists of uppercase 中文版 letters. Output

In the ' The ', print integer k (0≤k≤|s|)-the number of prefixes that match a suffix of string s. Next print k lines, in the each line print two integers Li Ci. Numbers Li Ci mean that the prefix's length li matches the suffix of length Li and occurs in string s as a substring CI times. Print pairs Li Ci in the order of increasing li. examples Input

Abacaba Output

3
1 4
3 2
7 1 Input

AAA Output

3
1 3
2 2
3 1

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.