16.1113 Mock Exam T2

Source: Internet
Author: User

test question #4 parentheses
brackets
"Problem description"
there is a sequence of parentheses of length, and a different parenthesis. Where is the sequence at each position?
The seed brackets are random, and the probabilities of each left and right parenthesis are known to occur at each location. To find the entire sequence is
the probability of a valid parenthesis sequence.
we define the legal bracket sequence as follows:
An empty sequence is a sequence of valid parentheses;
if it is a valid bracket sequence, then??? Is the legal parenthesis sequence, when and only if the and? are the same
left and right brackets;
? If and? is the legal bracket sequence, then?? Is the legal parenthesis sequence.
"input Format"
Enter the first line containing two integers?. The next input is divided into groups, each group of rows. The first group
The line contains two real numbers? [?,?] And? [?,?], representing the opening and closing brackets of the first class, respectively, on the first position.
the probability.
"Output format"
The output line contains a real number that represents the probability that the sequence is a valid bracket sequence. It is recommended to keep at least
5-bit fractional output. Only if the absolute error between your output and the standard answer is not more than 10? 5 o'clock
was judged to be correct.
"Sample input 1"
2 1
1.00000 0.00000
0.00000 1.00000
"Sample Output 1"
1.00000
"Sample input 2"
4 1
0.50000 0.50000
1.00000 0.00000
0.00000 1.00000
0.50000 0.50000
test question #4 parentheses
"Sample Output 2"
0.25000
"Data size and conventions"
for 20% of the data, ≤50,? = 1, the probability of all positions is 0 or 1.
another 30% of the data, ≤34,? = 1, the first 10 and the last 10 positions all probabilities are
is 0.5, the probability of the remaining position in the middle is not 0 or 1.
80% of the data,?,? ≤50.
for 100% of data, 1≤?≤200,1≤?≤50.

1 /*maintaining two arrays is a good solution to the problem of repeated computations*/2#include <iostream>3#include <cstdio>4 #defineLD long Double5 #defineMAXN 2106 using namespacestd;7 intn,k;8LD f[maxn][maxn],ff[maxn][maxn],g[maxn][maxn][2];9 intMain () {TenFreopen ("brackets.in","R", stdin); OneFreopen ("Brackets.out","W", stdout); ACin>>n>>K; -      for(intI=1; i<=n;i++) -          for(intj=1; j<=k;j++) thecin>>g[i][j][0]>>g[i][j][1]; -      for(intI=1; i<n;i++) -          for(intx=1; x<=k;x++) -f[i][i+1]+=g[i][x][0]*g[i+1][x][1]; +      for(inti=n-1; i>=1; i--) -          for(intj=i+3; j<=n;j++){ +              for(intx=1; x<=k;x++) Af[i][j]+= (f[i+1][j-1]+ff[i+1][j-1]) *g[i][x][0]*g[j][x][1];//a bracket on both ends at              for(intx=i+1; x<j-1; x + +) -ff[i][j]+= (F[i][x]+ff[i][x]) *f[x+1][j]; -         } -printf"%.5f\n",(Double) (f[1][n]+ff[1][n])); -     return 0; -}

Idea: For three consecutive identical brackets, the enumeration repeats, we define the x from this position is the F array to be introduced, the front does not matter (introduced by the FF Array)

16.1113 Mock Exam T2

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.