zoukankan      html  css  js  c++  java
  • HDU——T 3336 Count the string

    http://acm.hdu.edu.cn/showproblem.php?pid=3336

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10917    Accepted Submission(s): 5083


    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
     
    Author
    foreverlin@HNU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1686 3746 1358 3341 2222 
     
    题意:求出每个前缀在串中出现的次数
    每次统计答案时 增加 此串next[i]位置的次数+1即  f[i]=f[next[i]]+1,ans+=f[i]
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int N(200000+5);
     8 const int mod(10007);
     9 int l,ans,p[N],f[N];
    10 char s[N];
    11 
    12 inline void Get_next()
    13 {
    14     for(int i=2,j=0;i<=l;i++)
    15     {
    16         for(;s[j+1]!=s[i]&&j>0;) j=p[j];
    17         if(s[i]==s[j+1]) j++;
    18         p[i]=j;
    19     }
    20 }
    21 
    22 inline void init()
    23 {
    24     ans=0;
    25     memset(p,0,sizeof(p));
    26     memset(f,0,sizeof(f));
    27 }
    28 
    29 int main()
    30 {
    31     int t; scanf("%d",&t);
    32     for(;t--;init())
    33     {
    34         scanf("%d%s",&l,s+1);
    35         Get_next();
    36         for(int i=1;i<=l;i++)
    37         {
    38             f[i]=(f[p[i]]+1)%mod;
    39             ans=(ans+f[i])%mod;
    40         }
    41         printf("%d
    ",ans);
    42     }
    43     return 0;
    44 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    全站导航
    常用模块
    模块的引用的路径的查找
    类的魔术方法
    包装和授权
    类内置的attr属性
    反射
    三大特性之多态
    三大特性之封装
    python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7381687.html
Copyright © 2011-2022 走看看