sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

Mountain Subsequences

题目描述

Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees and flowers on the mountain, and there are many animals and birds also. Coco like the mountain so much that 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 be a max value letter, and the subsequence should like this, a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an

2. It should have at least 3 elements, and in 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 letter is the ASCII value.

 

Given a letter sequence, Coco wants to know how many Mountain Subsequences exist.

输入

Input contains multiple test cases.

For each case 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 that the letter sequence only contain lowercase letters. 

输出

For each case please output the number of the mountain subsequences module 2012.

示例输入

4
abca

示例输出

4

提示

The 4 mountain subsequences are:

aba, aca, bca, abca

来源

 2013年山东省第四届ACM大学生程序设计竞赛
 1 #include <iostream>
 2 #include <cstring>
 3 #define maxn 100005
 4 #define mod 2012
 5 using namespace std;
 6 char str[maxn];
 7 int dp[30],dl[maxn],dr[maxn],s[maxn];
 8 int main()
 9 {
10     int n;
11     while(cin>>n)
12     {
13         cin>>str;
14         for(int i=0;i<n;i++)
15             s[i]=str[i]-'a';
16         memset(dp,0,sizeof(dp));
17         memset(dl,0,sizeof(dl));
18         memset(dr,0,sizeof(dr));
19         for(int i=0;i<n;i++)
20         {
21             for(int j=0;j<s[i];j++)
22                 dl[i]=(dl[i]+dp[j])%mod;//统计前i个满足小于(s[i])条件的个数
23             dp[s[i]]=(dp[s[i]]+dl[i]+1)%mod;//加上当前满足条件的个数为下一个做准备
24         }
25         memset(dp,0,sizeof(dp));
26         for(int i=n-1;i>=0;i--)
27         {
28             for(int j=0;j<s[i];j++)
29                 dr[i]=(dr[i]+dp[j])%mod;
30             dp[s[i]]=(dp[s[i]]+dr[i]+1)%mod;
31         }
32         int ans=0;
33         for(int i=0;i<n;i++)
34             ans=(ans+dl[i]*dr[i])%mod;
35         cout<<ans<<endl;
36     }
37     return 0;
38 }

 

posted on 2015-04-28 21:59  细雨微光  阅读(462)  评论(0编辑  收藏  举报