Hrbust 2051 Mountain Subsequences【dp+思维】【哈理工OJ 800题纪念】

351 篇文章 2 订阅

Mountain Subsequences
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 25(6 users)Total Accepted: 7(5 users)Rating: Special Judge: No
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
4
abca
Sample Output
4
Hint

The 4 mountain subsequences are:

aba, aca, bca, abca

Source
HCPC2014校赛训练赛 2

题目大意(窝只会刷水啊):

给你一个长度为N的字符串(只包含小写字母),让你找一共有多少个子序列,是山形的。


思路:


1、计数问题,考虑dp;

①设定dp【26】表示dp到当前位子以第i个字母结尾的子序列的个数。

②设定dppre【i】表示以a【i】结尾的递增子序列个数。

③设定dpback【i】表示以a【i】结尾的逆序递增子序列个数。

那么不难想到,此时ans=dppre【i】*dpback【i】;


2、那么接下来考虑状态转移方程:

①对于dppre【i】:dppre【i】+=dp【j】(字母j小于字母a【i】);(解决了暴力O(n^2)处理会超时的情况)

如果上述状态转移方程我们暴力去写,就是dppre【i】+=dppre【j】(a【j】<a【i】);进一步递推得到Σdppre【j】==Σdp【j】;

②对于dp【i】:dp【i】=dppre【i】+1;

对于dpback【i】;逆序同理。


3、别忘了取摸。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
#define mod 2012
ll dp[30];
ll dppre[100500];
ll dpback[100500];
char a[100500];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        ll output=0;
        memset(dppre,0,sizeof(dppre));
        memset(dp,0,sizeof(dp));
        memset(dpback,0,sizeof(dpback));
        scanf("%s",a);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<26;j++)
            {
                if(a[i]-'a'>j)
                {
                    dppre[i]+=dp[j];
                    dppre[i]%=mod;
                }
                else break;
            }
            dp[a[i]-'a']+=dppre[i]+1;
            dp[a[i]-'a']%=mod;
        }
        memset(dp,0,sizeof(dp));
        for(int i=n-1;i>=0;i--)
        {
            for(int j=0;j<26;j++)
            {
                if(a[i]-'a'>j)
                {
                    dpback[i]+=dp[j];
                    dpback[i]%=mod;
                }
                else break;
            }
            dp[a[i]-'a']+=dpback[i]+1;
            dp[a[i]-'a']%=mod;
        }
        for(int i=0;i<n;i++)
        {
            output=(output+dppre[i]*dpback[i])%mod;
        }
        printf("%lld\n",output);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值