题目链接:点击打开链接
题目大意:给出从根节点遍历到所有叶子节点,每个子节点对父节点来说概率相同,计算最终得到的结果的期望
给出n个字符串,也就可以得到n个方程,用高斯消元解出结果,注意,可能存在不能被计算出来的值,要注意判断,判断方式,从1到n找到第一个Map[i][j]不为0的值,也就是当前方程要求的值,如果从j向后存在Map[i][k]不为0,并且ans[k]还没有被计算出来,那么这个ans[j]也不能被解出来。
注意:可能输出-0.000,这是错误的
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std ; #pragma comment(linker,"/STACK:102400000,102400000") #define eqs 1e-8 struct node{ double a[26] ; double k ; }; int n , len , id , vis[26] ; double Map[26][26] , a[26] , ans[26] ; char str[1000000] , s[30] ; node dfs() { int i , k = 0 , flag = 0 , num = 0 ; char ch ; node p , q ; p.k = 0 ; memset(p.a,0,sizeof(p.a)) ; while( ch = str[id++] ) { if( ch == ' ' ) continue ; else if( ch == '(' ) { q = dfs() ; for(i = 0 ; i < n ; i++) p.a[i] += q.a[i] ; p.k += q.k ; num++ ; } else if( ch == '-' ) { flag = 1 ; } else if( ch >= '0' && ch <= '9' ) { k = k*10 + ch - '0' ; if( str[id] >= '0' && str[id] <= '9' ) continue ; if( flag ) k = -k ; flag = 0 ; p.k += k ; k = 0 ; num++ ; } else if( ch >= 'a' && ch <= 'z' ) { p.a[ ch-'a' ] += 1.0 ; num++ ; } else if( ch == ')' ) { for(i = 0 ; i < n ; i++) p.a[i] /= (num*1.0) ; p.k /= (num*1.0) ; return p ; } } } void solve() { int i , j , k , l , num = 0 ; double temp ; for(i = 0 , k = 0 ; i < n && k < n ; i++ , k++) { for(j = i ; j < n ; j++) if( fabs(Map[j][k]) > eqs ) break ; if( j >= n ) { i-- ; continue ; } if( i != j ) { for(l = k ; l < n ; l++) swap(Map[i][l],Map[j][l]) ; swap(a[i],a[j]) ; } for(j = i+1 ; j < n ; j++) { if( fabs(Map[j][k]) < eqs ) continue ; temp = Map[j][k]/Map[i][k] ; for(l = k ; l < n ; l++) Map[j][l] = Map[i][l]*temp - Map[j][l] ; a[j] = a[i]*temp - a[j] ; } num++ ; } memset(ans,0,sizeof(ans)) ; memset(vis,0,sizeof(vis)) ; for(i = n-1 ; i >= 0 ; i--) { for(k = 0 ; k < n ; k++) if( fabs(Map[i][k]) > eqs ) break ; if( k >= n ) continue ; for(j = k+1 ; j < n ; j++) { if( !vis[j] && fabs(Map[i][j]) > eqs ) break ; } if( j < n ) continue ; for(j = k+1 ; j < n ; j++) { a[i] -= ans[j]*Map[i][j] ; } ans[k] = a[i] / Map[i][k] ; if( fabs(ans[k]) < eqs ) ans[k] = 0.0 ; vis[k] = 1 ; } for(i = 0 ; i < n ; i++) { if( !vis[i] ) printf("Expected score for %c undefined\n", 'a'+i) ; else printf("Expected score for %c = %.3f\n", 'a'+i, ans[i]) ; } printf("\n") ; } int main() { int i , j , step = 0 ; char ch , k ; node p ; //freopen("1.in","r",stdin) ; //freopen("2.out","w",stdout) ; while( scanf("%d", &n) && n ) { getchar() ; for(i = 0 ; i < n ; i++) { gets(str) ; id = 5 ; p = dfs() ; for(j = 0 ; j < n ; j++) Map[i][j] = p.a[j] ; Map[i][ str[0]-'a' ] -= 1.0 ; a[i] = -p.k ; } printf("Game %d\n", ++step) ; solve() ; } return 0 ; }
数据:
1
a = ((1 7) 6 ((8 3) 4))
2
a = (1 b)
b = (4 a)
1
a = (a a a)
3
a = (1 b c)
b = (a 2 c)
c = (a b 3)
5
a = (e (15 d (57 a) (45 (75 c))) b 189)
b = (d (a -29 30) ((b c)) ((51 a) (a 67) (e 79) (e a)))
c = (((d)))
d = (-1 (-98) ((a) c) (d e e (-81) ((a))) 58 (b))
e = (a b c d a b c d a b c d a b c d a b c d a b c d)
2
a = (1 (2 (3 (4 (5 (6 (7 (8 (9 (10 (11 (12 (13 (14 (15 b)))))))))))))))
b = (b b)
5
a = ((((((((((((((a))))))))))))))
b = (13 17 18 b d)
c = (a b c d)
d = (b 8 (d 7))
e = ((e) ((7)) (((((e))))))
7
a = ((4 5) 2 ((1 3) 7))
b = (9)
c = ((((((((((((((((((((((((((((((((((((99))))))))))))))))))))))))))))))))))))
d = ((1)(1)(1))
e = (1 (2 (3 (4 (5 (6 (7 (8 (9 (10 20))))))))))
f = (-1 1)
g = (-1 -2 -3 -4 -5 -6 1998 -1111)
2
a = ( 17 ( 18 b ))
b = ( -19 a ( 8 a ) ( 4 9))
26
a = ((-17 a) (z (z 3)))
b = (b 8 c g i h j)
c = ((a c m) (p r o g r a m m i n g) (c o n t e s t))
d = (1 6 (1 6 (1 6 (1 6 d d))))
e = ((((f)) g) h)
f = (17 18 19 (20 21))
g = (((((7 (n) 8))) 9 b d))
h = (((((((1 2 3 4 5 6 7 8) (8 7 6 5 4 3 2 1)))))))
i = (((((h a c)))))
j = ((1) (11) (1998))
k = ((((((((k l))))))))
l = (l k)
m = (1 9 2 0 -1)
n = (0 e)
o = (o h n o (8))
p = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 p p p p p p p p p p p p p p)
q = ((((((((w))))))))
r = (m c m)
s = (1 p 1 p 1 p 1 p s s s s s s s o m)
t = (a d f e g t s r p)
u = (u v w x y z)
v = (a a a a v v v v)
w = ( w y w y )
x = ((2) m o r e)
y = (q)
z = (1 -9 9 -8)
0
输出
Game 1
Expected score for a = 4.917
Game 2
Expected score for a = 2.000
Expected score for b = 3.000
Game 3
Expected score for a undefined
Game 4
Expected score for a = 1.750
Expected score for b = 2.000
Expected score for c = 2.250
Game 5
Expected score for a = 67.521
Expected score for b = 24.617
Expected score for c = 4.685
Expected score for d = 4.685
Expected score for e = 25.377
Game 6
Expected score for a undefined
Expected score for b undefined
Game 7
Expected score for a undefined
Expected score for b = 14.611
Expected score for c undefined
Expected score for d = 10.444
Expected score for e = 7.000
Game 8
Expected score for a = 3.667
Expected score for b = 9.000
Expected score for c = 99.000
Expected score for d = 1.000
Expected score for e = 2.008
Expected score for f = 0.000
Expected score for g = 108.250
Game 9
Expected score for a = 13.759
Expected score for b = 3.034
Game 10
Expected score for a = -6.042
Expected score for b = 120.778
Expected score for c = 5.593
Expected score for d = 3.500
Expected score for e = 15.712
Expected score for f = 18.625
Expected score for g = 35.224
Expected score for h = 4.500
Expected score for i = 1.350
Expected score for j = 670.000
Expected score for k undefined
Expected score for l undefined
Expected score for m = 2.200
Expected score for n = 7.856
Expected score for o = 6.785
Expected score for p = 0.000
Expected score for q undefined
Expected score for r = 3.331
Expected score for s = 1.299
Expected score for t = 8.956
Expected score for u undefined
Expected score for v = -6.042
Expected score for w undefined
Expected score for x = 6.006
Expected score for y undefined
Expected score for z = -1.750
版权声明:本文为博主原创文章,未经博主允许不得转载。