hdu 3336 Count the string KMP+DP优化
Count the string
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.
4
abab
#include <bits/stdc++.h>
using namespace std;
#define mod 10007
const int maxn=2e5+;
int f[maxn];
char s[maxn];
void getfail(char *T)
{
int i=,j=,n=strlen(T+);f[]=;
while(i<n){
if(j==||T[i]==T[j])//确定当前位,给下一位一个机会
++i,++j,f[i]=j;
else
j=f[j];
}
}
int main()
{
int T,n;
cin>>T;
while(T--)
{
scanf("%d%s",&n,s+);
getfail(s);
int cnt=;
for(int i=;i<=n;i++){
int t=;
for(int j=i;j;j=f[j])
if(s[i]==s[j]) // 相等才累加;
++t;
cnt=(cnt+t)%mod;
}
cout<<cnt<<endl;
}
return ;
}
2.上面的是正版的KMP...对于每一个f[i]其实只是一个试探可能性,并不确定是否相等;那么我们就利用前面的相等的f[i]来确定与当前位相等的f[i],这样就不需要病了f[i]来找是否p[i] = p[f[i]]了,并且这是一个基础,即一个子结构。用一个数组记录下前一个p[f[i]]的前缀串,直接+1即可;
62ms,时间复杂度为O(n)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+;
const int mod = ;
char p[N];
int f[N],dp[N];
void getfail()
{
int j=,n=strlen(p+);
f[]=;
for(int i = ;i <= n;i++){
while(j && p[j+] != p[i]) j = f[j];//确定当前位置是前面前缀串的最后一位;
if(p[i] == p[j+]) j++;
f[i] = j;
}
}
int main()
{
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s", p+);
getfail();
int ans = ;
dp[] = ;
for(int i = ;i <= n;i++){
dp[i] = dp[f[i]] + ;
ans = (ans+dp[i])%mod;
}
printf("%d\n",ans);
}
return ;
}
hdu 3336 Count the string KMP+DP优化的更多相关文章
- 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][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
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 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(next数组运用)
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(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
随机推荐
- IBM Rational ClearCase 部署指南
引言 随着时间的推移,可视化设计与软件配置管理(SCM)已经逐渐成为现代软件项目成功的关键因素.IBM Rational 是 IBM Rational XDE 和 IBM Rational Clear ...
- iOS7 设置隐藏状态栏(status bar)
在info.plist 添加 UIViewControllerBasedStatusBarAppearance(View controller-based status bar appearance) ...
- Maven项目中如何添加日志
- MVC中使用QrCodeNet 生成二维码
QrCodeNet下载地址:http://qrcodenet.codeplex.com/ using System.Drawing; using System.Drawing.Imaging; usi ...
- gcc的基础知识
GCC(GNU Compiler Collection,GNU编译器集合)是一套由GNU工程开发的支持多种编程语言的编译器. 基本用法 在使用Gcc编译器的时候,我们必须给出一系列必要的调用参数和文件 ...
- selendroid项目实战3 selendroid driver初始化失败问题
小米4/LG手机作为测试用机,随着测试时间变长,driver初始化失败率越来越高. 分析: 1.手机原因: 从小米换到LG,刚开始问题确实减少了,但是时间一长,又出现类似问题,提示Connect re ...
- PHP自动执行程序
/****config.php***/ <?php return 1; //需要停止时改成0 ?> /******************/ ignore_user_abort();//关 ...
- JDBC入门try/catch型
package com.itheima.domain; import java.sql.Connection; import java.sql.DriverManager; import java.s ...
- MVC下设置默认页为index.html
将RouteConfig代码修改为如下 public class RouteConfig { public static void RegisterRoutes(RouteCollection rou ...
- SVN-钩子
先说说钩子是干什么的吧,,简单的说,svn钩子就是在提交svn时前后所要触发的事件,于是我们可以用钩子做一些提交时的限制,及提交后的操作.最常用的一般有两个,pre-commit,post-commi ...