HDU3336 Count the string(kmp
时间:2019-10-19
本文章向大家介绍HDU3336 Count the string(kmp,主要包括HDU3336 Count the string(kmp使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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.
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.
InputThe 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.
OutputFor 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
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 const int maxn=1e6+5; 6 const int mod = 1e4+7; 7 int net[maxn],len; 8 char c[maxn]; 9 void getnext(){ 10 int l = strlen(c); 11 int i = 0,j = -1; 12 net[0]=-1; 13 while(i<l){ 14 if(j==-1||c[i]==c[j]) net[++i]=++j; 15 else j = net[j]; 16 } 17 } 18 int main() 19 { 20 int t; 21 cin>>t; 22 while(t--){ 23 cin>>len>>c; 24 getnext(); 25 int l = strlen(c),res = 0; 26 for(int i = 0;i < l;++i){ 27 if(net[i]!=0&&net[i]+1!=net[i+1])res += net[i]; 28 } 29 cout<<(res+l+net[l])%mod<<endl; 30 } 31 return 0; 32 }
原文地址:https://www.cnblogs.com/h404nofound/p/11704057.html
- 谷歌开放的TensorFlow Object Detection API 效果如何?对业界有什么影响?
- 生信编程直播课程优秀学员作业展示1
- ID转换大全
- 走在专家的路上,每天一条SQL优化(3)
- 表现层设计模式
- MongoDB模拟多文档事务操作
- 表达矩阵可视化大全
- .NET MongoDB Driver GridFS 2.2原理及使用示例
- .NET MongoDB Driver 2.2使用示例
- 设计模式(行为模式)——观察者模式
- 机器学习实战 | 第二章:线性回归模型
- C#要点补充
- 【直播】我的基因组63:wegene芯片跟二代测序的简单比较
- .Net多线程编程—System.Threading.Tasks.Parallel
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 小生归一(六)xss特殊绕过
- 最新绕过D盾注入方法分享(学姿势)
- 利用C#编写的绕过360安全卫士添加系统用户
- 小生归一(七)sprintf字符串格式化漏洞
- FastAdmin后台GetShell
- scRNA-seq Clustering
- Dizzy Blog
- LeetCode 392. 判断子序列
- LeetCode 5473. 灯泡开关 IV
- LeetCode 5472. 重新排列字符串
- BootStrap 导航栏实现下滑消失,上滑出现
- 七日Python之路--第六天
- 超性感的React Hooks(六)自定义hooks的思维方式
- 七日Python之路--第五天(之找点乐子)
- 超性感的React Hooks(七)useReducer