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 }
- Http协议中的数据传送之多重表单提交--multipart/form-data
- 另类大数据:中国有嘻哈的rapper们都在唱些什么?
- jquery属性值选择器
- sqlserver 2000/2005 Ambiguous column error错误解决办法
- jquery 层级选择器
- Twitter发布基于组件的轻量级JavaScript框架——Flight
- jquery的基本选择器
- WordPress 路径相关函数总结(一):站点路径相关函数
- 修复Visual Studio 2010 SP1的Toolbox导致的VS不可用
- 破解Excel 密码保护
- 配置Subversion
- WordPress 路径相关函数总结(二):主题路径相关函数
- ASP.NET 2.0 中 Web 事件
- Visual Studio 必备可视化插件推荐
- 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 数组属性和方法
- Android 功耗(3)---高通功耗问题分析方法
- 搞定Java快速排序
- Android 功耗(4)---MTK平台待机功耗分析流程
- 使用iframe实现在pc端预览移动端页面的效果
- html+css实现彩色渐变滑动条
- 项目实战 01:将唐诗三百首写入 Elasticsearch 会发生什么?
- 装饰器和代理模式的区别,从一碗小米粥谈起
- EasyNVR部署在centos虚拟机上出现无法访问情况,该如何排查?
- RTSP协议网页无插件直播平台EasyNVR视频广场无法搜索纯数字关键词的通道,该如何解决的?
- RTSP协议网页无插件直播平台EasyNVR通道管理搜索纯数字关键词,返回结果为空如何解决?
- Mysql如何选择唯一索引和普通索引
- Python自学成才之路 多线程开发
- Mysql为什么会抖一下呢
- Python自学成才之路 线程间协作 lock,condition,event的使用
- Java 语言基础 (初识Java语言, 变量和数据类型, 运算符, 流程控制语句, 数组)