这道题本来想对了,可是因为hdu对pascal语言的限制是我认为自己想错了,结果一看题解发现自己对了……

  • 题意:给以字符串 计算出以前i个字符为前缀的字符中 在主串中出现的次数和
  • 如: num(abab)=num(a)+num(ab)+num(aba)+num(abab)=2+2+1+1=6;
  • 题解:next[i]记录的是 长度为i 不为自身的最大首尾重复子串长度  num[i]记录长度为next[i]的前缀所重复出现的次数
  • 推介一篇博文,非常不错,和本代码解法不一样,但实质上是一样的。

附上代码:

const mo=;
var sum,next:array[..] of longint;
i,j,n,t,ans:longint;
a:array[..] of char;
procedure main;
begin
fillchar(next,sizeof(next),);
readln(n);
readln(a);
next[]:=-;
i:=-;j:=;
while j<n do
if (i=-) or (a[i]=a[j]) then
begin
inc(i);inc(j);next[j]:=i;
end
else i:=next[i];
ans:=;
fillchar(sum,sizeof(sum),);
for i:= to n do inc(sum[next[i]]);
for i:= to n do
ans:=(ans+sum[i]+) mod mo;
writeln(ans);
end;
begin
readln(t);
while t> do
begin
main;
dec(t);
end;
end.
end;

要注意的 hdu不能用ansistring,必须用成array of char 不过读还是可以直接readln(a);

另外再附一个完整的KMP的代码

var i,j,n,m,t:longint;
next,a,b:array[..] of longint;
procedure main;
begin
readln(n,m);
for i:= to n do read(a[i]);readln;
for i:= to m do read(b[i]);
fillchar(next,sizeof(next),);
i:=;j:=;
while j<m do
if (i=) or (b[i]=b[j]) then
begin
inc(i);inc(j);next[j]:=i;
end
else i:=next[i];
i:=;j:=;
while (i<=m) and (j<=n) do
if (i=) or (b[i]=a[j]) then
begin
inc(i);inc(j);
end
else i:=next[i];
if i>m then writeln(j-m)
else writeln(-);
end;
begin
readln(t);
while t> do
begin
main;
dec(t);
end;
end.

这里面比较的是数。

hud 3336 count the string (KMP)的更多相关文章

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

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

  3. HDU 3336 Count the string KMP

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

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

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

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

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

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

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

  8. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

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

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

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

随机推荐

  1. ECSHOP购物流程收货人信息详细地址显示省市区

    方法一: 1.在flow.php中的 elseif ($_REQUEST['step'] == 'checkout') 中 $_SESSION['flow_consignee'] = $consign ...

  2. linux 输入子系统(4)---- input子系统的初始化

    Input子系统的初始化函数为input_init(),如下: static int __init input_init(void) { int err; input_init_abs_bypass( ...

  3. Opencv 的数据结构

    opencv的基本数据结构 结构 成员 意义 CvPoint int x,y 图像中的点 CvPoint2D32f float x,y 二维空间中的点 CvPoint3D32f float x,y,z ...

  4. python中文件的复制

    python中文件的复制 python的os模块有很多文件目录相关的函数,但没有提供直接复制文件的函数,当然可以通过边都边写的方式复制文件.想要直接复制文件可以通过shutil模块 shutil模块是 ...

  5. Vi 几个实用的命令

    vi有三种工作模式:指令模式.编辑模式和命令模式. 我们从打开vi说起,这样可以确定下学习环境,也方便学习者实践.打开vi,当前模式即为指令模式,此时可以按a, i, 或o进入编辑模式,或按:(冒号) ...

  6. poj 3415 Common Substrings 后缀数组+单调栈

    题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...

  7. 在ubuntu下给eclipse创建桌面快捷方式

    在桌面进行编辑 编辑eclipse.desktop [Desktop Entry] Encoding=UTF-8 Name=Eclipse Platform Comment=Eclipse IDE E ...

  8. cadence 封装制作小结

    assembly :是装配层,就是元器件的实际大小,用来产生元器件的装配图.也可以使用此层进行布局:外框尺寸应该为元件除焊盘外的部分 该区域可比silkscreen小10mil,线宽不用设置,矩形即可 ...

  9. stm32之ADC学习

    1.stm32中采用的是逐次逼近型模拟数字方式,那么什么是逐次逼近呢? 逐次逼近的方式类似于二分法,以8位数据为例:当输入一个模拟量的时候,首先取这8位数的一半,即1000 0000,与模拟量比较,大 ...

  10. Fedora 17安装NFS

    1.NFS概述 NFS(Network File System)是一种分布式文件系统,允许网络中的安装不同操作系统的计算机间共享文件和外设,所以它的通讯协定设计与主机及作业系统无关. 它是由SUN公司 ...