Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5595
Test instructions: give n a string containing ' * ', '. ', ' ^ ', '! ' These four operators, along with lowercase letters (representing vectors) and numbers.
All four operators have different arithmetic rules. Ask how many different formulas (not illegal operations) can be formed after the parentheses are added.
Idea: Simple interval DP, which regards an operator as a bit.
DP[I][J][1] Indicates how many of the cases where the result of the I-j bitwise operation is a number.
Dp[i][j][0] Indicates the number of cases where the result of the I-j bitwise operation is a vector.
Then the interval DP is selected, and the two sides are either numbers or vectors according to the middle operator to avoid illegal situations.
1#include <bits/stdc++.h>2 using namespacestd;3 intT;4 strings;5 Long Longdp[ $][ $][2];6 Charop[ the];7 intmark[ the];8 Const Long LongMoD =1000000000+7;9 intMain ()Ten { Onescanf"%d", &T); A while(t--) - { -Cin>>s; the BOOLFOP =false; - intCntop =0, cnt =0; - for(inti =0; I < s.size (); i++) - { + if(i = =0) - { + if(s[0] >='0'&& s[0] <='9') mark[++cnt] =1; A Else if(s[0] >='a'&& s[0] <='Z') mark[++cnt] =0; at } - Else - { - if(S[i] = ='^'|| S[i] = ='!'|| S[i] = ='*'|| S[i] = ='.') - { -FOP =true; inOp[++cntop] =S[i]; - } to Else + { - if(FOP = =true) the { * if(S[i] >='0'&& S[i] <='9') mark[++cnt] =1; $ Else if(S[i] >='a'&& S[i] <='Z') mark[++cnt] =0; Panax NotoginsengFOP =false; - } the Else Continue; + } A } the + } -Memset (DP,0,sizeof(DP)); $ for(inti =1; I <= CNT; i++) $ { - if(Mark[i] = =0) dp[i][i][0] =1; - Else if(Mark[i] = =1) dp[i][i][1] =1; the } - Wuyi for(intLen =2; Len <= CNT; len++) the { - for(inti =1; I <= cnt-len+1; i++) Wu { - intj = i+len-1; About for(intK = i; K < J; k++) $ { - if(Op[k] = ='*') - { -dp[i][j][0] + = (dp[i][k][1]*dp[k+1][j][0]%mod+dp[i][k][0]*dp[k+1][j][1]%MoD); Adp[i][j][0] %=MoD; +dp[i][j][1] + = dp[i][k][1]*dp[k+1][j][1]%MoD; thedp[i][j][1] %=MoD; - } $ Else if(Op[k] = ='^') the { thedp[i][j][0] + = dp[i][k][0]*dp[k+1][j][0]%MoD; thedp[i][j][0] %=MoD; the } - Else if(Op[k] = ='.') in { thedp[i][j][1] + = dp[i][k][0]*dp[k+1][j][0]%MoD; thedp[i][j][1] %=MoD; About } the Else if(Op[k] = ='!') the { thedp[i][j][1] + = dp[i][k][0]*dp[k+1][j][0]%MoD; +dp[i][j][1] %=MoD; -dp[i][j][0] + = dp[i][k][0]*dp[k+1][j][1]%mod + dp[i][k][1]*dp[k+1][j][0]%MoD the+ dp[i][k][1]*dp[k+1][j][1]%MoD;Bayidp[i][j][0] %=MoD; the } the } - } - } the Long LongAns = (dp[1][cnt][0] + dp[1][cnt][1])%MoD; theprintf"%lld\n", ans); the the - } the return 0; the}
ZOJ 3892 Available computation Sequence interval DP