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;
    }
  • 相关阅读:
    UWP 统一平台开发介绍
    异步编程之Javascript Promises 规范介绍
    Winform文件下载之WebClient
    微软 Build 2016年开发者大会发布多项功能升级
    绝对干货:自定义msi安装包的执行过程
    人机大战之AlphaGo的硬件配置和算法研究
    .Net 高效开发之不可错过的实用工具
    ComponentOne 2016 年产品规划
    三天学会HTML5 ——多媒体元素的使用
    三天学会HTML5——SVG和Canvas的使用
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4454583.html
Copyright © 2011-2022 走看看