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

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607

Mountain Subsequences

Time Limit: 1000MS Memory Limit: 65536KB
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.
Example Input
4abca
Example Output
4
Hint
The 4 mountain subsequences are:

aba, aca, bca, abca

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

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607


题目大意:

给你一个长度为n的字符串仅由小写英文字母组成,求满足

a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an

的子串的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子串的数目,要求该子串
最小长度为3,中心元素左右都至少有一个元素。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100005;
const int mod = 2012;
char s[maxn];
int a[maxn], dp1[maxn], dp2[maxn], num[maxn];
//dp1[i]表示字符串1到i中以s[i]为结尾的递增子序列个数
//dp2[i]表示字符串i到n中以s[i]为结尾的递减子序列个数
int main()
{
    int n;
    while(cin >> n)
    {
        scanf("%s", s+1);
        memset(dp1, 0, sizeof(dp1));
        memset(dp2, 0, sizeof(dp2));
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
            a[i] = s[i] - 'a';
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < a[i]; j++)
                dp1[i] = (dp1[i] + num[j]) % mod;
            //num[j]表示当前以s[j+'a']结尾的递增子序列个数
            num[a[i]] = (num[a[i]] + dp1[i] + 1) % mod;
            //当前以a[i]结尾的递增子序列加上因为此次a[i]出现而
            //新增的递增子序列个数,+1是因为a[j]自己也是一个递增的子序列
        }
        memset(num, 0, sizeof(num));
        for(int i = n; i >= 1; i--)
        {
            for(int j = 0; j < a[i]; j++)
                dp2[i] = (dp2[i] + num[j]) % mod;
            num[a[i]] = (num[a[i]] + dp2[i] + 1) % mod;
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            ans = (ans + dp1[i]*dp2[i]) % mod;
        cout << ans << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值