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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值