# [enumeration] Rikka with String II

Posted by sosha on Mon, 04 May 2020 05:43:16 +0200

### describe

As we all know, mengmengda Liuhua is not good at math, so Yongtai gave her some math problems to practice. One of them is like this:

For n 01 strings of si, their weight is defined as the number of nodes in Trie after the N strings of si are inserted into an empty Trie tree. For example, the weight of ["01", "00"] is 4, and the weight of ["010", "1"] is 5.

Now, Yong Tai gives n strings si containing only 01. Where? Means either 0 or 1. Obviously, if there are k?, there are 2K possible string sets.

Now Yongtai wants to know the sum of weights for 2K states.

Of course, this question is too difficult for cute six flowers. Can you help her?

### Analysis

For each scheme, the total number of nodes is actually the number of public prefixes that are not repeated.
Here we need to use the inclusion exclusion principle:
Namely The specific proof is clear on baidu

Since there are only 20 sequences at most, we can enumerate one of the subsets violently, and then calculate the total number of prefixes of the current subset in all schemes by the complexity of O (| S|). Let this value be f(S)

∑S(−1)(|S|+1)f(S)
In other words, it is decided to add or subtract according to the parity of the size of the subset.
See code for specific implementation:
```#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define SF scanf
#define PF printf
#define MOD 998244353
#define MAXN 25
#define MAXL 55
using namespace std;
int n,tot,sum;
char s[MAXN][MAXL];
long long slen,ans1,pw[MAXN*MAXL];
vector<int> a;
int main(){
SF("%d",&n);
for(int i=0;i<n;i++){
SF("%s",s[i]);
int len=strlen(s[i]);
slen+=len;
}
for(int i=0;i<n;i++){
int len=strlen(s[i]);
for(int j=0;j<len;j++)
if(s[i][j]=='?')
tot++;
}
pw=1;
for(int i=1;i<=tot;i++)
pw[i]=(pw[i-1]<<1ll)%MOD;
slen=(pw[tot]*slen)%MOD;
for(int s1=1;s1<(1<<n);s1++){
int len=MAXL,pre=0;
sum=tot;
long long ans=0;
a.clear();
for(int i=0;i<n;i++)
if(s1&(1<<i))
a.push_back(i);
for(int i=0;i<a.size();i++){
int len1=strlen(s[a[i]]);
len=min(len1,len);
}
int j;
for(j=0;j<len;j++){
int flag=-1;
int nowl=0;
for(int i=0;i<a.size();i++)
if(s[a[i]][j]=='?'){
nowl++;
}
else if(s[a[i]][j]=='0'){
if(flag==1){
flag=2;
break;
}
else
flag=0;
}
else if(s[a[i]][j]=='1'){
if(flag==0){
flag=2;
break;
}
else
flag=1;
}
sum-=nowl;
if(flag==2){
break;
}
else if(flag==-1){
pre++;
}
ans=(ans+pw[pre+sum])%MOD;
}
if(a.size()%2==1)
ans1=(ans1+ans)%MOD;
else
ans1=(ans1+MOD-ans)%MOD;
}
PF("%lld",(ans1+pw[tot])%MOD);

}
```

I'm ashamed to say so.. Maybe it was because of the cold and fever, brain is not easy to use, such a simple water problem actually read for half a day.. It took a long time to understand all the answers... OIers must pay attention to their health..