(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) ...
随机推荐
- 趣味编程:CPS风格代码(Java 8,Functional Java版)
CPS风格代码(Java 8版) package fp; import java.util.function.IntConsumer; public class CPS { static int ad ...
- Git reset head revert 回滚
Overview 涉及Git一些日常操作 :) 基础知识 <Pro Git>至少了解branch,commit的概念,及基本的原理 Git常用魔法 存档:master代码回滚方法 我是QA ...
- sessionStorage和localStorage
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...
- oracle老是出现监听错误
解决方法之一: 点击开始-->选择程序--->选中你安装的oracle版本-->选中配置与移植工具-->选中Net Configuration Assistant-->在 ...
- 吴裕雄 实战PYTHON编程(10)
import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ...
- TLS/SSL 协议详解 ssL 、TLS 1.0、TLS 1.1、TLS 1.2的了解
TLS 1.0 RFC http://www.ietf.org/rfc/rfc2246.txt TLS 1.1 RFC http://www.ietf.org/rfc/rfc4346.txt TLS ...
- 安装Android模拟器
说明:可以直接通过Android SDK目录下的SDK Manager.exe进行安装,我这里写的是通过下载各种文件进行安装的方法,没有通过SDK Manager.exe进行下载安装 一.下载以下内容 ...
- The partial charge density (1)
============================================================================================= The pa ...
- win10系统打开自带的画图软件的步骤
1.win+R打开输入系统命令的输入框 2.输入mspaint即可打开电脑自带的画图软件.
- loadrunner11--集合点(Rendezvous )菜单是灰色不能点击
新建场景的时候“Manual Scenario”下的check box不能选中,取消选中就好了.即Vuser不能以百分比的形式. 所以:集合点灰化有两种情况: 脚本没有添加集合点函数 场景中设置以Vu ...