1879: [Sdoi2009]Bill的挑战
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 803 Solved: 403
[Submit][Status][Discuss]
Description
Input
本题包含多组数据。
第一行:一个整数T,表示数据的个数。
对于每组数据:
第一行:两个整数,N和K(含义如题目表述)。
接下来N行:每行一个字符串。
T ≤ 5,M ≤ 15,字符串长度≤ 50。
Output
如题
Sample Input
5
3 3
???r???
???????
???????
3 4
???????
?????a?
???????
3 3
???????
?a??j??
????aa?
3 2
a??????
???????
???????
3 2
???????
???a???
????a??
Sample Output
914852
0
0
871234
67018
HINT
Source
Day2
【分析】
有趣的状压dp
预处理出来mat[i][ch]表示在第i位填字母ch可以匹配的字串,用01串来表示。
dp[i][j]表示匹配到第i个字符,此时能与状态为j的子串匹配,总共的方案数。
那么
ans=∑(1<<n)−1j=0dp[len][j](j代表的01串中共有k个1)
【代码】
//bzoj 1879 [Sdoi2009]Bill的挑战
#include<bits/stdc++.h>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int p=1000003;
const int mxn=55;
char s[mxn][mxn];
int n,m,T,k,len,ans;
int dp[mxn][1<<15],mat[mxn][mxn];
int main()
{
int i,j,ch;
scanf("%d",&T);
while(T--)
{
M(dp),M(mat),ans=0;
scanf("%d%d",&n,&k);
fo(i,1,n) scanf("%s",s[i]+1);
len=strlen(s[1]+1);
fo(i,1,len)
fo(ch,1,26)
fo(j,1,n)
if(s[j][i]=='?'||s[j][i]-'a'+1==ch)
mat[i][ch]|=(1<<j-1);
dp[0][(1<<n)-1]=1;
fo(i,0,len-1)
fo(j,0,(1<<n)-1) if(dp[i][j])
fo(ch,1,26)
dp[i+1][mat[i+1][ch]&j]=(dp[i+1][mat[i+1][ch]&j]+dp[i][j])%p;
fo(i,0,(1<<n)-1)
{
int x=i,cnt=0;
while(x)
{
if(x&1) cnt++;
x>>=1;
}
if(cnt==k) ans=(ans+dp[len][i])%p;
}
printf("%d\n",ans);
}
return 0;
}
/*
1
3 3
???r???
???????
???????
*/