zoukankan      html  css  js  c++  java
  • hdu 3336 Count the string -KMP&dp

      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,那么来回想一下fail(next)数组的意义——最长相同的前缀和后缀。对于长度为i的前缀,那么f[i]中包含两个长度为f[i]的前缀(大概就这个意思),用cnt[i]表示1-i这一段中有多少个子串与长度为f[i],f[f[i]]的前缀相同(除去cnt[f[i]]这一部分),于是可以得出

      cnt[i] = cnt[f[i]] + 1

      最后把所有cnt求个和就是最终答案

    Code

     1 /**
     2  * hdu
     3  * Problem#3336
     4  * Accepted
     5  * Time:46ms
     6  * Memory:3316k
     7  */
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<cctype>
    11 #include<cstring>
    12 #include<cstdlib>
    13 #include<fstream>
    14 #include<sstream>
    15 #include<algorithm>
    16 #include<map>
    17 #include<ctime>
    18 #include<set>
    19 #include<queue>
    20 #include<vector>
    21 #include<stack>
    22 using namespace std;
    23 typedef bool boolean;
    24 #define INF 0xfffffff
    25 #define smin(a, b) a = min(a, b)
    26 #define smax(a, b) a = max(a, b)
    27 #ifndef WIN32
    28 #define AUTO "%lld"
    29 #else
    30 #define AUTO "%I64d"
    31 #endif
    32 template<typename T>
    33 inline void readInteger(T& u){
    34     char x;
    35     int aFlag = 1;
    36     while(!isdigit((x = getchar())) && x != '-');
    37     if(x == '-'){
    38         x = getchar();
    39         aFlag = -1;
    40     }
    41     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
    42     ungetc(x, stdin);
    43     u *= aFlag;
    44 }
    45 
    46 int kase;
    47 int n;
    48 char s[200005];
    49 int f[200005];
    50 int cnt[200005];
    51 
    52 inline void init() {
    53     readInteger(n);
    54     gets(s);
    55     gets(s);
    56 }
    57 
    58 inline void solve() {
    59     f[0] = f[1] = 0;
    60     int j;
    61     for(int i = 1; i < n; i++) {
    62         j = f[i];
    63         while(j > 0 && s[i] != s[j])    j = f[j];
    64         f[i + 1] = (s[i] == s[j]) ? (j + 1) : (0);
    65     }
    66     int res = 0;
    67     for(int i = 1; i <= n; i++)
    68         (cnt[i] = cnt[f[i]] + 1) %= 10007, (res += cnt[i]) %= 10007;
    69     printf("%d
    ", res);
    70 }
    71 
    72 int main() {
    73     readInteger(kase);
    74     while(kase--) {
    75         init();
    76         solve();
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    原生js操作Dom命令总结
    JSON语法、对象、遍历数组的区别和基本操作
    JavaScript数组方法
    JavaScript基本语法
    JavaScript
    CSS外边框、边界样式常用组合
    HTML特殊符号(字符实体)大全
    CSS设置边框、符号、背景样式、链接属性
    CSS样式链接和文字常用属性
    k8s kubernetes给node节点添加标签和删除node节点标签
  • 原文地址:https://www.cnblogs.com/yyf0309/p/6379890.html
Copyright © 2011-2022 走看看