bzoj1879: [Sdoi2009]Bill的挑战(codevs2308)(luoguP2167) 状压dp

j 这个状态指 n 个串中选了哪些串。

f[i,j & g[i,x]+=f[i-1,j]

枚举一个 x 字符，对于 ‘a’-‘z’ 这些字符都可以是第 i 位的。

 1 const HR=1000003;
2 var s:string;
3     i,j,x:longint;
4     g:array[0..55,0..26]of longint;
5     t,n,k:longint;
6     f:array[0..55,0..35000]of longint;
7     num:array[0..35000]of longint;
8     len:longint;
9     ans:longint;
10 function check(x:longint):longint;
11 var i,num:longint;
12 begin
13   num:=0;
14   for i:=1 to 15 do
15   if (1 << (i-1))and x>0 then inc(num);
16   exit(num);
17 end;
18 begin
20   for i:=0 to (1 << 15)-1 do
21   num[i]:=check(i);
22   while t>0 do
23   begin
24     dec(t);
25     ans:=0;
27     for i:=1 to n do
28     begin
30       if i=1 then len:=length(s);
31       for j:=1 to len do
32         for x:=0 to 25 do
33         if ((ord(s[j])-97)=x)or(s[j]='?') then
34           g[j,x]:=g[j,x] or (1 << (i-1));
35     end;
36     f[0,(1 << n)-1]:=1;
37     for i:=1 to len do
38     begin
39       for j:=0 to (1 << n)-1 do
40       if f[i-1,j]<>0 then
41       begin
42         for x:=0 to 25 do
43         begin
44           inc(f[i,(g[i,x] and j)],f[i-1,j]);
45           if f[i,(g[i,x] and j)]>=HR then
46             f[i,(g[i,x] and j)]:=f[i,(g[i,x] and j)] mod HR;
47         end;
48         f[i-1,j]:=0;
49       end;
50       if i=len then
51       begin
52         for j:=0 to (1 << n)-1 do
53         begin
54           if num[j]=k then
55           begin
56             inc(ans,f[len,j]);
57             if ans>=HR then ans:=ans mod HR;
58           end;
59           f[i,j]:=0;
60         end;
61       end;
62     end;
63     for i:=1 to len do
64     for x:=0 to 25 do
65     g[i,x]:=0;
66     writeln(ans);
67   end;
68 end.
bzoj1879

posted @ 2017-09-03 18:26  Bunnycxk  阅读(226)  评论(0编辑  收藏  举报