• 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 }
      
  • 相关阅读:
    js弹出框、对话框、提示框、弹窗总结
    GIT本地免密配置
    SpringMVC 框架的搭建及基本功能的实现
    win10调用局域网内xp系统上的打印机
    JSP页面之间互相传值
    Spring MVC 框架的架包分析,功能作用,优点
    hibernate_Restrictions用法
    服务器中配置多个Tomcat及内存溢出配置
    破解navicat
    如何重装mysql8及初次修改密码
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271220.html
Copyright © 2020-2023  润新知