您的位置:首页 > 其它

[CF Gym 100372B] Sergey and a pattern

2017-01-24 14:45 323 查看

题意

给出一个初始序列ai和一个最终序列bi,要求用一个栈执行两种操作

1 从ai的前面取出一个数字压到栈中

2 弹栈,将栈顶放到bi的后面。给出的最终序列中有一些位置是位置的数字,询问有多少种方法构造出bi来。

长度至多16

题解

将ai中数字是否在栈中状压,状态表示
f[压栈次数][弹栈次数][状态]
然后dp

代码

#include <cstdio>

#define  maxn  16
#define  maxs  (1<<16)
#define  mod   1000000007LL

int n, a[maxn+1], b[maxn+1], f[maxn+1][maxn+1][maxs] = {0};
/// f[in][out][exist]
int main() {
int i, j, s, k;
scanf("%d", &n);
for (i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for (i = 1; i <= n; i ++ ) scanf("%d", &b[i]);
///  build array b from array a !
f[0][0][0] = 1;
for (i = 0; i <= n; i ++ )
for (j = 0; j <= i; j ++ )
for (s = (1<<i)-1; s >= 0; s -- ) {
if (!f[i][j][s]) continue;
if (i < n) (f[i+1][j][s|(1<<i)] += f[i][j][s]) %= mod;
if (!s) continue;
for (k = i; !((1<<k-1)&s); k -- );
if (b[j+1]!=-1 && a[k]!=b[j+1]) continue;
(f[i][j+1][s^(1<<k-1)] += f[i][j][s]) %= mod;
}
printf("%d\n", f

[0]);
//while(1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: