The program Design Competition of ACM College students in Shandong province Mountain Subsequences DP

Source: Internet
Author: User
Mountain Subsequences Time limit:1000ms Memory limit:65536k Topic Description

Coco is a beautiful acmer girl living in a very beautiful. There are many trees and flowers on the mountain, and There are many and animals birds. Coco like the "Mountain so much," 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 being a max value letter, and the subsequence should as this, A1 ; ...< ai < ai+1 < Amax > AJ > Aj+1 > ... > an

2. It should have at least 3 elements, with 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 * is the ASCII value.

Given a letter sequence and Coco wants to know how many mountain. input Input contains multiple test cases.

For each case there are a number n (1<= n <= 100000) which means the length of the letter sequence in the And the next line contains the letter sequence.

Please note this letter sequence only contain lowercase letters. Output For each case, please output the number of the Mountain subsequences module 2012. Sample Input

4abca
Sample Output
4
TipsThe 4 mountain subsequences are:

ABA, ACA, BCA, ABCA source 2013 Shandong four ACM undergraduate Program design Contest Sample program


To find the number of convex sub sequences

I'm going to have to ask each point to the current number of ascending subsequence.

To find the number of descending subsequence of each point for the beginning to the end

The last multiplication is the answer.

Accode:

#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring&gt  
; #include <stdlib.h> #include <iostream> #include <algorithm> #define MAXN 100100 #define MOD 201  
2 using namespace Std;  
int LOW[MAXN],BIG[MAXN],A[MAXN],NUM[MAXN];  
Char MAPP[MAXN];  
    void Init () {memset (low,0,sizeof (Low));  
    memset (big,0,sizeof (big));  
memset (num,0,sizeof (num));  
    int main () {int n,ans;  
        while (~SCANF ("%d", &n)) {init ();  
        scanf ("%s", &mapp);  
        for (int i=0;i<n;++i) a[i]=mapp[i]-' a ';  
            for (int i=0;i<n;++i) {for (int j=0;j<a[i];++j) low[i]= (low[i]+num[j))%mod;  
        num[a[i]]= (num[a[i]]+low[i]+1)%mod; 
        }/*for (int i=0;i<n;++i) cout<<num[i]<< ';  
        cout<< ' \12 '; * * memset (num,0,sizeof (num)); for (int i=n-1;i>=0;--i) {for (int j=0;J&LT;A[I];++J) big[i]= (big[i]+num[j])%mod;  
        num[a[i]]= (num[a[i]]+big[i]+1)%mod; 
        }/* for (int i=0;i<n;++i) cout<<num[i]<< '; 
        cout<< ' \12 '; 
        for (int i=0;i<n;++i) cout<<low[i]<< ""; 
        cout<< ' \12 '; 
        for (int i=0;i<n;++i) cout<<big[i]<< ';  
        cout<< ' \12 '; * * ans=0;  
        for (int i=0;i<n;++i) ans= (ans+big[i]*low[i])%mod;  
    printf ("%d\n", ans);  
return 0;   }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.