zoukankan      html  css  js  c++  java
  • HDU 3336 Count the string(KMP+DP)

    Count the string

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 52   Accepted Submission(s) : 27

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    Problem Description

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
    s: "abab"
    The prefixes are: "a", "ab", "aba", "abab"
    For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
    The answer may be very large, so output the answer mod 10007.

    Input

    The first line is a single integer T, indicating the number of test cases.
    For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.

    Output

    For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.

    Sample Input

    1
    4
    abab

    Sample Output

    6

    给你一个字符串,然后找出这个字符串的所有前缀,并计算它们在字符串中出现都次数,求次数和。
    在kmp中next数组的含义是:next[i]表示前i个字符所组成的字符串的最大前后缀匹配长度 。
    如:next[i]=j,则s[1....j]=s[i-j+1.....i]。
    dp【i】:以s[i]结尾的子串总共含前缀的数量
    dp[i]=dp[next[i]]+1;表示前i个字符中,最大前后缀在字符串中出现的次数+1(本身)。
     
    next[i]存的数表示第next[i]位之前的数是以i结尾的最大公共前后缀字符串。
    把所有前缀数量加起来就是所有前缀在字符串中出现都次数。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #define mod 10007;
     6 using namespace std;
     7 int n;
     8 char a[200005];
     9 int nxt[200005];
    10 long long dp[2000005]; 
    11 void get_nxt()
    12 {
    13     int i = 0, j = -1;
    14     nxt[0] = -1;
    15     while (i < n)
    16     {
    17         if (j == -1 || a[i] == a[j])
    18         {
    19             i++; j++;
    20             nxt[i] = j;
    21         }
    22         else j = nxt[j];
    23     }
    24 }
    25 int main()
    26 {
    27     int t;
    28     cin >> t;
    29     while (t--)
    30     {
    31         cin >> n;
    32         cin >> a;
    33         get_nxt();
    34         int i, j;
    35         long long s = 0;
    36         memset(dp, 0, sizeof(dp));
    37         /*for (i = 0; i <= n; i++) cout << nxt[i];
    38         cout << endl;*/
    39         for (i = 1; i <= n; i++)
    40         {
    41             dp[i] = dp[nxt[i]] + 1;
    42             s = (s + dp[i]) % mod;
    43         }
    44         cout << s << endl;
    45     }
    46     return 0;
    47 }
      
  • 相关阅读:
    Navicat在MySQL中添加外键详细过程
    java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法
    记录springboot jar包冲突异常处理
    终止线程
    SpringDataJPA
    mysql安装后无法启动问题
    地理空间几种数据格式
    图幅与经纬度之间的换算
    postgresql12集成postgis与timescale
    C# 微信支付 V2
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271220.html
Copyright © 2011-2022 走看看