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 ...
随机推荐
- Eclipse中Ant的配置与测试 转
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
- J2EE相关总结
Java Commons The Java™ Tutorials: http://docs.oracle.com/javase/tutorial/index.html Java Platform, E ...
- Question store (Repeated review)
题目36 - ACM在线评测系统http://acm.nyist.net/JudgeOnline/problem.php?pid=36 用户名密码INVATION 讲道理太卡 第一:要注意不同的函数 ...
- WebGL 入门-WebGL简介与3D图形学
什么是WebGL? WebGL是一项使用JavaScript实现3D绘图的技术,浏览器无需插件支持,Web开发者就能借助系统显卡(GPU)进行编写代码从而呈现3D场景和对象. WebGL基于OpenG ...
- js,jquery判断某一节点是否存在
前两天工作时遇到一问题,就是模块A显示时,B是一种样式,模块A删除,B是另一种样式.记录下判断节点存在的方法. 先写下html <!doctype html> <html> & ...
- CentOS 6.x启动时网卡eth0未激活
简述 安装CentOS 6.x操作系统后,开机时发现没有网络,最后发现系统启动时未激活网卡 - 因为只有在激活状态的网卡才能去连接网络,进行网络通讯. 简述 激活网卡eth0 激活网卡eth0 执行& ...
- 【转】用PowerDesigner对现有的数据库反向工程建立E-R图
转自:http://www.cnblogs.com/oceanshare/archive/2010/02/10/1667071.html 由于早期的一个项目在数据库设计建立时没有输出为E-R图,偶在对 ...
- android关于uses-permission权限列表
在编写Android程序时经常会忘记添加权限,下面是网上收集的关于Androiduses-permission的资料,方便查找~ android.permission.ACCESS_CHECKIN_P ...
- [转]JavaSE 8—新的时间和日期API
为什么我们需要一个新的时间日期API Java开发中一直存在一个问题,JDK提供的时间日期API一直对开发者没有提供良好的支持. 比如,已有的的类(如java.util.Date和SimpleDate ...
- Ubuntu 查看/修改文件编码
使用enca工具可以查看和修改文件编码 1.安装 sudo apt-get install enca 2.使用 查看文件编码 enca –L zh_CN file_name 修改文件编码 enca – ...