HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少。
此题要用KMP的next和DP来做。
next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置。
下标从0开始。
设j=next[i],那么
如果j==0,回溯到起点说明该字符不匹配。
其他情况,说明字符串S[0,...j-1]与字符串S[0,..i-1]的某个后缀(准确的说是S[i-j,i-1])相同,这样的话,S[0,..i-1]的后缀(S[i-j,i-1])一定包含字符串S[0,..i-1]的后缀能够匹配的前缀个数,除此之外,S[0,..i-1]的后缀还应该包括自身这种情况。
设dp[i]表示字符串S[0,..i-1]的后缀可以匹配的前缀个数。
这样dp[i]=dp[next[i]]+1
答案ans=sum{dp[i]}(1<=i<=n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define MOD 10007
#define MAXN 200005
using namespace std;
char str[MAXN];
int next[MAXN];
int dp[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
scanf("%s",str);
;i<n;++i)
{
int j=next[i];
while(j&&str[i]!=str[j]) j=next[j];
next[i+]=(str[i]==str[j])?j+:;
}
;
;i<=n;++i)
{
dp[i]=(dp[next[i]]+)%MOD;
ans=(ans+dp[i])%MOD;
}
printf("%d\n",ans);
}
;
}
HDU 3336 - Count the string(KMP+递推)的更多相关文章
- 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&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- [HDU 3336]Count the String[kmp][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- HDU 3336 Count the string ( KMP next函数的应用 + DP )
dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...
- 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算法)
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) ...
- hdu 3336 Count the string(next数组)
题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...
- HDU 3336 Count the string
题解:利用next数组来保存前缀位置,递推求解. #include <cstdio> #include <cstring> char pat[200005]; int next ...
随机推荐
- iOS AVCaptureVideoDataOutputSampleBufferDelegate 录制视频
iOS AVCaptureVideoDataOutputSampleBufferDelegate 录制视频 应用场景: 使用AVFoundation提供的API, 我们可以从 AVCaptureVid ...
- JUnit org.junit.runner.Request.classWithoutSuiteMethod解决方法
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- redis命令集合
一.连接控制 QUIT 关闭连接 AUTH (仅限启用时)简单的密码验证 二.适合全体类型的命令 EXISTS key 判断一个键是否存在;存在返回 1;否则返回0;DEL key 删除某个key,或 ...
- Date的那一大堆事儿--1
String perfTimeStr = "";// 统一设置日历格式 Calendar calendar = Calendar.getInstance(); calendar.s ...
- robotframework笔记8
文件变量 包含变量类型 Python 的 '.py' 扩展文件只不过是变量文件. # -*- Coding: utf-8 -*- var01 = "Hello " var02 = ...
- 127. 126. Word Ladder *HARD* -- 单词每次变一个字母转换成另一个单词
127. Given two words (beginWord and endWord), and a dictionary's word list, find the length of short ...
- 用eclipse开发javaweb项目
准备工作:安装并配置jdk jdk环境变量:1.C:\Program Files\Java\jdk1.6.0_31\bin path的配置2.C:\Program Files\Java\jdk1. ...
- Introduction to Windows 8: The Definitive Guide for Developer
<Windows 8应用开发权威指南>介绍 Introduction to Windows 8: The Definitive Guide for Developer 一.封面设计要求及文 ...
- myeclipse设置编码格式的4种情况
(1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...
- jquery 添加方法 : $.fn.方法名 = function(参数a,b,c){
$.fn.image_checked = function(self,status,img_body,csrf_token){ $(this).live('click', fu ...