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大学生程序设计竞赛
 #include <iostream>
#include <cstring>
#define maxn 100005
#define mod 2012
using namespace std;
char str[maxn];
int dp[],dl[maxn],dr[maxn],s[maxn];
int main()
{
int n;
while(cin>>n)
{
cin>>str;
for(int i=;i<n;i++)
s[i]=str[i]-'a';
memset(dp,,sizeof(dp));
memset(dl,,sizeof(dl));
memset(dr,,sizeof(dr));
for(int i=;i<n;i++)
{
for(int j=;j<s[i];j++)
dl[i]=(dl[i]+dp[j])%mod;//统计前i个满足小于(s[i])条件的个数
dp[s[i]]=(dp[s[i]]+dl[i]+)%mod;//加上当前满足条件的个数为下一个做准备
}
memset(dp,,sizeof(dp));
for(int i=n-;i>=;i--)
{
for(int j=;j<s[i];j++)
dr[i]=(dr[i]+dp[j])%mod;
dp[s[i]]=(dp[s[i]]+dr[i]+)%mod;
}
int ans=;
for(int i=;i<n;i++)
ans=(ans+dl[i]*dr[i])%mod;
cout<<ans<<endl;
}
return ;
}
上一篇:优化checkbox和radio,类似Bootstrap中的iCheck


下一篇:2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server