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=10007;
var sum,next:array[0..200000] of longint;
i,j,n,t,ans:longint;
a:array[0..200000] of char;
procedure main;
begin
fillchar(next,sizeof(next),0);
readln(n);
readln(a);
next[0]:=-1;
i:=-1;j:=0;
while j<n do
if (i=-1) or (a[i]=a[j]) then
begin
inc(i);inc(j);next[j]:=i;
end
else i:=next[i];
ans:=0;
fillchar(sum,sizeof(sum),0);
for i:=1 to n do inc(sum[next[i]]);
for i:=1 to n do
ans:=(ans+sum[i]+1) mod mo;
writeln(ans);
end;
begin
readln(t);
while t>0 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[0..1000100] of longint;
procedure main;
begin
readln(n,m);
for i:=1 to n do read(a[i]);readln;
for i:=1 to m do read(b[i]);
fillchar(next,sizeof(next),0);
i:=0;j:=1;
while j<m do
if (i=0) or (b[i]=b[j]) then
begin
inc(i);inc(j);next[j]:=i;
end
else i:=next[i];
i:=1;j:=1;
while (i<=m) and (j<=n) do
if (i=0) 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(-1);
end;
begin
readln(t);
while t>0 do
begin
main;
dec(t);
end;
end.

这里面比较的是数。

时间: 2024-08-09 23:56:26

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

HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

hdu 3336 Count the string KMP+DP

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6375    Accepted Submission(s): 2947 Problem Description It is well known that AekdyCoin is good at string problems as well as n

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

hdu 3336 Count the string

Count the stringTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4239    Accepted Submission(s): 1977 Problem Description It is well known that AekdyCoin is good at string problems as well as num

HDUOJ------3336 Count the string(kmp)

D - Count the string Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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 wr

KMP——hdu 3336 count the string

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10478    Accepted Submission(s): 4893 Problem Description It is well known that AekdyCoin is good at string problems as well as n

喝豆浆 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6062    Accepted Submission(s): 2810 Problem Description It is well known that AekdyCoin is good at string problems as well as nu

HDU 3336 Count the string (next数组活用)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5224    Accepted Submission(s): 2454 Problem Description It is well known that AekdyCoin is good at string problems as well as n

HDU 3336 Count the string (基础KMP)

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&qu