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;
    }
  • 相关阅读:
    arcgis连接oracle发布服务,提示数据未注册到服务器,手动注册服务器失败
    安装arcgis server时提示“应用程序无法启动,因为应用程序......或使用命令行sxstrace.exe”
    创建自定义地理(坐标)变换
    坐标系转换方式
    ArcSDE数据库、文件地理数据库和个人地理数据库的区别
    4D
    Oracle 11g中创建实例
    Oracle 10g客户端的安装和配置
    Oracle 11g服务端的安装和配置
    类装载器ClassLoader
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4454583.html
Copyright © 2011-2022 走看看