noip2001-统计单词个数2008.11.5

noip2001-统计单词个数2008.11.5

 

心得1.dp前,可能要对数据进行处理

2.拿到一个题目,要看有没有和以前做过的题目相似的地方。

3.一定要静态差错,多查几遍,又是一个字母打错,就挂了!

program1:算从Ij有多少个单词时,wa2

program noip;
const fin='tjdcgs.in';fout='tjdcgs.out';
var a:array[1..6]of string;
    b:array[1..6]of longint;
    x,x1:string;
    y,s,p,m,l,i,j,k,n:longint;
    sum:array[1..200,1..200]of longint;
    c:array[1..200]of longint;
    f:array[0..200,0..40]of longint;
    f1,f2:text;
procedure init;
begin
  fillchar(sum,sizeof(sum),0);
  fillchar(c,sizeof(c),0);
  fillchar(f,sizeof(f),0);
  readln(f1,p,m);l:=p*20;x:='';
  for i:=1 to p do
    begin readln(f1,x1);
          x:=x+x1;
    end;
    readln(f1,s);
  for i:=1 to s do
    begin
      readln(f1,a[i]);
      b[i]:=length(a[i]);
    end;
 for i:=1 to s-1 do
  for j:=i+1 to s do
    if b[i]>b[j] then
      begin
        x1:=a[i];a[i]:=a[j];a[j]:=x1;
        p:=b[i];b[i]:=b[j];b[j]:=p;
      end;
{  for i:=1 to s do
    writeln(a[i]);}
end;
procedure clear;
var i,j:longint;xx:string;
begin  xx:='*'+x;
  for i:=1 to l do
   begin  xx:=copy(xx,2,length(xx)-1);
      for j:=1 to s do
        if pos(a[j],xx)=1 then
          begin c[i]:=b[j];break;
          end;
   end;
{  for i:=1 to l do
    writeln(i,'---',c[i]);}
   for i:=1 to l do
    for j:=i to  l do
      for k:=i to j do
        if (c[k]+i-1<=j)and(c[k]<>0) then inc(sum[i,j]);
应为:if (c[k]+k-1<=j)and(c[k]<>0) then inc(sum[i,j]);
{    for i:=1 to l do
      for j:=i to l do
        writeln(i,'  ',j,'------',sum[i,j]);}
end;
procedure dp;
var i,j,k:longint;
begin
  for j:=1 to m do
    for i:=1 to l do
     if i>=j then
      for k:=i-1 downto j-1 do
         if sum[k+1,i]+f[k,j-1]>f[i,j] then f[i,j]:=sum[k+1,i]+f[k,j-1];
  writeln(f2,f[l,m]);
end;
begin
  assign(f1,fin);reset(f1);
  assign(f2,fout);rewrite(f2);
  readln(f1,n);
  for y:=1 to n do
begin
  init;
  clear;
  dp;
end;
  close(f1);close(f2);
end.

Program2ac

program noip;
const fin='tjdcgs.in';fout='tjdcgs.out';
var a:array[1..6]of string;
    b:array[1..6]of longint;
    x,x1:string;
    y,s,p,m,l,i,j,k,n:longint;
    sum:array[1..200,1..200]of longint;
    c:array[1..200]of longint;
    f:array[0..200,0..40]of longint;
    f1,f2:text;
procedure init;
begin
  fillchar(sum,sizeof(sum),0);
  fillchar(c,sizeof(c),0);
  fillchar(f,sizeof(f),0);
  readln(f1,p,m);l:=p*20;x:='';
  for i:=1 to p do
    begin readln(f1,x1);
          x:=x+x1;
    end;
    readln(f1,s);
  for i:=1 to s do
    begin
      readln(f1,a[i]);
      b[i]:=length(a[i]);
    end;
 for i:=1 to s-1 do
  for j:=i+1 to s do
    if b[i]>b[j] then
      begin
        x1:=a[i];a[i]:=a[j];a[j]:=x1;
        p:=b[i];b[i]:=b[j];b[j]:=p;
      end;
{  for i:=1 to s do
    writeln(a[i]);}
end;
procedure clear;
var i,j,u:longint;xx:string;
begin  xx:='*'+x;
  for i:=1 to l do
   begin  xx:=copy(xx,2,length(xx)-1);
      for j:=1 to s do
        if pos(a[j],xx)=1 then
          begin c[i]:=b[j];break;
          end;
   end;
{  for i:=1 to l do
    writeln(i,'---',c[i]);}
   for i:=1 to l do
    for j:=1 to  l do
      for u:=i to j do
        if (c[u]+u-1<=j)and(c[u]<>0) then inc(sum[i,j]);
{    for i:=1 to l do
      for j:=i to l do
        writeln(i,'  ',j,'------',sum[i,j]);}
end;
procedure dp;
var i,j,k,u,v:longint;
begin
  for j:=1 to m do
    for u:=j to l do
      for v:=u to l do
         if f[u-1,j-1]+sum[u,v]>f[v,j] then
              f[v,j]:=f[u-1,j-1]+sum[u,v];
  writeln(f2,f[l,m]);
end;
begin
  assign(f1,fin);reset(f1);
  assign(f2,fout);rewrite(f2);
  readln(f1,n);
  for y:=1 to n do
begin
  init;
  clear;
  dp;
end;
  close(f1);close(f2);
end.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值