转载请注明出处: 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. 【android】android中activity的启动模式

    在AndroidManifest.xml中配置 <activity android:label="第二个应用" android:name=".Demo2Activi ...

  2. 35个jQuery小技巧(转)

    1. 禁止右键点击$(document).ready(function(){    $(document).bind("contextmenu",function(e){     ...

  3. Uva220 Othello

     Othello  Othello is a game played by two people on an 8 x 8 board, using disks that are white on on ...

  4. 观《Terminal》之感

    读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html.        经人推荐,用了几天时间欣赏了这部斯皮尔伯格导演的电影<Te ...

  5. 转载收藏之用 - 微信公众平台开发教程(六):了解MessageHandler

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK>我们讲述了如何使用Senparc.Weixin.MP SDK ...

  6. Android使用xml中定义的动画效果

    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...

  7. qt之窗口换肤(一个qss的坑:当类属性发现变化时需要重置qss,使用rcc资源文件)

    1.相关文章 Qt 资源系统qt的moc,uic,rcc命令的使用 2.概要    毕业两年了,一直使用的是qt界面库来开发程序,使用过vs08.10.13等开发工具,并安装了qt的插件,最近在做客户 ...

  8. 《Programming WPF》翻译 第7章 3.笔刷和钢笔

    原文:<Programming WPF>翻译 第7章 3.笔刷和钢笔 为了在屏幕上绘制一个图形,WPF需要知道你想要为图形填充什么颜色以及如何绘制它的边框.WPF提供了一些Brush类型支 ...

  9. GridView视图

    本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...

  10. Udp实现简单的聊天程序

    在<UDP通讯协议>这篇文章中,简单的说明了Udp协议特征及如何Udp协议传输数据 这里将用Udp协议技术,编写一个简单的聊天程序: //发送端: package com.shindo.j ...