ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

问题 E: Mountain Subsequences

2020-09-12 11:31:20  阅读:363  来源: 互联网

标签:Mountain int 问题 ++ Subsequences letter dp mod


问题 E: Mountain Subsequences

时间限制: 1 Sec  内存限制: 128 MB
提交 状态

题目描述

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.

 

样例输入 Copy

4
abca

样例输出 Copy

4

提示

 The 4 mountain subsequences are:

aba, aca, bca, abca

 

#include <cstring>
#include <iostream>

using namespace std;

const int N = 100010, mod = 2012;

int n;
char a[N];
int s[N], f[N], d[N], dp[N];

int main(){
    while (~scanf("%d%s", &n, &a)){
        memset(f, 0, sizeof f);
        memset(d, 0, sizeof d);
        memset(dp, 0, sizeof dp);
        
        
        for (int i = 0; i < n; i ++ ) s[i] = a[i] - 'a';
        for (int i = 0; i < n; i ++ ){
            for (int j = 0; j < s[i]; j ++ ){
                f[i] = (f[i] + dp[j]) % mod;
            }
            dp[s[i]] = (dp[s[i]] + f[i] + 1) % mod;
        }
        
        memset(dp, 0, sizeof dp);
        for (int i = n - 1; i >= 0; i -- ){
            for (int j = 0; j < s[i]; j ++ ){
                d[i] = (d[i] + dp[j]) % mod;
            }
            dp[s[i]] = (dp[s[i]] + d[i] + 1) % mod;   
        }
        
        long long res = 0;
        for (int i = 0; i < n; i ++ ) res = (res + f[i]*d[i] % mod) % mod;
        
        printf("%lld\n", res);
    }
    
    return 0;
}

  

标签:Mountain,int,问题,++,Subsequences,letter,dp,mod
来源: https://www.cnblogs.com/Iamcookieandyou/p/13656369.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有