(KMP)Count the string -- hdu -- 3336
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): 6672 Accepted Submission(s): 3089
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.
1 | 2 | 3 | 4 | 5 | |
a | b | a | b | a | |
-1 | 0 | 0 | 1 | 2 | 3 |
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define MOD 10007
#define N 210000 char M[N];
int Next[N], dp[N]; void FindNext(int len)
{
int i=, j=-;
Next[] = -; while(i<len)
{
if(j==- || M[i]==M[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, i, Sum=;
memset(dp, , sizeof(dp)); scanf("%d%s", &n, M); FindNext(n);
for(i=; i<=n; i++)
{
dp[i] = (dp[Next[i]] + )%MOD;
Sum = (dp[i]+Sum)%MOD;
} printf("%d\n", Sum);
}
return ;
}
(KMP)Count the string -- hdu -- 3336的更多相关文章
- Count the string HDU - 3336
题意: 求一个字符串的每个前缀在这个字符串中出现次数的加和 解析: 默默的骂一句...傻xkmp..博主心里气愤... 拓展kmp就好多了... 因为拓展kmp每匹配一次 就相当于这些前缀出现了一 ...
- Count the string - HDU 3336(next+dp)
题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", &quo ...
- Count the string -- HDOJ 3336
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Matlab学以致用 - 曲线拟合
曲线拟合 使用Matlab自带的polyfit函数,可以很方便地根据现有样本数据进行多项式曲线拟合,为了有直观感受,先上例程,如下所示: x = -:; % 样本数据x坐标 y = *x.^ + *x ...
- 迷你MVVM框架 avalonjs 学习教程12、数据联动
在许多表单应用,我们经常遇到点击一个复选框(或下拉框)会引发旁边的复选框(或下拉框)发生改变,这种联动效果用avalon来做是非常简单的.在avalon里,存在各种绑定回调与$watch回调,完全满足 ...
- session会话时间
session对象失效在下列情况下被删除: A.程序调用HttpSession.invalidate() B.距离上一次收到客户端发送的session id时间间隔超过了session的最大有效时间 ...
- mysql 建库建表建用户
1.创建数据库 create database school; 2.使用数据库 Use school; 3.创建用户 create user jame@localhost identified by ...
- JS中创建对象的方法及json
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 反射, getClass(), 和something.class以及类型类(转)
原文地址:http://www.cnblogs.com/lianghui66/archive/2012/12/03/2799134.html 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统 ...
- android笔记:ListView及ArrayAdapter
ListView用于展示大量数据,而数据无法直接传递给ListView,需要借助适配器adapter来完成. ArrayAdapter是最常用的adapter,可以通过泛型来指定要适配的数据类型.常见 ...
- 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...
- 数据流滑动窗口平均值 · sliding window average from data stream
[抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...
- (三)介绍简单的ROS命令
1.ROS文件系统中的基本概念 Packages: Package是ROS系统中最底层最基本的组织,里面存放各种文件:库.工具.可执行文件等.Manifest: 一个package描述xml文件,定义 ...