HDUOJ------3336 Count the string(kmp)
D - Count the string
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
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
求解前缀数组的数目:
代码:
1 #include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 using namespace std;
5 const int maxn= 200050;
6 char st[maxn];
7 int next[maxn];
8 int dp[maxn];
9 int main()
10 {
11 int test,lenb;
12 scanf("%d",&test);
13 while(test--)
14 {
15 scanf("%d %s",&lenb,st);
16 int i=0,j=-1;
17 next[0]=-1;
18 memset(dp,0,sizeof(int)*(lenb+1));
19 while(i<lenb)
20 {
21 if(j==-1||st[i]==st[j])
22 next[++i]=++j;
23 else j=next[j];
24 }
25 //得到了一个next数组...
26 for(i=1;i<=lenb;i++)
27 dp[i]+=dp[next[i]]+1; //求解所有前缀的种类 while(next[i])
28 int ans=0;
29 for(i=1;i<=lenb;i++)
30 {
31 ans+=dp[i];
32 ans%=10007;
33 }
34 printf("%dn",ans);
35 }
36 return 0;
37 }
- 通过shell脚本得到数据库的基本信息(一)(r9笔记第89天)
- iOS设备唯一标识的前世今生
- python 生成内嵌式字典(dict)-案例从python提取内嵌json写入mongodb
- Golang语言打印九九乘法表
- AVFoundation 框架初探究(四)
- Data Guard跳归档恢复的实践(r9笔记第92天)
- AVFoundation 框架初探究(三)
- AVFoundation 框架初探究(二)
- 关于视图和存储过程的权限问题探究 (r9笔记第87天)
- Java基础-day08-超市购物系统总结
- AVFoundation 框架初探究(一)
- 【Go 语言社区】 HTML5 前端--数据保存实例
- crontab设置导致的服务器进程异常问题 (r10笔记第4天)
- 一条SQL语句的执行计划变化探究(r10笔记第3天)
- 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 数组属性和方法
- 用 Python 实现微信自动回复
- 一行命令给猎狐 F4 带口罩 & 检测是否带口罩
- 备战 618,用 Python 领取京东优惠券
- 一键生成你的微信社交数据报告
- 分析OutOfMemoryError异常
- 用 Python 制作关不掉的端午安康弹窗
- 使用JFR分析性能问题
- MyISAM 迁移至 InnoDB方案
- 解决Seafile局域网访问失败
- 一键解锁网易云音乐变灰歌曲
- 彻底理解 IO多路复用
- 分享Apache环境禁止目录浏览的方法
- DB2 Linux平台安装 Part 1 Linux环境配置
- DB2 Linux平台安装 Part 2 单机版软件安装
- DB2 Linux平台安装 Part 3 实例的建立与配置