Mountain subsequences Title Description
Coco is a beautiful acmer girl living in a very beautiful mountain. There is many trees and flowers on the mountain, and there is many animals and birds also. Coco like the mountain so much, she now name some letter sequences as Mountain subsequences.
A Mountain subsequence is defined as following:
1. If the length of the subsequence is N, there should are a max value letter, and the subsequence should like this, A1 < ; ...< ai < ai+1 < Amax > AJ > Aj+1 > ... > an
2. It should has at least 3 elements, and in the "left" of the max value letter there should has at least one element, the Same as in the right.
3. The value of the letter is the ASCII value.
Given a letter sequence, Coco wants to know how many Mountain subsequences exist.
Enter input contains multiple test cases.
There is a number n (1<= n <= 100000) which means the length of the letter sequence in the first line, And the next line contains the letter sequence.
Please note this letter sequence only contain lowercase letters.
Output for each case please output the number of the Mountain subsequences Module 2012. Sample input
4abca
Sample output
4
Tip the 4 mountain subsequences is:
ABA, ACA, BCA, ABCA
Source 2013 The first ACM University student Program Design competition in Shandong Province
1#include <iostream>2#include <cstring>3 #defineMAXN 1000054 #defineMoD 20125 using namespacestd;6 CharSTR[MAXN];7 intdp[ -],DL[MAXN],DR[MAXN],S[MAXN];8 intMain ()9 {Ten intN; One while(cin>>N) A { -Cin>>str; - for(intI=0; i<n;i++) thes[i]=str[i]-'a'; -Memset (DP,0,sizeof(DP)); -memset (DL,0,sizeof(DL)); -memset (DR,0,sizeof(DR)); + for(intI=0; i<n;i++) - { + for(intj=0; j<s[i];j++) ADl[i]= (Dl[i]+dp[j])%mod;//count the number of the first I satisfies the condition less than (S[i]) atDp[s[i]]= (dp[s[i]]+dl[i]+1)%mod;//Add the current number of satisfied conditions to prepare for the next - } -Memset (DP,0,sizeof(DP)); - for(inti=n-1; i>=0; i--) - { - for(intj=0; j<s[i];j++) inDr[i]= (Dr[i]+dp[j])%MoD; -Dp[s[i]]= (dp[s[i]]+dr[i]+1)%MoD; to } + intans=0; - for(intI=0; i<n;i++) theAns= (Ans+dl[i]*dr[i])%MoD; *cout<<ans<<Endl; $ }Panax Notoginseng return 0; -}
Sdut Mountain subsequences 2013 the first ACM College student Program Design contest in Shandong Province