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

 题意:

求满足以某元素为中心,左边递增右边递减的子串数目

Ps:最小长度为3,切中心元素左右至少各一个元素

Pps:aaba中,前两个a是不同的有a1ba、a2ba两个答案

题解:

求出每个字符为中心的左侧递增子序列和右侧递减子序列

然后左右相乘求和

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include <set>
#include 
#include <string>
#include 
using namespace std;
#define is_lower(c) (c >= 'a' && c <= 'z')
#define is_upper(c) (c >= 'A' && c <= 'Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c >= '0' && c <= '9')
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define PI acos(-1)
#define IO                 \
  ios::sync_with_stdio(0); \
  cin.tie(0);              \
  cout.tie(0);
#define For(i, a, b) for (int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair pll;
typedef vector<int> vi;
const ll inf = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll inf_ll = (ll)1e18;
const ll maxn = 100005LL;
const ll mod = 2012LL;
const int N = 100000 + 5;
int dp1[N],dp2[N],dp[30],s[N];
char str[N];
int main() {
    int n;
    while(cin >> n && n) {
        scanf("%s", str);
        For(i, 0, n-1) {
            s[i] = str[i] - 'a';
        }
        memset(dp1 , 0, sizeof(dp1));
        memset(dp2 , 0, sizeof(dp2));
        For(i, 0, n-1) {
             For(j, 0, s[i]-1) {
                 dp1[i] = (dp1[i] + dp[j]) % mod;
             }
             dp[s[i]] = (dp[s[i]] + dp1[i] + 1) % mod;
        }
        memset(dp, 0, sizeof(dp));
        for(int i = n - 1; i >= 0; i--) {
            for(int j = 0; j < s[i]; j++) {
                dp2[i] = (dp2[i] + dp[j]) % mod;
            }
            dp[s[i]] = (dp[s[i]] + dp2[i] + 1) % mod;
        }
        ll res = 0;
        for(int i = 0; i < n; i++) {
            res = (res + dp1[i] * dp2[i]) % mod;
        }
        cout << res << endl;
    }
}

 

转载于:https://www.cnblogs.com/GHzcx/p/8629834.html

你可能感兴趣的:(山东省第四届省赛 E-Mountain Subsequences)