Mountain Subsequences

Problem Description

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

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. 

Output

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

Sample Input

4abca

Sample Output

4

Hint

The 4 mountain subsequences are:

aba, aca, bca, abca

Source

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


借鉴的网上的思路

#include <iostream>
#include<string.h>
using namespace std;
char str1[110000];
int str2[110000];
int dangqian[110000];
int youbian[110000];
int zuobian[110000];
int mod=2012;
int main()
{
    int n;
    while(cin>>n)
    {
        cin>>str1;
        for(int i=0;i<n;i++)
            str2[i]=str1[i]-'a';
        memset(dangqian,0,sizeof(dangqian));
        memset(zuobian,0,sizeof(zuobian));
         memset(youbian,0,sizeof(youbian));
        for(int i=0;i<n;i++)//对于s【i】为止的递增
        {
            for(int j=0;j<str2[i];j++) //遍历左右比s【i】小的,我们把他们累加即可
            {
                zuobian[i]=(zuobian[i]+dangqian[j])%mod;//当前是指每一个j现在有的,这个不是最终的,因为会随着变化
            }
            dangqian[str2[i]]=(dangqian[str2[i]]+zuobian[i]+1)%mod;//更改现在有的
        }
       memset(dangqian,0,sizeof(dangqian));
        for(int i=n-1;i>=0;i--)//对于s【i】为止的递增
        {
            for(int j=0;j<str2[i];j++)
            {
                youbian[i]=(youbian[i]+dangqian[j])%mod;
            }
            dangqian[str2[i]]=(dangqian[str2[i]]+youbian[i]+1)%mod;
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
           ans=(ans+youbian[i]*zuobian[i])%mod;
        }
        cout<<ans%mod<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Little_boy_z/article/details/80042029
今日推荐