poj2752 bzoj3670

时间:2023-03-09 16:46:08
poj2752 bzoj3670

2752这是一道关于next函数的题
(其实好像也可以用后缀数组暴力搞搞,但大概会超时)
根据next[i]=max{j} (s[0..j]=s[i-j..i] j<i)
不难发现这正是某个串既是前缀又是后缀的形式
所以我们先求一遍kmp,然后就是找出所有这样的串
首先最长的肯定是原串,,次长的肯定是s[0..next[n]]
那下一个呢?答案是只要一直从n按着next迭代下去就能找到可行解。
我们不妨设next[i]=j next[j]=k,当i是可行解,那j一定也是
因为s[0..j]=s[i-j..i]可得s[0..k]=s[i-k..k]=s[i-k,i]
然后就解决了
bzoj3670是今年的noi题,其实也是一样的,只不过多了不能重叠,其实也是很好解决的

 code:poj2752

 var next,q:array[..] of longint;
i,j,n,t :longint;
s:array[..] of char;
ch:ansistring; begin
while not eof do
begin
readln(ch);
n:=length(ch);
for i:= to n- do
s[i]:=ch[i+];
i:=;
j:=-;
next[]:=-;
while (i<n) do
begin
if (j=-) or (s[i]=s[j]) then
begin
inc(i);
inc(j);
next[i]:=j;
end
else j:=next[j];
end;
t:=;
j:=n;
while j<> do
begin
inc(t);
q[t]:=next[j];
j:=next[j];
end;
for i:=t- downto do
write(q[i],' ');
writeln(n);
end;
end.

poj2752

 const mo=;

 var f,next:array[..] of longint;
a:array[..] of char;
k,n,t,i,j:longint;
ans:int64;
s:ansistring; begin
readln(k);
while k> do
begin
dec(k);
readln(s);
n:=length(s);
for i:= to n do
a[i-]:=s[i];
i:=;
j:=-;
next[]:=-;
ans:=;
while i<n do
begin
if (j=-) or (a[i]=a[j]) then
begin
inc(i);
inc(j);
next[i]:=j;
f[i]:=f[j]+;
end
else j:=next[j];
end;
i:=;
j:=-;
while i<n do
begin
if (j=-) or (a[i]=a[j]) then
begin
inc(i);
inc(j);
while (j*>i) do j:=next[j];
ans:=ans*int64(f[j]+) mod mo;
end
else j:=next[j];
end;
writeln(ans);
end;
end.

bzoj3670