Advertisement
Guest User

Algorytm2

a guest
Apr 19th, 2015
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.22 KB | None | 0 0
  1. type mas=array[0..1000,0..1000] of longint;
  2.      vector=array[0..1000,0..1000] of char;
  3. var a:vector; b:mas;
  4.     ii,jj,i,j,n,k,f,l:longint;s:string;x,c,c1:char;
  5.  
  6. function max(aa,bb:longint):longint;
  7. begin
  8.    if aa>bb then max:=aa else max:=bb;
  9. end;
  10.  
  11. begin
  12.  readln (n);     k:=0;  c:='f';
  13.  for i := 1 to n do
  14.   begin
  15.  
  16.     for j := 1 to n do  begin read(x);a[i,j]:=x;if x=c then k:=k+1;c:=x;
  17.                         end; readln;
  18.    end;
  19.  
  20.    readln(s);
  21.  
  22.   if n*n-1=k then
  23.    begin
  24.    f:=1;
  25.      for I := 1 to n do
  26.        f:=(f*i) mod 1000003;
  27.        writeln(f)
  28.    end   else
  29.    begin
  30.  
  31.     c:=s[1];
  32.     c1:=s[2*n-1];
  33.  if (a[1,1]<>c)or (a[n,n]<>c1) then  writeln(0) ELSE
  34. begin
  35.    b[1,1]:=1;
  36.    for I := 2 to n do
  37.      begin
  38.      if (a[1,i]=s[i]) and (b[1,i-1]<>0) then  b[1,i]:=1;
  39.      if (a[i,1]=s[i]) and (b[i-1,1]<>0) then  b[i,1]:=1;
  40.     end;
  41.  
  42.      for I := 2 to n do
  43.      for j := 2 to n do
  44.        begin
  45.          if a[i,j]=s[i+j-1] then
  46.           begin
  47.           if (a[i-1,j]<>a[i,j-1]) then b[i,j]:=max(b[i-1,j],b[i,j-1])  else
  48.           b[i,j]:=(b[i,j-1] mod 1000003+b[i-1,j] mod 1000003) mod 1000003;
  49.           end ;
  50.  
  51.        end;
  52.  
  53.  
  54.     writeln(b[n,n] mod 1000003);
  55.     end;      end;
  56.      
  57. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement