转载请注明出处: 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, 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

题意:满足a[i]<a[i+1]<......<a[j]>a[j+1]>......a[k]的子序列有多少个(左边和右边的长度都至少为1)

分析:算出从左向右以当前字母结尾的上升子序列的个数和从右向左的。。。

然后再把左边和右边的乘一下就行

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
int dp[];
int a[MAXN];
int dp1[MAXN],dp2[MAXN];
int main()
{
ios::sync_with_stdio(false);
int n;
char s;
while(cin>>n){
for(int i=;i<n;i++){
cin>>s;
a[i]=s-'a';
}
CLR(dp,);
CLR(dp1,);
for(int i=;i<n;i++){
for(int j=;j<a[i];j++) dp1[i]+=dp[j];
while(dp1[i]>=)dp1[i]-=;
dp[a[i]]+=dp1[i]+;
while(dp[a[i]]>=)dp[a[i]]-=;
}
CLR(dp,);
CLR(dp2,);
for(int i=n-;i>=;i--){
for(int j=;j<a[i];j++) dp2[i]+=dp[j];
while(dp2[i]>=)dp2[i]-=;
dp[a[i]]+=dp2[i]+;
while(dp[a[i]]>=)dp[a[i]]-=;
}
int ans=;
for(int i=;i<n;i++){
ans=(ans+dp1[i]*dp2[i])%;
}
cout<<ans<<endl;
}
return ;
}

13年山东省赛 Mountain Subsequences(dp)的更多相关文章

  1. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  2. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  3. 2018ACM山东省赛 Games(dp取数)

    Games Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description Alice and Bob ...

  4. 2013年山东省赛F题 Mountain Subsequences

    2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...

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

    Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...

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

    Description Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees ...

  7. sdutoj 2607 Mountain Subsequences

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...

  8. Triangle (第8届山东省赛的某题)

    triangle(第8届山东省赛的某题) 传送门 题意:喵了个呜,这题意真是峰回路转啊.懒死了,不想描述. 做法:我们拿set或线段树维护exp的最小值,每次取出exp值最小的边,删除之.并更新这条边 ...

  9. HEX SDUT 3896 17年山东省赛D题

    HEX SDUT 3896 17年山东省赛D题这个题是从矩形的左下角走到右上角的方案数的变形题,看来我对以前做过的题理解还不是太深,或者是忘了.对于这种题目,直接分析它的性质就完事了.从(1,1)走到 ...

随机推荐

  1. $.fn、$.fn.extend和$.extend的区别

    $.fn $.fn是指jquery的命名空间,加在fn上的方法及属性,会对jquery实例每一个有效. 如:扩展$.fn.abc(),即$.fn.abc()是对jquery扩展了一个abc方法,那么后 ...

  2. 火狐的bug

    初次启动火狐的界面并且默认是最大化的情况下,第一个业签时会发现火狐的浏览器无法达到下边框,请看图 途中可以看到,body区域没有填充满浏览器可用区域.但是当浏览器已经启动页签,现在是第二个页签时,则不 ...

  3. keil c51编译器的一些使用心得

    现在的存储器已经不像七八年前那样昂贵了,但是ram相对于rom和eeprom的价格还是不可同样看待的,所以程序中节省内存在现在看来还是非常关键的.原因有以下几点: 1.ram的存取速度相对于eepro ...

  4. 《Programming WPF》翻译 第7章 7.我们进行到哪里了?

    原文:<Programming WPF>翻译 第7章 7.我们进行到哪里了? WPF提供了一个范围的高质量生成和合成服务.一组形状元素支持各种的绘图基础.一些笔刷类型是可利用的,对于决定如 ...

  5. 我的Android4.3新书即将上市,谢谢大家的支持

    首先感谢清华大学.电子工业.机械工业.人民邮电等各大出版社对本书的肯定.我想说中国的IT业如果没有你们的辛勤工作,是不会发展得这么快的.经过再三权衡,本书将选择人民邮电出版社于近几个月在全国出版发行. ...

  6. mutate 转换

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat geoip.conf input {stdin {} } filter { geoip { ...

  7. Compound class names are not supported. Consider searching for one class name and filtering the results

    原文地址:http://stackoverflow.com/questions/20361643/compound-class-names-are-not-supported-consider-sea ...

  8. Django学习笔记(精简版)

    dos:安装:python setup.py install 环境变量:D:\Python27;D:\Python27\Lib\site-packages\django\bin; 转到创建的目录:dj ...

  9. ECharts JavaScript图表库 ECharts

    ECharts开源来自百度商业前端数据可视化团队,基于html5 Canvas,是一个纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽重计算.数据视图.值 ...

  10. 人物角色群体攻击判定(三)Physics.OverlapSphere(群体攻击)

    使用Physics.OverlapSphere来检测不方便调试, 其他都可以.   核心代码: //检测敌人 public void CheckEnemy() { Collider[] cols = ...