noip2001统计单词个数

本题字符串处理需要注意;

单词个数的计算:先算s[i]表示以i开头的单词,若有则s[i]=1,否则为0.(这要充分注意题目的要求)

考试时很难想到。

另外在算[i,j]中单词的个数时,如果以i为开头,需要判断这个单词结尾有没有超过j,没有才可计入。

同样需要注意有些状态是达不到的,这时若不赋初值在动规时判断,状态会出错!

program word;
var l,n,p,k,i1,i,j,s1,flag,k1:longint;
    a,aa:string;
    word:array[1..6]of string;
    s,t:array[1..200]of longint;
    su:array[1..200,1..200]of longint;
    f:array[0..200,0..40]of longint;
begin
 readln(p,k1);
 for i:=1 to p do
  begin
   readln(a);
   aa:=aa+a;
  end;
 readln(s1);
 for i:=1 to s1 do
  readln(word[i]);
 n:=p*20;
 for i:=1 to n do
   for j:=1 to s1 do
    if n-i+1>=length(word[j]) then
     if word[j]=copy(aa,i,length(word[j])) then
      begin
       s[i]:=1;
       if t[i]=0 then
        t[i]:=i+length(word[j])-1
       else
        if t[i]>i+length(word[j])-1 then t[i]:=i+length(word[j])-1;//记录单词结尾
      end;


 for i:=1 to n do
  for j:=i to n do
   for k:=i to j do
    if (t[k]>0)and(t[k]<=j) then inc(su[i,j],s[k]);
 for i:=1 to n do
  for j:=1 to k1 do
   f[i,j]:=-1;

 for i:=1 to n do f[i,1]:=su[1,i];
 for j:=2 to k1 do
  for i:=j to n do
   for k:=1 to i-1 do
    if f[k,j-1]<>-1 then   //可能达不到的状态
     if f[i,j]<f[k,j-1]+su[k+1,i] then
      f[i,j]:=f[k,j-1]+su[k+1,i];
 write(f[n,k1]);
end.


 

你可能感兴趣的:(noip2001统计单词个数)