http://acm.hdu.edu.cn/showproblem.php?pid=3336

Count the string

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

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
 
Recommend
lcy
题意:求字符串所有前缀出现的次数(包括本身)
思路:求所有next的值的个数加上前缀本身
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[]; int num; void getnext(char* a, int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[j] == a[k])
{
k++;
j++;
// if(a[j] != a[k])
next[j] = k ;
// else
// {
// next[j] = next[k];
// } }
else
{
k = next[k];
}
}
} int main()
{int n ;
scanf("%d" , &n);
while(n--)
{
int next[];
int l ;
scanf("%d" , &l);
scanf("%s" , a);
getnext(a , l , next);
int j = ;
for(int i = ; i <= l ; i++)
{
// cout << next[i] << " " ;
j = i ;
while(next[j] > )
{
num = (num + ) % ;
j = next[j];
}
}
// cout << endl ; printf("%d\n" , (num + l)%);
num = ;
} return ;
}

kmp(前缀出现次数next应用)的更多相关文章

  1. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  2. HDU4300-Clairewd’s message(KMP前缀匹配后缀)

    Clairewd's message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. POJ 3167 Cow Patterns (KMP+前缀和)

    题意:给你两串数字,长度分别为n和m,数字大小在[1,25].当后一串数字每个数字的排名位置与前一串数字(任一长度为m的子串)每个数字的排名位置一致时就完全匹配,最后求哪些位置是完全匹配的. 例如:1 ...

  4. hdu 1251 统计难题 前缀出现次数

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  5. hiho1015(kmp+统计出现次数)

    http://hihocoder.com/problemset/problem/1015 时隔多天再次温习了一下KMP #include <iostream> #include <c ...

  6. 题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  7. hdu1671 字典树记录前缀出现次数

    题意:       给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...

  8. POJ2406 KMP前缀周期

    题意:       给你一个字符串,长度小于1百万,问你他最多可以拆成集合相同字符串,例如abcabcabc 可以拆成3个abc,所以输出3. 思路:       这个是比较常规的next应用,首先假 ...

  9. CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)

    题目链接:http://codeforces.com/contest/432/problem/D 题意:对一个长度不超过10^5的字符串.按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现 ...

随机推荐

  1. 同步请求与异步请求Json

    同步请求的返回值类型 : void : 啥也不返回 String :表示逻辑视图名 ModelAndView:该对象既有逻辑视图名,还可以携带去页面要展示的数据 同步请求:如何将controller层 ...

  2. 一、JQJson数组

    叙述:常用的数据格式无非三种(组装数据,传参传值) 一.数组 : 1.定义 var select = []; //或 var select = new Array(); 2.JS给一个数组赋值 sel ...

  3. fiddler 手机抓包,CS端抓包 使用记录

    1.允许远程连接 2.忽略https证书错误 3.设置代理 4.重启fiddle 5.PC客户端抓包分工具FIddler+Proxifer https://blog.csdn.net/sunbo_cs ...

  4. django之请求和响应

    一:请求 1. 浏览器向服务器传参的几种方式: 方式一:通过正则的形式进行传递,名字为未命名模式 路由的正则形式: from django.conf.urls import url from . im ...

  5. 爬虫技术:数据处理josn和pickle模块

    一:json模块 json模块的作用就是讲json字符串("{"a":1,"b":1}")和python能够识别的字典进行相互转换. imp ...

  6. java基础复习(四)

    ---恢复内容开始--- 一.for循环的掌握  语法格式: for( 初始化语句A   ;  条件判断B  ;  循环后功能语句C){ //循环体D } for的执行流程:   整个for循环结构中 ...

  7. frugally-deep: Header-only library for using Keras models in C++

    // Convenience wrapper around predict for models with // single tensor outputs of shape (1, 1, 1), / ...

  8. 用C语言编程乘法口诀表

    首先是全部,代码如下: #include<stdio.h> void main() { int i,j; ;i<=;i++) { ;j<=;j++) printf(" ...

  9. OPTIONS请求后台处理 跨域Filter

    import cn.hutool.http.Method; import org.springframework.web.filter.OncePerRequestFilter; import jav ...

  10. ueditor 复制word里面带图文的文章,图片可以直接显示

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...