Pyramid solution to a problem report

Topic Portal

[Title] effect

Pyramid as a whole rooted tree structure, the root node entries, each node is coated with a color. Starting from the inlet for the DFS robot, each through a node, it will record this node in color, to form a last sequence number of request type structure corresponds to the sequence of viable tree.

[Resolved] ideas

For sequence S, if the substring S [l ~ r] corresponding to a subtree, then S [l + 1] and S [r-1] are generated when entering and exiting. In addition, [l, r] and each of them contains a deeper subtree corresponds to a sub-problem, produces [l, r] in the passage, as well as via the roots generated between adjacent two of a character. Because [l, r] contains the number of sub-tree may be more than two, [l ~ r] for all positions and the number of division points dividing point if simple enumerated substring S, then the time complexity will be high.

Then we consider another way, considering only the substring S [l ~ r] in the first subtree by which section configuration. The first subtree enumeration division point k, so substring S [l + 1 ~ k- 1] constitute [l, r] is, S [k ~ r] configuration [l, r] the remaining portion (in other subtrees) . If k is different, then the substring S [l + 1 ~ k- 1] representing the size of the subtrees is different, it is impossible to duplicate the results of calculations occur and we can get transfer equation (f [l] [r] denotes program number): $$ if (S [l ] \ ne S [r]) \ to f [l] [r] = 0 $$

$$ if (S [l] = S [r]) \ to f [l] [r] = f [l + 1] [r-1] + \ sum_ {l + 2 \ le k \ le r-2 , S [l] = S [k]} f [l + 1] [k-1] * f [k] [r] $$ initial value: $ \ forall l \ in [1, len (S)], f [l] [l] = 1 $, the rest are zero.

Target: f [1] [len (S)]

We found that, for the program count kind of problem, usually a satisfying "addition principle" between the various decision-making states , and meet the "multiplication principle" between several sub-division of the state of each decision . When decision-making methods and design division of the state transition equations between all decisions must have a state of mutually exclusive , to ensure that will not be repeated.

【Code】

 

 1 #include<bits/stdc++.h>
 2 #define rg register
 3 #define go(i,a,b) for(rg int i=a;i<=b;i++)
 4 #define back(i,a,b) for(rg int i=a;i>=b;i--)
 5 #define ll long long
 6 #define mem(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 const int N=302;
 9 const int mod=1e9;
10 int f[N][N];
11 char S[N];
12 int work(int l,int r){
13     if(l>r||S[l]!=S[r]) return 0;
14     if(l==r) return 1;
15     if(f[l][r]!=-1) return f[l][r];
16     f[l][r]=0;
17     go(k,l+2,r)
18         f[l][r]=(f[l][r]+(ll)work(l+1,k-1)*work(k,r))%mod;
19     return f[l][r];
20 }
21 int main(){
22     scanf("%s",S);
23     int n=strlen(S);
24     mem(f,-1);
25     printf("%d\n",work(0,n-1));
26     return 0;
27 }
Code poke here

 

Guess you like

Origin www.cnblogs.com/THWZF/p/11001656.html