kmp(前缀出现次数next应用)
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
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.
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.
4
abab
#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应用)的更多相关文章
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- HDU4300-Clairewd’s message(KMP前缀匹配后缀)
Clairewd's message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ 3167 Cow Patterns (KMP+前缀和)
题意:给你两串数字,长度分别为n和m,数字大小在[1,25].当后一串数字每个数字的排名位置与前一串数字(任一长度为m的子串)每个数字的排名位置一致时就完全匹配,最后求哪些位置是完全匹配的. 例如:1 ...
- hdu 1251 统计难题 前缀出现次数
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hiho1015(kmp+统计出现次数)
http://hihocoder.com/problemset/problem/1015 时隔多天再次温习了一下KMP #include <iostream> #include <c ...
- 题解报告: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 ...
- hdu1671 字典树记录前缀出现次数
题意: 给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...
- POJ2406 KMP前缀周期
题意: 给你一个字符串,长度小于1百万,问你他最多可以拆成集合相同字符串,例如abcabcabc 可以拆成3个abc,所以输出3. 思路: 这个是比较常规的next应用,首先假 ...
- CF 246 div2 D Prefixes and Suffixes (全部前缀的出现次数)
题目链接:http://codeforces.com/contest/432/problem/D 题意:对一个长度不超过10^5的字符串.按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现 ...
随机推荐
- js中的函数声明置顶
函数声明置顶是指 js引擎在读取变量与声明式函数时,会优先读取,例如如下 var a = 1: function a(){}; console.log(a); //这里得到的为1,而不是该functi ...
- Maven将jar包放入本地库
转自:https://blog.csdn.net/qq_33314107/article/details/73549256 这是由于Oracle授权问题,Maven3不提供Oracle JDBC dr ...
- 基于socket实现大文件上传
import socket 1.客户端: 操作流程: 先拿到文件--->获取文件大小---->创建字典 1.制作表头 header 如何得到 他是一个二进制字符串 序列化得到 字典字符串 ...
- a标签实现下载canvas图片
令 a 的 href = canvas.toDataURL("image/png");
- CentOS7系统局域网内配置本地yum源解决cannot find a valid baseurl for repo
一. 问题详情 因为服务器无法连接外网,所有直接用yum安装某些功能将受到影响,报错如下: Error: Cannot find a valid baseurl for repo: base ...
- CSS3 flexbox弹性布局实例
常用例子 1.居中对齐 <!DOCTYPE html> <head> <meta charset="utf-8"> <style type ...
- flask之显示当地时间
一:在网页上显示时间 flask-moment 程序扩展可以实现 pip install flask-moment # 未完待续
- HDU 1373 XYZZY (spfa的特殊用法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意:有n个房间,编号1-n,房间之间有单向门连接.某人初始位于1号房间,且具有100点能量 ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Knative 实战:基于 Knative Serverless 技术实现天气服务
提到天气预报服务,我们第一反应是很简单的一个服务啊,目前网上有大把的天气预报 API 可以直接使用,有必要去使用 Knative 搞一套吗?杀鸡用牛刀?先不要着急,我们先看一下实际的几个场景需求: 场 ...