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的字符串.按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现 ...
随机推荐
- Python之路-Python中文件和异常
一.文件的操作 open函数 在python中,使用open函数,打开一个已经存在的文件,或者新建一个新文件. 函数语法 open(name[, mode[, buffering[,encoding] ...
- 调用SM30数据表维护的函数
相关文章:http://www.cnblogs.com/caizjian/p/3248499.html 1.se11进去新建一个数据表 2.se55进去生产表维护 3.sm30进去维护数据表 4.se ...
- Robot Framework 源码阅读 day1 run.py
robot里面run起来的接口主要有两类 run_cli def run_cli(arguments): """Command line execution entry ...
- linux grep 设置高亮显示
[root@eric ~]# vi /etc/profile alias grep='grep --color=auto' [root@eric ~]# source /etc/profile
- 在虚拟机Linux中安装VMTools遇到的问题-小结
总结: 遇到的问题:No support for locale: zh_CN.utf8 可能的解决方法:1.sudo dpkg-reconfigure locale (重新配置?) 2.上一步失败,提 ...
- wireshare文件格式
你用Wireshark打开这个pkt试试,如果可以打开,就说明Wireshark支持这种格式.然后你就可以去看Wireshark的源码.*.pkt是omnipeek/etherpeek的默认文件格式, ...
- Java 根据银行卡号获取银行名称以及图标
转 https://blog.csdn.net/N_007/article/details/78835526 参考 CNBankCard 中国各大银行卡号查询 一.支付宝接口获取名称 根据 卡号 获取 ...
- git 常用命令记录
删除远程分支 git push origin --delete 远程分支名 删除本地分支 git branch -d 本地分支名 从master新建分支 git checkout -b 新分支名 建立 ...
- php in_array()函数 语法
php in_array()函数 语法 作用:搜索数组中是否存在指定的值.大理石机械构件价格 语法:in_array(search,array,type) 参数: 参数 描述 search 必需.规定 ...
- Quick Notes
激励 每当在书中读及那些卑微的努力,都觉得感动且受震撼.也许每个人在发出属于自己的光芒之前,都经历了无数的煎熬,漫长的黑夜,无尽的孤独,甚至不断的嘲讽和否定,但好在那些踮脚的少年,最后都得到了自己想要 ...