zoukankan      html  css  js  c++  java
  • HDU-3336

    Count the string

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


    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
     
    /**
              题意:给一个字符串,问字符串前缀的个数
              做法:kmp 每次next[]数组记录的下标  前缀数组
    **/
    #include <iostream>
    #include <stdio.h>
    #include <cmath>
    #include <algorithm>
    #include <string.h>
    #define maxn 200000 + 10
    #define mod 10007
    using namespace std;
    char ch[maxn];
    int mmap[maxn];
    int Next[maxn];
    void getnext(char *p)
    {
              int len = strlen(p);
              int j,k;
              j=0;
              k = -1;
              Next[0] = -1;
              while(j < len)
              {
                        if(k == -1 || p[j] == p[k])
                        {
                                  j++;
                                  k++;
                                  Next[j] = k;
                        }
                        else k = Next[k];
              }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
              freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
              int T;
              scanf("%d",&T);
              while(T--)
              {
                        int n;
                        scanf("%d",&n);
                        scanf("%s",ch);
                        getnext(ch);
                        mmap[0] = 0;
                        int res= 0 ;
                        for(int i=1;i<=n;i++)
                        {
                                  mmap[i] = mmap[Next[i]] + 1;
                                  mmap[i] %= mod;
                                  res +=  mmap[i];
                                  res %= mod;
                        }
                        printf("%d
    ",res);
              }
              return 0;
    }
  • 相关阅读:
    shell 函数
    使用Alpine镜像构建镜像
    macos修改vmware Fusion的NAT网络
    K8s Pod与宿主机时区不同步
    nginx热升级
    awk分析web日志
    k8s 新建用户远程连接集群和context切换
    查询出口公网ip
    微服务之服务网格 Istio
    Systemd 、systemctl进程管理工具
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4454583.html
Copyright © 2011-2022 走看看