不难想到是一个布尔型dp,

不难想到用f[i,j,k]表示区间[i,j]能否变为字母k

不难想到对于f[i,j,k],拆[i,j]成两个区间,然后穷举k的每一个变换来判断

感觉记忆化搜索写的比较顺,就写了记忆化

 1 const a:array[1..4] of char=('W','I','N','G');
 2 
 3 var b:array[0..300] of longint;
 4     f:array[0..300,0..300,0..4] of integer;
 5     w:array[0..4,0..20] of longint;
 6     c:array[1..5] of longint;
 7     l,i,j,k,e,n,m,p:longint;
 8     flag:boolean;
 9     s:string;
10 
11 function exc(x:char):longint;
12   begin
13     if x='W' then exit(1)
14     else if x='I' then exit(2)
15     else if x='N' then exit(3)
16     else if x='G' then exit(4);
17   end;
18 
19 function search(l,r,k:longint):integer;
20   var i,j,x1,x2:longint;
21   begin
22     if f[l,r,k]<>0 then exit(f[l,r,k]);
23     if l=r then
24     begin
25       f[l,l,b[l]]:=1;
26       if k=b[l] then exit(1)
27       else begin
28         f[l,l,k]:=-1;
29         exit(-1);
30       end;
31     end;
32     for i:=1 to c[k] do
33     begin
34       x1:=(w[k,i]-1) div 4+1;
35       x2:=(w[k,i]-1) mod 4+1;
36       for j:=l to r-1 do
37       begin
38         if (search(l,j,x1)=1) and (search(j+1,r,x2)=1) then
39         begin
40           f[l,r,k]:=1;
41           exit(1);
42         end;
43       end;
44     end;
45     f[l,r,k]:=-1;
46     exit(-1);
47   end;
48 
49 begin
50   for i:=1 to 4 do
51     read(c[i]);
52   readln;
53   for i:=1 to 4 do
54     for j:=1 to c[i] do
55     begin
56       readln(s);
57       p:=(exc(s[1])-1)*4+exc(s[2]);
58       w[i,j]:=p;
59     end;
60 
61   readln(s);
62   n:=length(s);
63 
64   for i:=1 to n do
65     b[i]:=exc(s[i]);
66 
67   flag:=false;
68   for i:=1 to 4 do
69     if search(1,n,i)=1 then
70     begin
71       write(a[i]);
72       flag:=true;
73     end;
74   if not flag then writeln('The name is wrong!') else writeln;
75 end.
View Code

 

posted on 2014-08-04 16:45  acphile  阅读(122)  评论(0编辑  收藏  举报