题意:构造长度为N的字符串,每一位可以是R或者B。给了P个前缀,构造的字符串不可包含这P个前缀。(如果不是作为前缀出现则可以。)问有多少种构造方式。
如果前缀长度是x,以此为前缀,有2^(N-x)个字符串是invalid的。
如果这P个前缀是互斥的,invalid的总和就是 sum_x 2^(N-x),但是P个前缀可能有重合,比如RB,RBBB这种。emmm然后我想到了容斥原理。但是容斥原理general case下要计算2^N个子集,而且也没有利用到前缀和的性质。
如果有两个prefix一个是另一个的前缀,比如N=4,R和RB,
R_ _ _ (1)
RB_ _ (2)
(1)中的RB_ _在(2)中也会出现所以(1)中Invalid字符串只要考虑RR_ _ 。
对于两个prefix A,B如果A是B的前缀且len(B)-len(A)=y,A对应的invalid字符串是(2^y-1)*(2^(N-len(A)-y)) ,重复的部分归到B中计算。
有些坑:
1. prefix A可能是多个prefix的前缀,例如 B,BR,BB
2. prefix A是prefix B的前缀,prefix B是prefix C的前缀,例如 B,BR,BRBB。A和C重合的部分已经被B排除了,所以只需要分别计算A and B, B and C。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
//Kickstart Round H Problem A
const int maxn=110;
int T;
int P;
int N;
string arr[maxn];
long long ans;
bool issubstr(int x,int y)
{
string tmp=arr[y].substr(0,arr[x].length());
return arr[x]==tmp;
}
long long twopow[maxn];
int mp[maxn][maxn];
int cmpans;
char cmpstr[maxn];
void dfs(int n)
{
if(n==N)
{
bool flg=true;
for(int i=0;i<P;i++)
{
bool equ=true;
for(int j=0;j<arr[i].length();j++)
{
if(arr[i][j]!=cmpstr[j])
{
equ=false;
break;
}
}
if(equ==true)
{
flg=false;
}
}
if(flg==true)
{
cmpans++;
}
// cout<<cmpstr<<" "<<flg<<endl;
return;
}
cmpstr[n]='R';
dfs(n+1);
cmpstr[n]='B';
dfs(n+1);
return;
}
int main()
{
// freopen("input.txt","r",stdin);
freopen("A-large.in","r",stdin);
freopen("Alarge.txt","w",stdout);
// freopen("Asmall-cmp0.txt","w",stdout);
clock_t START_TIME;
clock_t FINISH_TIME;
START_TIME=clock();
scanf("%d",&T);
memset(twopow,0,sizeof(twopow));
twopow[0]=1;
for(int i=1;i<maxn;i++)
{
twopow[i]=2*twopow[i-1];
}
for(int ca=1;ca<=T;ca++)
{
memset(mp,0,sizeof(mp));
memset(cmpstr,0,sizeof(cmpstr));
ans=0;
cmpans=0;
cin>>N>>P;
for(int i=0;i<P;i++)
{
string tmp;
cin>>tmp;
cin.ignore();
arr[i]=tmp;
}
sort(arr,arr+P);
// for(int i=0;i<P;i++)
// {
// cout<<arr[i]<<" ";
// }
// cout<<endl;
ans=twopow[N];
// cout<<ans<<endl;
for(int i=0;i<P;i++)
{
for(int j=i+1;j<P;j++)
{
if(issubstr(i,j)==true)
{
mp[i][j]=1;
}
}
}
for(int i=0;i<P;i++)
{
for(int j=i+1;j<P;j++)
{
for(int k=i+1;k<j;k++)
{
if(mp[i][k]==1&&mp[k][j]==1)
{
mp[i][j]=0;
break;
}
}
}
}
// for(int i=0;i<P;i++)
// {
// for(int j=i+1;j<P;j++)
// {
// cout<<i<<" "<<j<<" "<<mp[i][j]<<endl;
// }
// }
for(int i=0;i<P;i++)
{
long long tmp0=twopow[N-arr[i].length()];
// cout<<i<<" "<<tmp0<<endl;
for(int j=i+1;j<P;j++)
{
if(mp[i][j]==1)
{
int len0=arr[j].length()-arr[i].length();
int len1=N-arr[j].length();
long long tmp=twopow[len1];
// cout<<i<<" "<<j<<" "<<len0<<" "<<len1<<" "<<tmp<<endl;
tmp0-=tmp;
}
}
// cout<<i<<" "<<tmp0<<endl;
ans-=tmp0;
}
// dfs(0);
// cout<<cmpans<<endl;
// printf("Case #%d: %d\n",ca,cmpans);
printf("Case #%d: %lld\n",ca,ans);
cerr<<"finish case "<<ca<<endl;
}
FINISH_TIME=clock();
cerr<<1.0*(FINISH_TIME-START_TIME)/CLOCKS_PER_SEC <<" (s) "<<endl;
return 0;
}
其实更直观的解法的把重复的部分算到前缀上,对于prefix A, B并且A是B的前缀,B对应的invalid字符串已经被A对应的invalid字符串包含了,所以把B删除即可。==