Bzoj 1444 [Jsoi2009] Fun Games

Source: Internet
Author: User

Chain of title:

http://www.lydsy.com/JudgeOnline/problem.php?id=1444
1:

probability dp, matrix multiplication, fast power.
set up AC automata for all strings,
then if the node on the trie tree is transferred to a labeled node, it means that the tag corresponds to the person to win.
(as the problem is clearly stated that the string length is the same, the strings are different, so it is shown that the entire trie tree after the establishment of an AC automaton only n labeled nodes, and there will be no node can not transfer problems.) )
then establish a transfer matrix of trie.sizextrie.size size trans, where trans (i,j) represents the probability of the I node being transferred to the J node:
Initial matrix:
if (Trie.tag[i]) trans (i,i) =1;
else Trans (I,trie.ch[i][c]) +=p[c] (enumerate the next character C)
at this point each position of the matrix (i,j) indicates the probability of stepping from I to J.
then the matrix is squared many times, you can get each position representation (I,J) from I walk many many times to the probability of J.
so the answer is trans (1,i). (I is the node that tagged the tag): The probability of going a lot of times from the initial position to the end of a string.
Since the data is small and the matrix is transferred many times, the probability of the matrix being very close to the theoretical probability value can be directly regarded as the answer.
complexity ((NL) ^3LOGP) (Transfer p-secondary matrix, I set the transfer 23336666233336666ll multiple)


Code. 1:

#include <bits/stdc++.h> #define MAXN 15using namespace Std;int n,m,l;int pos[maxn];d ouble p[maxn];struct matrix{ int r,c;double a[maxn*maxn][maxn*maxn];void Reset (int _r,int _c) {r=_r; c=_c; memset (A,0,sizeof (a));} void Identity () {for (int i=1;i<=r;i++) a[i][i]=1;} Matrix operator * (const matrix &AMP;RTM) Const{matrix now; Reset (R,RTM.C); for (int i=1;i<=now.r;i++) for (int. j=1;j<=now.c;j++) for (int k=1;k<=c;k++) now.a[i][j]+=a[i][ K]*rtm.a[k][j];return now;} Matrix operator ^ (Long long B) Const{matrix now,base; Base=*this;now. Reset (R,C); Now. Identity (); for (; b;base=base*base,b>>=1) if (b&1) Now=now*base;return now;}; struct Trie{int size,p;int CH[MAXN*MAXN][MAXN],TAG[MAXN*MAXN]; Trie (): Size (1) {}void Insert (char *s) {static int cnt; p=1;for (int i=0;i<l;i++) {int c=s[i]-' A '; if (!ch[p][c]) ch[p][c] =++SIZE;P=CH[P][C];} Tag[p]=1; Pos[++cnt]=p;}} T;struct acam{int fail[maxn*maxn];void Build () {static queue<int>q; Q.push (1); Fail[1]=0;while (! Q.empty ()) {int U=q.front (); Q.poP (); t.tag[u]|=t.tag[fail[u]];for (int c=0;c<m;c++) {int k=fail[u];if (! T.ch[u][c]) {t.ch[u][c]=k? T.ch[k][c]:1;continue;} while (k&&! T.CH[K][C]) k=fail[k];fail[t.ch[u][c]]=k? T.ch[k][c]:1; Q.push (T.ch[u][c]);}}} A;int Main () {Matrix trans;static char s[maxn];ios::sync_with_stdio (0); cin>>n>>l>>m;for (int i=0,a, b;i<m;i++) cin>>a>>b,p[i]=1.0*a/b;for (int i=1;i<=n;i++) Cin>>s,t.insert (S); A.build (); trans. Reset (t.size,t.size); for (int. i=1;i<=t.size;i++) {if (T.tag[i]) trans.a[i][i]=1;else for (int c=0;c<m;c++) TRANS.A[I][T.CH[I][C]]+=P[C];} Trans=trans^23336666233336666ll;cout<<fixed<<setprecision (2); for (int i=1;i<=n;i++) cout<< Trans.a[1][pos[i]]<<endl;return 0;}

  

2:

Expected DP, Gaussian elimination
To create AC automata for all strings, the problem is turned into a topic like bzoj_3143_[hnoi2013] walk.
Dp[i] Indicates the expected number of nodes I have passed through the trie tree, Pro[j][i] represents the probability of transfer from J to I point.
Then we can list the following transfer equations:
$ $DP [i]=\sum_{j->i}{dp[j]*pro[j][i]}$$
In particular:
1. When j is a trie tree is a node that is tagged with a tag, it cannot be transferred to another node
2. When I is node 1th, add a value of 1 to indicate that you have just started to expect it.
The transfer of the above equation exists in the ring, which requires Gaussian elimination.

The end game is no longer transferred because the node with tag tag is reached, so the number of times to reach all the tag nodes is 1
In other words, the expectation of each tag node is equal to the probability of the person winning the corresponding node.

Complexity O ((NL) ³)

Code. 2:

#include <bits/stdc++.h> #define MAXN 15using namespace Std;const double eps=1e-8;int n,m,l,fail;int ID[MAXN]; Double A[MAXN*MAXN][MAXN*MAXN],G[MAXN],DP[MAXN*MAXN];d ouble *a[maxn*maxn];int dcmp (double x) {if (Fabs (x) <eps) return 0;return x>0?1:-1;} struct Acam{int size;int CH[MAXN*MAXN][MAXN],TAG[MAXN*MAXN],FAIL[MAXN*MAXN]; ACAM (): Size (1) {}void Insert (char *s) {static int p,cnt; p=1; bool Fg=0;for (int i=0;i<l;i++) {int c=s[i]-' A '; if (!ch[p][ C]) ch[p][c]=++size;p=ch[p][c];} Tag[p]=1; Id[++cnt]=p;} void Build () {static queue<int>q; Q.push (1); Fail[1]=0;while (! Q.empty ()) {int U=q.front (); Q.pop (); tag[u]|=tag[fail[u]];for (int c=0;c<m;c++) {int k=fail[u];if (!ch[u][c]) {ch[u][c]=k?ch[k][c]:1;continue;} while (K&&!ch[k][c]) k=fail[k];fail[ch[u][c]]=k?ch[k][c]:1; Q.push (Ch[u][c]);}}} Ds;void buildequation () {for (int i=1;i<=ds.size;i++) if (! Ds.tag[i]) for (int c=0;c<m;c++) a[ds.ch[i][c]][i]+=g[c];for (int i=1;i<=ds.size;i++) a[i][i]+=-1;a[1][ds.size+ 1]+=-1;for (int i=1;i<=ds.size;i++) a[i]=a[i];} void gausselimination (int pos,int i) {if (pos==ds.size+1| | i==ds.size+1) return;for (int j=pos;j<=ds.size;j++) if (dcmp (A[j][i])!=0) {swap (A[j],a[pos]); if (dcmp (A[pos][i])!=0) for (int j=pos+1;j<=ds.size;j++) {double k=a[j][i]/a[pos][i];for (int l=i;l<=ds.size+1;l + +) A[j][l]-=k*a[pos][l];} Gausselimination (pos+ (dcmp (a[pos][i))!=0), i+1), if (dcmp (a[pos][i))!=0) {for (int l=i+1;l<=ds.size;l++) dp[i]+=A[ POS][L]*DP[L];DP [i]=a[pos][ds.size+1]-dp[i];DP [i]=dp[i]/a[pos][i];}} int main () {static char s[15];ios::sync_with_stdio (0); cin>>n>>l>>m;for (int i=0,p,q;i<m;i++) CIN >>p>>q,g[i]=1.0*p/q;for (int i=1;i<=n;i++) Cin>>s,ds. Insert (S);D s.build (); Buildequation (); Gausselimination (Cout<<fixed<<setprecision); (2); for (int i=1;i<=n;i++) Cout<<fabs (dp[id[i ]]) <<endl;return 0;}

  

Bzoj 1444 [Jsoi2009] Fun Games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.