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) ...
随机推荐
- DrawWindowFrame
extern void DrawWindowFrame(HWND hWnd)//画窗口边框 { RECT rc; HWND DeskHwnd = ::GetDesktopWindow(); //取得桌 ...
- jooml二次开发---添加文章组件
在写一个joomla组件的时候需要手动添加excel表格,并把表格当做文章的内容添加到前台文章中, 开始不知道怎么下手,索性先把一个基本的组件写出来,在joomla网站上测试是可以访问这个组件的,在p ...
- 编码错误设置错误报 "SyntaxError: Non-ASCII character '/xe6' "
无意中碰到键盘导致一段处理中文拼音的 python 代码跑起来报了个错 “SyntaxError: Non-ASCII character ‘/xe6' " 看了下是注释 # coding: ...
- Error is 10055 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
今天上午,一个同事反映:某系统的某个通过socket来进行通信的服务无法连接上数据库里,在操作系统上用数据库的客户端测试数据库连接也出现这样的错误信息:Error is 10055 由于系统缓冲区空间 ...
- ITaCS Change Password web part
http://changepassword.codeplex.com/ A webpart is used to change your sharepoint AD password.
- Object-C单元测试&MOCK(摘录精选)
断言测试类型: 下面一共18个断言(SDK中也是18个,其含义转自ios UnitTest 学习笔记,真心佩服原文的博主): XCTFail(format…) 生成一个失败的测试: XCTAssert ...
- HTML标签<b>与<strong>以及<i>与<em>的区别
在一般情况下,<b>和<strong>标签的显示效果一样,<i>和<em>标签的显示效果一样.那么它们的区别在哪呢?我们应该使用哪种标签呢? 在w3sc ...
- C#中Thread.sleep()
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题:1.假设现在是 2008-4-7 12:00:00.000,如果我调 ...
- nginx+ tomcat集群+动静资源分离
不知道为什么这个随便删不掉,写了也值显示一半一半不显示, 我把重新写了一遍: nginx + tomcat集群和动静资源分离
- C#读取网页源码
#region 1.读取 网页源码 + static string ReadHtml(string urlStr,int type) /// <summary> /// 读取 网页源码 + ...