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, 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)

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

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

 1 #include <iostream>
 2 #include <sstream>
 3 #include <ios>
 4 #include <iomanip>
 5 #include <functional>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <string>
 9 #include <list>
10 #include <queue>
11 #include <deque>
12 #include <stack>
13 #include <set>
14 #include <map>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cmath>
18 #include <cstring>
19 #include <climits>
20 #include <cctype>
21 using namespace std;
22 #define XINF INT_MAX
23 #define INF 0x3FFFFFFF
24 #define MP(X,Y) make_pair(X,Y)
25 #define PB(X) push_back(X)
26 #define REP(X,N) for(int X=0;X<N;X++)
27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
29 #define CLR(A,X) memset(A,X,sizeof(A))
30 #define IT iterator
31 typedef long long ll;
32 typedef pair<int,int> PII;
33 typedef vector<PII> VII;
34 typedef vector<int> VI;
35 #define MAXN 100010
36 int dp[1010];
37 int a[MAXN];
38 int dp1[MAXN],dp2[MAXN];
39 int main()
40 {
41     ios::sync_with_stdio(false);
42     int n;
43     char s;
44     while(cin>>n){
45         for(int i=0;i<n;i++){
46             cin>>s;
47             a[i]=s-‘a‘;
48         }
49         CLR(dp,0);
50         CLR(dp1,0);
51         for(int i=0;i<n;i++){
52             for(int j=0;j<a[i];j++) dp1[i]+=dp[j];
53             while(dp1[i]>=2012)dp1[i]-=2012;
54             dp[a[i]]+=dp1[i]+1;
55             while(dp[a[i]]>=2012)dp[a[i]]-=2012;
56         }
57         CLR(dp,0);
58         CLR(dp2,0);
59         for(int i=n-1;i>=0;i--){
60             for(int j=0;j<a[i];j++) dp2[i]+=dp[j];
61             while(dp2[i]>=2012)dp2[i]-=2012;
62             dp[a[i]]+=dp2[i]+1;
63             while(dp[a[i]]>=2012)dp[a[i]]-=2012;
64         }
65         int ans=0;
66         for(int i=0;i<n;i++){
67             ans=(ans+dp1[i]*dp2[i])%2012;
68         }
69         cout<<ans<<endl;
70     }
71     return 0;
72 }
时间: 2024-11-29 05:28:13

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

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec  Memory Limit: 128 MB Description In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence.

acm山东省赛 Games(dp取数)

Games Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description Alice and Bob are playing a stone game. There are nn piles of stones. In each turn, a player can remove some stones from a pile (the number must be positive and no

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

山东省第四届省赛 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 Mou

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

HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences                                  Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                             

第五届山东省赛总结

这次比赛在HITWH,10号我们早早的坐车来到了威海,然后随便逛了逛,吃了饭,就是热身赛. 热身赛题目比较坑爹,尤其是A题,要求区间素数个数,给的数据是10^7,我写了一个却WA了.后来得知有10^9的情况,感觉一下成了神题,我想了好久也没思路,结束后得知大于10^7的数全部当成10^7做,太坑.但这样都有人能AC,真是膜拜.B题是要交一个随机数,RP比较好,2A.C据说是概率DP,我不会也没仔细看. 之后是开幕式,完了以后我一个人回了宾馆,跟山大威海的同学吃了一顿,随后回去看了看电视就睡了.