Test instructions
Give a string of length n, with only lowercase letters.
Ask you how many of Yamagata's sub-sequences are, here is the subsequence is not a substring.
The so-called Yamagata is strictly ascending to descending.
Ideas:
Dp[i][j][k] represents the first I letter, the front letter is the K state of J how many kinds.
Status 0 Why not, 1 for the mountain, 2 for the mountain, 3 for the answer state.
Rolling array maintenance is fine.
Code:
#include "cstdlib" #include "Cstdio" #include "CString" #include "Cmath" #include "queue" #include "algorithm" #include " iostream "#include" map "#include" vector "#define LL long longusing namespace Std;char v[123456];int dp[2][28][5];int main () {int n; while (scanf ("%d", &n)!=-1) {memset (dp,0,sizeof (DP)); scanf ("%s", v+1); Dp[0][0][0]=1; for (int i=1, i<=n; i++) {for (int j=0; j<=26; j + +) for (int k=0; k<4; k++) dp[i%2][j][k]=dp[1-i% 2][J][K]; for (int j=0; j<=26; J + +) {int tep=v[i]-' a ' +1; if (tep>j) {dp[i%2][tep][1]= (dp[i%2][tep][1]+dp[1-i%2][j][1]+dp[1-i%2][j][0])%2012; Dp[i%2][tep][2]= (dp[i%2][tep][2]+dp[1-i%2][j][1])%2012; } else if (tep<j) {dp[i%2][tep][2]= (dp[i%2][tep][2]+dp[1-i%2][j][2])%2 012; Dp[i%2][tep][3]= (dp[i%2][tep][3]+dp[1-i%2][j][2])%2012; }}} int ans=0; for (int i=0; i<=26; i++) ans= (ans+dp[n%2][i][3])%2012; printf ("%d\n", ans); } return 0;}
[DP] UPCPJ 2221 Mountain subsequences