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 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    鸟哥私房菜*基础篇(3)
    Java 基于Graphics2D绘制电子收据图片
    微信扫码支付沙盒测试,解决沙盒环境下签名验证失败
    JAVA对象合集,根据条件过滤
    java对象根据字段进行排序
    vue 弹窗调用父窗口函数
    php使用post功能,调用微信推送服务
    JS字符串截取
    关于两个 IQueryable 合并
    bootstrap清除数据源
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7381687.html
Copyright © 2011-2022 走看看