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;
    }
  • 相关阅读:
    初识 Umbraco CQ
    程序员的利器SourceInsight CQ
    关于Hg的文件过滤 CQ
    蓝桥杯 基本内容
    leedswriting符号
    tiny mission 2021 11 15
    拓扑排序+二分答案+建图
    mission 11.24
    高数积分求面积
    高数积分求弧长
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4454583.html
Copyright © 2011-2022 走看看