问题 E: Mountain Subsequences
作者:互联网
问题 E: Mountain Subsequences
时间限制: 1 Sec 内存限制: 128 MB提交 状态
题目描述
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 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.
输出
For each case please output the number of the mountain subsequences module 2012.
样例输入 Copy
4 abca
样例输出 Copy
4
提示
The 4 mountain subsequences are:
aba, aca, bca, abca
#include <cstring> #include <iostream> using namespace std; const int N = 100010, mod = 2012; int n; char a[N]; int s[N], f[N], d[N], dp[N]; int main(){ while (~scanf("%d%s", &n, &a)){ memset(f, 0, sizeof f); memset(d, 0, sizeof d); memset(dp, 0, sizeof dp); for (int i = 0; i < n; i ++ ) s[i] = a[i] - 'a'; for (int i = 0; i < n; i ++ ){ for (int j = 0; j < s[i]; j ++ ){ f[i] = (f[i] + dp[j]) % mod; } dp[s[i]] = (dp[s[i]] + f[i] + 1) % mod; } memset(dp, 0, sizeof dp); for (int i = n - 1; i >= 0; i -- ){ for (int j = 0; j < s[i]; j ++ ){ d[i] = (d[i] + dp[j]) % mod; } dp[s[i]] = (dp[s[i]] + d[i] + 1) % mod; } long long res = 0; for (int i = 0; i < n; i ++ ) res = (res + f[i]*d[i] % mod) % mod; printf("%lld\n", res); } return 0; }
标签:Mountain,int,问题,++,Subsequences,letter,dp,mod 来源: https://www.cnblogs.com/Iamcookieandyou/p/13656369.html