ch_5302 金字塔
区间dp,递归写法,
#include<iostream> #include<cstdio> #define ri register int #define u long long namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } return x*f; } } using opt::in; #define NN 305 #define MO 1000000000 #include<algorithm> #include<cstring> namespace mainstay { char s[NN]; u f[NN][NN]; u dfs(const u &l,const u &r) { if(s[l]!=s[r]) return 0; if(f[l][r]!=-1) return f[l][r]; if(l==r) return 1; if(l>r) return 0; f[l][r]=0; for(ri i(l+2); i<=r; ++i) f[l][r]=(f[l][r]+dfs(l+1,i-1)*dfs(i,r)%MO)%MO; return f[l][r]; } inline void solve() { scanf("%s",s); memset(f,-1,sizeof(f)); u len(std::strlen(s)); std::cout<<dfs(0,len-1); } } int main() { //freopen("x.txt","r",stdin); //freopen("my.txt","w",stdout); std::ios::sync_with_stdio(false); mainstay::solve(); }