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;
    }
  • 相关阅读:
    【创建型】Singleton模式
    【创建型】Abstract Factory模式 & Factory Method模式
    红黑树插入操作
    红黑树删除操作
    bat命令中的变量声明及使用
    bat(传参情况下)取得当前bat所在的目录路径
    代理模式 -- 大话设计模式
    装饰模式 -- 大话设计模式
    依赖倒转原则 -- 大话设计模式
    开放封闭原则 -- 大话设计模式
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4454583.html
Copyright © 2011-2022 走看看