山东省第四届省赛 E-Mountain Subsequences

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

题意:

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

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

的子串的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子串的数目,要求该子串

最小长度为3,中心元素左右都至少有一个元素。

思路:

对于每一个字符,求出其左侧递增的序列个数,以及右侧递减的序列个数,然后相乘即可。

举个例子来说:

abca  对于a来说左侧没有递增的序列,所以为0,右侧不用计算了。然后对b来说左侧递增序列只有1个,右侧递减序列也只有1个,那么以b为中心的满足条件的个数为1个。

接下来对于c来说,左侧递增序列为3个,右侧递减序列为1个,那么以c为中心的有3个。最后的a右侧没有递减的,所以为0;所以此样例结果为1+3=4;

求每个字符的左侧递增和右侧递减实际上是一个相反的过程,只需要求出一个即可,具体实现过程看代码,代码中num数组是用来记录字母出现的个数,low和high数组分别记录左侧递增和右侧递减的序列的个数。

代码:

#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define IO ios::sync_with_stdio(false);\
    cin.tie(0);    cout.tie(0);
typedef long long LL;
const long long inf = 0x3f3f3f3f;
const long long mod = 2012;
const double PI = acos(-1.0);
const double wyth=(sqrt(5)+1)/2.0;
const int maxn = 100000+1000;
const char week[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const char month[12][10]= {"Janurary","February","March","April","May","June","July",
                           "August","September","October","November","December"
                          };
const int daym[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
using namespace std;
int num[maxn];
int low[maxn];
int high[maxn];
int a[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        memset(num,0,sizeof(num));
        memset(low,0,sizeof(low));
        memset(high,0,sizeof(high));
        char s;
        for(int i=0; i<n; i++)
        {
            cin>>s;
            a[i]=s-‘a‘;
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<a[i]; j++)
                low[i]=(low[i]+num[j])%mod;
            num[a[i]]=(num[a[i]]+low[i]+1)%mod;
        }
        memset(num,0,sizeof(num));
        for(int i=n-1; i>=0; i--)
        {
            for(int j=0; j<a[i]; j++)
                high[i]=(high[i]+num[j])%mod;
            num[a[i]]=(num[a[i]]+high[i]+1)%mod;
        }
        LL ans=0;
        for(int i=0; i<n; i++)
            ans=(ans+high[i]*low[i])%mod;
        cout<<ans<<endl;
    }
}

原文地址:https://www.cnblogs.com/aiguona/p/8663112.html

时间: 2024-08-29 23:41:31

山东省第四届省赛 E-Mountain Subsequences的相关文章

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

Mountain Subsequences 题目描述 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 s

13年山东省赛 Mountain Subsequences(dp)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec  Memory Limit: 128 MB Description Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees and flowers on the mountain,

Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 Alice and Bob Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*....

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 1 <= n <= 10^6 0 <= A, K, a, b <= 10^9 1 <= m, P <= 10^9 本题目的关键在于大幂的分解和..你要这样想,因

sdutoj Mountain Subsequences

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequences Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees and

[ACM] SDUT 2607 Mountain Subsequences

Mountain Subsequences Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 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 lik

山东省第五届省赛总结

省赛终于过去了.感觉准备最充分的一次比赛比的最差,心里很伤心. 省赛之前,目标一直很明确,拿个金牌,向冠军冲刺.没想到最后拿了一个银牌.真的是讽刺. 热身赛的时候,一开始机器很搓.根本运行不了程序.然后就找来技术人员.技术人员搞了半个小时,也没搞出来个毛.最后还是机器自己莫名其妙的能用了. 然后我就上手敲B的随机题.没想到两次就过了,太开心了.然后专心看c题.一开始看出来的做法不对.后来离结束还有一点点的时候,终于想起来了正解.然后跟 scf一说,scf接着上手一敲.结果很对.然后果断交,然后就

Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1). Then Alice as