hud 3336 count the string (KMP)
这道题本来想对了,可是因为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)的更多相关文章
- 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) ...
- hdoj 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) ...
随机推荐
- phpcms v9修改栏目描述的多行文本为编辑器方法
phpcms v9在添加栏目的时候,栏目描述为多行文本,无法满足有图片,以及格式的修改调整,那么仿站网今天告诉大家如何将他改为编辑器,方法如下 找到phpcms/moudles/admin/templ ...
- CCNP第四天 OSPF综合实验(1)
ospf综合实验(1) 本实验主要考察ospf中的接口上的多种工作方式 实验如图所示: 所用拓扑为CCNP标准版,如图: --------------------------------------- ...
- C++ 编写 CorelDRAW CPG 插件例子(1)—WelcomeScreen
据我所知,这是国外论坛最早的一个例子,原贴在此:http://forum.oberonplace.com/showthread.php?t=1880&highlight=Plugins 贴上主 ...
- 用python实现了一下:甲乙两人互猜数字(数理逻辑)
今天在园子里看到博客:超难面试题:甲乙两人互猜数字(数理逻辑).然后琢磨了半天,写了个Python程序实现算法,我得出来的结果是1,6或1,8或35,42的组合, 不知道是否正确,请高人指点? 下面列 ...
- Spark Streaming揭秘 Day27 Job产生机制
Spark Streaming揭秘 Day27 Job产生机制 今天主要讨论一个问题,就是除了DStream action以外,还有什么地方可以产生Job,这会有助于了解Spark Streaming ...
- 从浏览器启动应用程序 - Application URL
关键字:Browser,Application,URL Protocol,Windows,Mac,IE,Chrome,Safari. OS: Windows 7, OS X Yosemite. Win ...
- Ubuntu虚拟机与Window、Arm的通信
Ubuntu虚拟机与Window的通信安装有Ubuntu14.04的虚拟机VMware,将虚拟机的网络适配器配置成NAT类型(默认使用VMnet8进行通信),此时将Ubuntu的IP地址设置成与VMn ...
- Sharing
To store English words, one method is to use linked lists and store a word letter by letter. To save ...
- WCF 傻瓜教程
第一步,新建WCF服务应用程序 第二步,定义接口: 修改接口类,定义你要的方法接口默认文件名:IService1.cs 第三步,实现接口: 在svc文件里实现接口方法,默认文件名:Service1.s ...
- String声明为NULL和""的区别
代码虐我千百遍,我待代码如初恋. String 声明为 NULL 则声明了一个变量不指向任何一块地址,则 length()会出现错误. 声明为"",则是一个长度为0的字符串.