CH 5302 Pyramid (section DP)

CH 5302 Pyramid



\(solution:\)

Amazing one subject, who saw thought it was a palindrome substring find string of problems. But the data range is very small, and only know a palindrome string is not good to do. But we have to be observed, if the depth of the search is convenient, then it passes through the sub-tree should be arranged in order from left to right, so we think linear DP. Then we found seems to be alone seek a certain period of consolidation then they traverse the way, this is a sign of the interval DP ah! So we consider setting \ (F [l] [r ] \) represents from \ (l \) into the first \ (r \) characters may be some sub-tree traversal scheme (note that since it is a sub-tree traversal program, then \ (l \) character and $ r $ of the same character, as they have said is the root of Fengyun subtree). But we need to ensure that the transfer of about computing do not leak case.

It is important here :( possible codes good code, but the principle we can not gloss over!) Based on the experience we're looking for a reference point to ensure that do not leak, and regular routine is to enumerate a segment \ (k \) ( Note that our \ (k \) character to character and both ends of the same! needed because the current sub-tree root!) so that \ ([l, k] \ ) is the first one sub-tree (sub-tree Fengyun attention at this time root still attached) and \ ([k, r] \ ) this paragraph is another way to navigate whatever, so we need to ensure that \ ([l, k] \ ) is a sub-tree (as it is now still attached root), it can not be classified into a plurality of pieces attached to the root of the subtree, so we make the side force even \ ([l + 1, k -1] \) is not even a real edge subtree, compulsory side is connected \ (e (l, l + 1) \) or \ (E (R & lt, R & lt-. 1) \) . So we need to ensure that \ (l + 1 \) character and \ (k-1 \) consistent character (as they have said is the root of Fengyun subtree). Then we can transfer up!

\(F[l][r]=\sum^{k\leq r}_{k=l+2} \{f[l+1][k-1]*f[k][r] \}_{条件:s[l+1]==s[k-1],s[k]==s[r]}\)



\(code:\)

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>

#define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int

using namespace std;

const int mod=1e9;

int n;
int f[305][305];
string s;

inline int qr(){
    register char ch; register bool sign=0; rg res=0;
    while(!isdigit(ch=getchar())) if(ch=='-')sign=1;
    while(isdigit(ch)) res=res*10+(ch^48),ch=getchar();
    return sign?-res:res;
}

inline int solve(int l,int r){
    if(l==r)return 1;
    if(f[l][r]>=0)return f[l][r];
    else f[l][r]=0;
    for(rg k=l+2;k<=r;++k)
        if(s[l+1]==s[k-1]&&s[k]==s[r])
            f[l][r]=(f[l][r]+(ll)solve(l+1,k-1)*solve(k,r)%mod)%mod;
    return f[l][r];
}

int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    cin>>s; n=s.size(); s=' '+s;
    memset(f,-1,sizeof(f));
    printf("%d\n",solve(1,n));
    return 0;
}

Guess you like

Origin www.cnblogs.com/812-xiao-wen/p/11014118.html