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的字符串.按长度输出和后缀全然匹配的的前缀的长度,及该前缀在整个串中出现 ...
随机推荐
- 2018团队项目beta阶段成果汇总
2018团队项目beta阶段成果汇总 第一组:二手书 团队博客:http://www.cnblogs.com/DeltaFish/ 博客汇总:https://www.cnblogs.com/Del ...
- 如何同步发送put或者delete请求
1.必须把前端发送方式改为post . 2.在web.xml中配置一个filter:HiddenHttpMethodFilter过滤器 3.必须携带一个键值对,key=_method, value= ...
- tf.concat,连接矩阵
tf.concat(concat_dim, values, name='concat') concat_dim需要连接的矩阵的维度, values需要连接的两个矩阵. a=[[1,2,3],[7,8, ...
- Asp.Net 保存Session的三种方式
一.默认方式,保存在IIS进程中保存在IIS进程中是指把Session数据保存在IIS的运行的进程中,也就是inetinfo.exe这个进程中,这也是默认的Session的存方式,也是最常用的. 这种 ...
- 外媒:Apple面临着印度iPhone的停用
据外媒报道:印度电信管理局(TRAI)制定遏制令人讨厌的消息的新规则可能导致印度数百万部iPhone的停用. 该监管机构本周早些时候宣布了“2018年电信商业通信客户偏好规定”,根据该规定,它已提议“ ...
- 使用JavaBean对象存储表格数据
范例:表格内容接上篇 package cn.sxt.collection; import java.util.ArrayList;import java.util.Date;import java.u ...
- 【leetcode】1047. Remove All Adjacent Duplicates In String
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- Oracle RAC运维所遇问题记录二
oracle12c RAC源端与Dataguard目标端实时同步,因业务需求需要在源端增加PDB 1. 源端添加PDB CREATE PLUGGABLE DATABASE kdlxpdb admin ...
- re正则常用示例积累
2019-12-7 import re ''' 示例1: 提取网站的网址 ''' urls = ['https://blog.csdn.net/xxcupid/article/details/5199 ...
- NotSerializableException
这个错误是实体类没有 继承 Serializeble.