1444: [Jsoi2009]有趣的游戏
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1382 Solved: 498
[Submit][Status][Discuss]
Description
Input
注意 是0<=P
Output
Sample Input
Sample Output
HINT
30%的数据保证, n ≤ 2. 50%的数据保证, n ≤ 5. 100%的数据保证, n , l, m≤ 10.
Source
析:很容易列出方程,dp[i] = ∑dp[j] * pj ,所以要处理出来就需要AC自动机,然后再用Gauss 消元即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #include <numeric> #define debug() puts("++++") #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) //#define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 150000 + 10; const int maxm = 3e5 + 10; const int mod = 10007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } const int maxnode = 10 * 10 + 50; int sigma; double A[maxnode][maxnode]; double p[15]; int pos[15]; struct Aho{ int ch[maxnode][11], f[maxnode]; bool val[maxnode]; int sz; void init(){ sz = 1; ms(ch[0], 0); } inline int idx(char ch){ return ch - 'A'; } int insert(const char *s){ int u = 0; for(int i = 0; s[i]; ++i){ int c = idx(s[i]); if(!ch[u][c]){ ms(ch[sz], 0); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = 1; return u; } void getFail(){ queue<int> q; f[0] = 0; for(int c = 0; c < sigma; ++c){ int u = ch[0][c]; if(u){ q.push(u); f[u] = 0; } } while(!q.empty()){ int r = q.front(); q.pop(); for(int c = 0; c < sigma; ++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]; } } } int solve(){ for(int i = 0; i < sz; ++i){ A[i][i] += 1.; if(val[i]) continue; for(int j = 0; j < sigma; ++j){ int nxt = ch[i][j]; A[nxt][i] -= p[j]; } } return sz; } }; Aho aho; char s[20]; void Gauss(int n){ for(int i = 0; i < n; ++i){ int r = i; for(int j = i+1; j < n; ++j) if(fabs(A[j][i] > fabs(A[r][i]))) r = j; if(r != i) for(int j = 0; j <= n; ++j) swap(A[r][j], A[i][j]); for(int k = i+1; k < n; ++k){ double f = A[k][i] / A[i][i]; for(int j = i; j <= n; ++j) A[k][j] -= f * A[i][j]; } } for(int i = n-1; i >= 0; --i){ for(int j = i+1; j < n; ++j) A[i][n] -= A[j][n] * A[i][j]; A[i][n] /= A[i][i]; } } int main(){ scanf("%d %d %d", &n, &m, &sigma); for(int i = 0; i < sigma; ++i){ int x, y; scanf("%d %d", &x, &y); p[i] = x * 1. / y; } aho.init(); for(int i = 1; i <= n; ++i){ scanf("%s", s); pos[i] = aho.insert(s); } aho.getFail(); int len = aho.solve(); A[0][len] = 1.; Gauss(len); for(int i = 1; i <= n; ++i) printf("%.2f ", A[pos[i]][len] / A[pos[i]][pos[i]]); return 0; }