AC自动机+概率
Given a set of pattern strings, and a text, you have to find, if any of the pattern is a substring of the text. If any of the pattern string can be found in text, then print “yes”, otherwise “no” (without quotes).
But, unfortunately, that’s not what is asked here. J
The problem described above, requires a input file generator. The generator generates a text of length L, by choosing L characters randomly. Probability of choosing each character is given as priori, and independent of choosing others.
Now, given a set of patterns, calculate the probability of a valid program generating “no”.
First line contains an integer T, the number of test cases. Each case starts with an integer K, the number of pattern strings. Next K lines each contain a pattern string, followed by an integer N, number of valid characters. Next N lines each contain a character and the probability of selecting that character, pi. Next an integer L, the length of the string generated. The generated text can consist of only the valid characters, given above.
There will be a blank line after each test case.
For each test case, output the number of test case, and the probability of getting a “no”.
Constraints
· T ≤ 50
· K ≤ 20
· Length of each pattern string is between 1 and 20
· Each pattern string consists of only alphanumeric characters (‘a’ to ‘z’, ‘A’ to ‘Z’,’0’ to ‘9’)
· Valid characters are all alphanumeric characters
· ∑pi = 1
· L ≤ 100
2 1 a 2 a 0.5 b 0.5 2
2 ab ab 2 a 0.2 b 0.8 2 |
Case #1: 0.250000 Case #2: 0.840000 |
#include<iostream> #include<cstring> #include<cstdio> using namespace std; #define prt(k) cout<<#k"="<<k<<endl; #define ll long long #include<queue> const int N=1e5+8; const int SIZE=133; #include<map> int match[N]; double d[2222][111],vis[2222][333]; double prob[333]; int n,k; map<char,int> mp; int ch[N][SIZE]; int sz; void init() { sz=1; memset(ch[0],0,sizeof ch[0]); memset(match,0,sizeof match); } int f[N]; void insert(char* s) { int u=0,n=strlen(s);//s.size(); for(int i=0; i<n; i++) { int c= mp[s[i]]; if(!ch[u][c]) { memset(ch[sz],0,sizeof ch[sz]); match[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } match[u]=1; } int getfail() { queue<int> q; f[0]=0; for(int c=0; c<SIZE; c++) { int u=ch[0][c]; if(u) { f[u]=0; q.push(u); } } while(!q.empty()) { int r=q.front(); q.pop(); for(int c=0; c<SIZE; c++) { int u=ch[r][c]; if(!u) { ch[r][c]=ch[f[r]][c]; continue; } q.push(u); int v=f[r]; while(v&&!ch[v][c])v=f[v]; f[u]=ch[v][c]; match[u]|=match[f[u]]; } } } double solve(int u,int L) { if(L==0) return 1; if(vis[u][L]) return d[u][L]; vis[u][L]=1; double &ans=d[u][L]; ans=0; for(int i=0;i<n;i++) if(!match[ch[u][i]]) ans+=prob[i]*solve(ch[u][i],L-1); return ans; } int main() { int _; cin>>_; int ca=1; while(_--) { cin>>k; mp.clear(); char s[111][111]; init(); for(int i=1;i<=k;i++) { scanf("%s",s[i]); } cin>>n; for(int i=0;i<n;i++) { char t[7]; double a; cin>>t>>a; mp[t[0]]=i; prob[i]=a; } int L; init(); for(int i=1;i<=k;i++) insert(s[i]); cin>>L; getfail(); memset(vis,0,sizeof vis); double ans=solve(0,L); printf("Case #%d: %.6lf\n",ca++,ans); } }