Count the string

Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
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.
 
Input
The first line is a single integer T, indicating the number of test cases.
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.
 
Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 
Sample Input
1
4
abab
 
Sample Output
6
思路:(字符串从1开始)
1.可以直接套用kmp的getfail()来得到f[i],但是这里的f[i]并不是p[i] = p[f[i]],而是在第i位失配情况下从第i位退回到第f[i]位判断是否能配对;这样就需要一直f[i]在f[i] = p[f[i]]时累加,直至i到0结束;由于里面使用的是朴素的mp匹配,即并没有进行优化f[i],KMP的时间复杂度为O(n),但是在循环查找的过程中,由于每个点最坏的情况就是倒退到1,这样最坏的时间复杂度就为O(n^2),能在78ms内过纯属数据..太弱了
#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优化的更多相关文章

  1. 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 ...

  2. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  3. 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 ...

  4. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  5. 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) ...

  6. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

随机推荐

  1. 文件I/O之fcntl函数

    fcntl函数可以改变已打开的文件的性质. #include <fcntl.h> int fcntl( int filedes, int cmd, ... /* int arg */ ); ...

  2. android离线安装adt

    打开Eclipse, 在菜单栏上选择help->Install New SoftWare 出现如下界面: 点击 Add按钮,出现如下界面 在Name这而随意输入一个名字:ADT15:点击打开Ar ...

  3. java code to byte code--partone--reference

    Understanding how Java code is compiled into byte code and executed on a Java Virtual Machine (JVM) ...

  4. Android_Handler

    xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  5. Linux学习之路:认识shell和bash

    一.shell  计算机硬件的直接控制者是操作系统的内核(kernel),因为内核的重要性,所以作为用户的我们是无法直接操作内核的,所以我们需要shell调用应用程序或者双击打开安装的应用软件与内核之 ...

  6. 关于Eclipse开发插件(三)

    视图之间实现事件监听 两个视图中的组件之间的互动,在开发插件的时候是经常碰到的问题.点击视图1列表的某项时,视图2的文本框显示相应的字符. 第一种主动式: 主动式就是在视图1的代码块中获取对视图2的对 ...

  7. PV模型

    你想建设一个能承受500万PV/每天的网站吗? 500万PV是什么概念?服务器每秒要处理多少个请求才能应对?如果计算呢? 一.PV是什么 PV是page view的简写.PV是指页面的访问次数,每打开 ...

  8. css扁平化博客学习总结(三)header代码实现

    页头.banner.正文.页脚的宏观布局 1.布局顺序的重要性: 由大到小,着眼最大的部分,慢慢细分. <body> <header><!-- 页头开始 --> & ...

  9. List的add方法与addAll方法的区别

    add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当 ...

  10. 用ASP生成RSS

    <% Response.Clear Response.CharSet="gb2312" '数据集 Response.ContentType="text/xml&qu ...