HDU Count the string+Next数组测试函数
链接:http://www.cnblogs.com/jackge/archive/2013/04/20/3032942.html
题意:给定一字符串,求它所有的前缀出现的次数的和。
这题很纠结,一开始不知道怎么做,如果直接统计子串在主串中出现的次数,orz···肯定
TLE,后来发现这题可以直接从next数组入手,因为next数组表示的是子串中最长公共前后缀串的长度,如果用dp[i]表示该字符串前i个字符中出
现任意以第i个字符结尾的前缀的次数,它的递推式是
dp[i]=dp[next[i]]+1,即以第i个字符结尾的前缀数等于以第next[i]个字符为结尾的前缀数加上它自己本身,这里要好好理解一下,不
太好解释。
举个例子:
i 1 2 3 4 5 6
字符串 a b a b a b
dt[i] 1 1 2 2 3 3
aba中出现的前缀为a,aba,所以dt[3]是2,ababa中出现的前缀为a,aba,ababa,所以dp[5]是3,当i=5时,next[5]=3,所以dp[i]=dp[next[i]]+1
理解了上面的部分就很简单了,后面直接套next数组的模板
第二次理解:
弄出next数组,之后其中的值就是要跳回去的位置,比如 ababab 0 0 1 2 3 4 当i=1,就跳到0的位置加一个,i=2,跳到0的位置加一个,i=3跳到1的位置加一个,(并且1的位置已经有一个了)所以就是2,以此类推……
#include <bits/stdc++.h>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long LL;
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const double EPS= 1e- ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAXN= + ; //kuangbin 模板
void kmp_pre(char x[],int m,int Next[])
{
int i,j;
j=Next[]=-;
i=;
while(i<m)
{
while(-!=j&&x[i]!=x[j]) j=Next[j];
Next[++i]=++j;
}
} char str[MAXN];
int Next[MAXN];
int dp[MAXN]; int M=; int main()
{
int o;
SI(o);
while(o--)
{
int n;
SI(n);
scanf("%s",str);
kmp_pre(str,n,Next);
int ans=;
Rep(i,,n)
{
dp[i]=dp[Next[i]]+;
ans=(ans+dp[i])%M;
}
PI(ans);
}
return ;
}
以下是next测试函数,可以自己测一下,有助于理解next原理
随便输入字符串,最后输出字符串,和对应next的值,
#include <bits/stdc++.h>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long LL;
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const double EPS= 1e- ; /* ///////////////////////// C o d i n g S p a c e ///////////////////////// */ const int MAXN= + ; //kuangbin 模板
void kmp_pre(char x[],int m,int Next[])
{
int i,j;
j=Next[]=-;
i=;
while(i<m)
{
while(-!=j&&x[i]!=x[j]) j=Next[j];
Next[++i]=++j;
}
} char str[MAXN];
int Next[MAXN];
int dp[MAXN]; int M=; int main()
{
while(~scanf("%s",str))
{ kmp_pre(str,strlen(str),Next);
printf(" ");
rep(i,strlen(str))
{
printf("%c ",str[i]);
}puts("");
rep(i,strlen(str)+)
{
printf("%d ",Next[i]);
}puts("");
}
return ;
}
引用大牛的话:
个人认为,next数组在求解的过程中,用到了KMP的思想,当前失配了,就回溯到上一个next,请见 j=next[j] ,先说个结论,如果到位置 i ,如果有
i%(i-next(i))==0 , 那说明字符串开始循环了,并且循环到 i-1 结束,为什么这样呢?
我们先假设到达位置 i-1 的时候,字符串循环了(到i-1完毕),那么如果到第i个字符的时候,失配了,根据next数组的求法,我们是不是得回溯?
然而回溯的话,由于字符串是循环的了(这个是假定的),next[i] 是不是指向上一个循环节的后面一个字符呢??
是的,上一个循环节的末尾是 next[i]-1 ,然后现在循环节的末尾是 i-1 ,然么循环节的长度是多少呢?
所以,我们有 (i - 1) - ( next[i] - 1 ) = i - next[i] 就是循环节的长度(假设循环成立的条件下),但是我们怎么知道这个循环到底成立吗?
现在我们已经假设了 0 — i-1 循环了,那么我们就一共有 i 个字符了,如果有 i % ( i - next[i] ) == 0,总的字符数刚好是循环节的倍数,那么说明这个循环是成立的。
注意还有一点,如果 next[i] == 0,即使符合上述等式,这也不是循环的,举个反例
0 1 2 3 4 5
a b c a b d
-1 0 0 0 1 2
下标为1,2,3的next值均为0,那么 i%(i-next[i])=i%i==0,但是这个并不是循环。
解释完毕,然后再来看下,为什么求出来的循环节长度是最小的呢?
因为next数组失配的时候,总是回溯到最近的循环节,所以i-next[i]就是最小的循环节长度
为什么求出来的循环次数是最多的呢?
循环节长度是最小的了,那么循环次数肯定是最多的了。
总结一下:
如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 ,则说明字符串循环,而且
循环节长度为: i - next[i] (因为i - next[i]比较小)
循环次数为: i / ( i - next[i] ) (因为总长是i,每节长i - next[i],所以次数就是i / ( i - next[i] ))
HDU Count the string+Next数组测试函数的更多相关文章
- K - Count the string kmp_Next数组应用
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的Next数组应用+DP)
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 KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- LB负载均衡集群及DR模式配置
一.系统环境准备: 1.dir服务器 主机名称:dir 系统环境:CentOS release 6.5 (Final) 外网ip:192.168.1.203(网络模式桥接) vip:192.168.1 ...
- UVA-11468 Substring(AC自动机+DP)
题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...
- backtrack下whatweb的使用
whatweb是backtrack下的一款Web识别工具,位于 Applications-->BackTrack-->Information Gathing-->Web Applic ...
- Linux shell编程札记
if-then的高级特性 [[]].[]都是用来测试( 测试分为数值比较.字符串比较.文件比价 )的,[[]]是关键字,里面可以用&&.||.<.>等类似C语言的语法:[] ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- Latex 编译错误: ! pdfTeX error (ext4): \pdfendlink ended up in different nesting level than \pd fstartlink. 解决方法
最近写 AAAI 的文章,下载了其模板,但是蛋疼的是,总是提示错误,加上参考文献总是出错: 如下: ! pdfTeX error (ext4): \pdfendlink ended up in dif ...
- javascript输出图的简单路径
<script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...
- Nginx-缓冲原理及优化
一.作用及原理 作用: 使用缓冲释放后端服务器 反向代理的一个问题是代理大量用户时会增加服务器进程的性能冲击影响.在大多数情况下,可以很大程度上能通过利用Nginx的缓冲和缓存功能减轻.当代理到另一台 ...
- 查看 activex 组件的方法
查看 activex 组件的方法 可以使用的工具COMRaider 直接安装 并选择对应的类型即可查看相关的信息,比OLE/COM Object Viewer 简洁方便. 具体的操作如下: 随意选择一 ...
- nginx设置不使用缓存 add_header Cache-Control no-cache
nginx设置不使用缓存 server { listen 443; #域名 server_name www.dev.163.com; #字符集 charset utf-8; ssl on; ssl_c ...