Ultraviolet A 11468 Ac automation + dp
Give k mode strings, and then randomly generate a string with a length of L. The probability of each character being selected is pi. The constructed string does not contain the probability of any mode string.
Analysis: it is clear that this is the matching of the parent string of a pattern string. It is clear that an AC automatic machine needs to be built first. We use dp [I] [j] to indicate the probability that the I character is being constructed, and the fail pointer can be successfully constructed on the j node. Then we can follow the status following the fail pointer back. Note that only the valid status can be extended, that is, the status of any mode string is not included. That is
Dp [I] [j]-> dp [I + 1] [ch [j] [k]; ch [j] [k] indicates the next state if k characters are selected.
VIEW CODE
#include
#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int mmax=2100;struct AC_trie{ double dp[110][mmax]; int ch[mmax][63]; int cnt[mmax],Fail[mmax],match[mmax]; int num,k,L,n; char patter[30]; double p[70]; int get_id(char c) { if('0'<=c && c<='9') return c-'0'; if('A'<=c && c<='Z') return c-'A'+10; if('a'<=c && c<='z') return c-'a'+36; return 0; } void init() { memset(ch,0,sizeof ch); memset(match,0,sizeof match); memset(cnt,0,sizeof cnt); cnt[0]=0; num=0; memset(p,0,sizeof p); memset(dp,0,sizeof dp); } void read(int n) { for(int i=0;i
q; Fail[0]=0; for(int i=0;i<62;i++) { int u=ch[0][i]; if(u) { q.push(u); Fail[u]=0; } } while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0;i<62;i++) { int u=ch[x][i]; if(!u) { ch[x][i]=ch[Fail[x]][i]; continue; } q.push(u); int v=Fail[x]; while(v && !ch[v][i]) v=Fail[v]; Fail[u]=ch[v][i]; match[u] |=match[Fail[u]]; } } } void work() { init(); scanf("%d",&k); read(k); scanf("%d",&n); for(int i=0;i
>t; while(t--) { printf("Case #%d: ",++ca); Ac.work(); } return 0;}