Mountain Subsequences
Time limit:1000ms Memory limit:65536k
Topic Description
Coco is a beautiful acmer girl living in a very beautiful. There are many trees and flowers on the mountain, and There are many and animals birds. 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 being a max value letter, and the subsequence should as this, A1 ; ...< ai < ai+1 < Amax > AJ > Aj+1 > ... > an
2. It should have at least 3 elements, with the left of the max value letter there should have at least one element, the Same as in the right.
3. The value of the * is the ASCII value.
Given a letter sequence and Coco wants to know how many mountain. input Input contains multiple test cases.
For each case there are a number n (1<= n <= 100000) which means the length of the letter sequence in the 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
TipsThe 4 mountain subsequences are:
ABA, ACA, BCA, ABCA source 2013 Shandong four ACM undergraduate Program design Contest Sample program
To find the number of convex sub sequences
I'm going to have to ask each point to the current number of ascending subsequence.
To find the number of descending subsequence of each point for the beginning to the end
The last multiplication is the answer.
Accode:
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring>
; #include <stdlib.h> #include <iostream> #include <algorithm> #define MAXN 100100 #define MOD 201
2 using namespace Std;
int LOW[MAXN],BIG[MAXN],A[MAXN],NUM[MAXN];
Char MAPP[MAXN];
void Init () {memset (low,0,sizeof (Low));
memset (big,0,sizeof (big));
memset (num,0,sizeof (num));
int main () {int n,ans;
while (~SCANF ("%d", &n)) {init ();
scanf ("%s", &mapp);
for (int i=0;i<n;++i) a[i]=mapp[i]-' a ';
for (int i=0;i<n;++i) {for (int j=0;j<a[i];++j) low[i]= (low[i]+num[j))%mod;
num[a[i]]= (num[a[i]]+low[i]+1)%mod;
}/*for (int i=0;i<n;++i) cout<<num[i]<< ';
cout<< ' \12 '; * * memset (num,0,sizeof (num)); for (int i=n-1;i>=0;--i) {for (int j=0;J<A[I];++J) big[i]= (big[i]+num[j])%mod;
num[a[i]]= (num[a[i]]+big[i]+1)%mod;
}/* for (int i=0;i<n;++i) cout<<num[i]<< ';
cout<< ' \12 ';
for (int i=0;i<n;++i) cout<<low[i]<< "";
cout<< ' \12 ';
for (int i=0;i<n;++i) cout<<big[i]<< ';
cout<< ' \12 '; * * ans=0;
for (int i=0;i<n;++i) ans= (ans+big[i]*low[i])%mod;
printf ("%d\n", ans);
return 0; }