转载请注明出处: 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. windows应用中调用DLL一步步试验

    试验环境: PC:win10 build 10143 IDE: vs2015 RC WinPhone: win10 build 10136 简单界面,点按钮,算加法 一.主程用C++ 1.新建visu ...

  2. Struts2中的get、set方法作用:

    Struts2中的get.set方法作用: 在Struts2中,客户端和服务器之间的数据传输全部要用到get.set方法:用set方法 ,可以将表单中的值存入Action类.通过Struts2.0标签 ...

  3. 使用json常用到的包有以下六个

    使用json常用到的包有以下六个 1. commons-logging-1.0.4.jar 2. commons-lang-2.3.jar 3. commons-collections-3.2.jar ...

  4. 为Mac自带的Apache配置PHP和虚拟机

    操作系统:os x 10.11.2 1.启动apache 打开终端(terminal),输入命令:sudo apachectl -k start ; 在浏览器地址栏中输入:http://localho ...

  5. 5W1H分析法

    "5W1H分析法"也叫"六何分析法",它是一种分析方法也可以说是一种创造技法.是对选定的项目.工序和操作,都要从原因(Why).对象(What).地点(Wher ...

  6. Static, Shared Dynamic and Loadable Linux Libraries

    转载:http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html Why libraries are used: Th ...

  7. 一条执行4秒的sql语句导致的系统问题 (转)

    为了一看究竟,抓取了一个awr报告.发现系统的负载情况确实很严重,每秒的redo有1.6M,可见系统的负载不是主要在select上,可能有一些dml之类的操作极为频繁. 看了下等待事件.都是关于loc ...

  8. Best Meeting Point 解答

    Question A group of two or more people wants to meet and minimize the total travel distance. You are ...

  9. Spiral Matrix 解答

    Question Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ...

  10. easyhadoop初识以及各种问题

    版本:easyhadoop 1.2.1,操作系统:Centos 6.4: 首先,我要说的是要用对版本,这点很重要.我使用ubuntu12.04 64bit 的虚拟机是不行的,安装直接报错.所以就下载了 ...