uoj5

1、30分做法

     枚举子串,对于每个子串枚举前缀,并与后缀判断比较,时间复杂度(L3),空间复杂度(L)

const oo=1000000007;maxn=1000000+10;
var t,i1,len,i,j:longint;
    num:array[0..maxn] of int64;
    ans:int64;
    s,s1,s2:ansistring;
begin
readln(t);
for i1:=1 to t do
begin
  readln(s);
  len:=length(s);
  ans:=1;
  for i:=1 to len do
  begin
    num[i]:=0;
    for j:=1 to i div 2 do
    begin
      s1:=copy(s,1,j);
      s2:=copy(s,i-j+1,j);
      if s1=s2 then inc(num[i]);
    end;
    ans:=ans*(num[i]+1) mod oo;
  end;
  writeln(ans);
end;
end.

2、100分做法

     一眼看去像KMP 然而KMP学一次忘一次

     woc我也不知道该怎么讲贴个标程算了

     时间复杂度(L),空间复杂度(L)

const oo=1000000007;maxn=1000000+10;
var t,i1,len,i,j:longint;
    num,next:array[0..maxn] of int64;
    ans:int64;
    s:ansistring;
begin
readln(t);
for i1:=1 to t do
begin
  readln(s);
  len:=length(s);
  j:=0;num[1]:=1;
  for i:=2 to len do
  begin
    while (j>0)and(s[j+1]<>s[i]) do j:=next[j];
    if s[j+1]=s[i] then inc(j);
    next[i]:=j;
    num[i]:=num[j]+1;
  end;
  j:=0;ans:=1;
  for i:=2 to len do
  begin
    while (j>0)and(s[j+1]<>s[i]) do j:=next[j];
    if s[j+1]=s[i] then inc(j);
    while 2*j>i do j:=next[j];
    ans:=ans*(num[j]+1) mod oo;
  end;
  writeln(ans);
end;
end.

  

原文地址:https://www.cnblogs.com/x1273011572/p/6708615.html