第四届 Mountain Subsequences
来源:互联网 发布:京东和淘宝年销售额 编辑:程序博客网 时间:2024/05/22 13:20
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
4abcaSample Output
4Hint
The 4 mountain subsequences are: aba, aca, bca, abca
题目大意:
给你一个长度为n的字符串且仅由小写英文字母组成,求满足
a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an的子序列的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子序列的数目,要求该子序列最小长度为3,中心元素左右都至少有一个元素。
思路解析:
题目告诉我们输入串仅包含26个英文字母,这样我们只要枚举每一个位置,然后记录每个位置左边满足条件的个数,右边满足条件的个数,最后乘起来就是了。关键是我们如何统计左右两边满足的个数呢?因为有之前做过的求最长递增子序列的基础,所以可以应用DP的阶段划分的思想。那么,怎么划分阶段呢?我们定义数组dp[26], dl[maxn], dr[maxn], dl[i]表示第i个位置左边满足条件的个数,dr[i]表示第i个位置右边满足条件的个数,dp[c]表示以字符c结尾的满足情况的个数。我们可以发现,dp[s[i]] = (dl[i] + 1),s[i]= c;
1表示s[i]单独一个时满足的情况,dl[i]表示他左边的满足的各种情况加上s[i] 后满足的情况。
注意:在一个串中如果有相同的字母,那么他们的dp值是否相同呢?题目在要求该子串的时候用了
a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an 这样一个式子,每个元素都表示不同位置的元素,也就是说,在串
aaba中应该有aba,aba两个答案,
#include <iostream>
#include <string.h>
using namespace std;
const int mod=2012;
int dp[30],dl[100005],dr[100005],s[100005];
int main()
{
int n;
while(cin>>n)
{
string str;
cin>>str;
memset(dp,0,sizeof(dp));
memset(dl,0,sizeof(dl));
memset(dr,0,sizeof(dr));
for(int i=0;i<n;i++)
s[i]=str[i]-'a';
for(int i=0;i<n;i++)
{
for(int j=0;j<s[i];j++)
dl[i]=(dl[i]+dp[j])%mod;
dp[s[i]]=(dp[s[i]]+dl[i]+1)%mod;
}
memset(dp,0,sizeof(dp));
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<s[i];j++)
dr[i]=(dr[i]+dp[j])%mod;
dp[s[i]]=(dp[s[i]]+dr[i]+1)%mod;
}
int ans=0;
for(int i=0;i<n;i++)
ans=(ans+dl[i]*dr[i])%mod;
cout<<ans<<endl;
}
return 0;
}
- 第四届 Mountain Subsequences
- 山东省第四届ACM Mountain Subsequences 解题报告
- 山东省第四届ACM大学生程序设计竞赛 Mountain Subsequences dp
- 2013山东省第四届ACM省赛 Mountain Subsequences
- 山东省第四届ACM大学生程序设计竞赛Mountain Subsequences
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- 2013年山东省第四届ACM大学生程序设计竞赛——Mountain Subsequences
- 第四届 山东省ACM SDUT 2607 Mountain Subsequences(LIS+哈希 OR 线段树 待解决)
- sdutoj-2607-Mountain Subsequences
- Mountain Subsequences(dp)
- sdut 2607 Mountain Subsequences (dp)
- [ACM] SDUT 2607 Mountain Subsequences
- [dp] upcpj 2221 Mountain Subsequences
- sdut 2605 Mountain Subsequences(树状数组)
- SDUT Mountain Subsequences 2607 dp(字母表优化) 2013年山东acm省赛
- Hrbust 2051 Mountain Subsequences【dp+思维】【哈理工OJ 800题纪念】
- COLD MOUNTAIN
- Mountain climbing
- linux中链接脚本ld文件详解
- 王艾辉: 5.5日晚间现货黄金、现货白银操作策略(晚间点评)
- tcp/udp缓冲区
- 华为机试-拼音翻译成阿拉伯数字
- zXing横竖屏与界面修改详细步骤
- 第四届 Mountain Subsequences
- UVa1262 - Password(暴力枚举)
- base64加密
- Http详解
- POJ 1111 Image Perimeters
- Ros中2D_Slam说明: turtlebot+kinect改写成turtlebot+laser(hokuyo)
- IOS_修改TableView的删除按钮的文本
- Ubuntu 14.04 为 root 帐号开启 SSH 登录
- Java静态方法