Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
来源:互联网 发布:js调用电脑摄像头拍照 编辑:程序博客网 时间:2024/06/04 18:08
Problem 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
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
Example Input
4abca
Example Output
4
Hint
aba, aca, bca, abca
题意:给定长度为n的由小写字母组成的字符串,求有多少个字符串满足严格递增再递减,注意abba,那么 aba ,aba算两个不同的串,因为下标不一样。
思路:正着dp一遍,反着一遍就行了
#include<iostream>#include<cstdio>#include<cstring>#include<string>#define maxn 100010#define INF 0x3f3f3f3f#define mod 2012using namespace std;int dp[maxn],f[30],n; //f是装的是以字母'a'+i为结尾的子序列的个数;dp是以第i个字母结尾的递增子序列的个数;char s[maxn];int x;int main(){ while(scanf("%d",&n)!=EOF){ scanf("%s",s); memset(f,0,sizeof(f)); for(int i=0;i<n;i++){ //正着来一遍; x=s[i]-'a'; //提取数字; dp[i]=0; for(int j=0;j<x;j++){ dp[i]+=f[j]; dp[i]=dp[i]%mod; } f[x]+=(dp[i]+1); //+1是因为要加上s[i]单个字符; f[x]=f[x]%mod; } memset(f,0,sizeof(f)); long long sum=0,ans=0; for(int i=n-1;i>=0;i--){ //反着来一遍; x=s[i]-'a'; sum=0; for(int j=0;j<x;j++){ sum+=f[j]; sum=sum%mod; } f[x]=f[x]+sum+1; f[x]=f[x]%mod; ans=ans+sum*dp[i]; //组合,所以要乘; ans=ans%mod; } cout<<ans<<endl; //输出最好是用cout,它能自己调输出格式,不会出现输出错误; } return 0;}
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- 山东省第四届ACM大学生程序设计竞赛 Mountain Subsequences dp
- 山东省第四届ACM大学生程序设计竞赛Mountain Subsequences
- 2013年山东省第四届ACM大学生程序设计竞赛——Mountain Subsequences
- 第四届 山东省ACM大学生程序设计竞赛
- 2013年山东省第四届ACM大学生程序设计竞赛
- 山东省第四届ACM大学生程序设计竞赛 Rescue The Princess
- 山东省第四届ACM大学生程序设计竞赛 Alice and Bob
- 山东省第四届ACM大学生程序设计竞赛 Rescue The Princess
- 山东省第四届ACM Mountain Subsequences 解题报告
- 2013山东省第四届ACM省赛 Mountain Subsequences
- 2013年山东省第四届ACM大学生程序设计竞赛-Alice and Bob (找规律+二进制)
- 山东省第四届ACM大学生程序设计竞赛-Rescue The Princess(计算几何)
- 山东省第四届ACM大学生程序设计竞赛-Contest Print Server(模拟)
- 山东省第四届ACM大学生程序设计竞赛-Alice and Bob(二进制&&找规律)
- 山东省第四届ACM大学生程序设计竞赛-Boring Counting(划分树-二分查找)
- 山东省第四届ACM大学生程序设计竞赛 The number of steps(概率dp&全期望)
- 山东省第四届ACM大学生程序设计竞赛 Thrall’s Dream(单源强连通分量)
- JS HTML DOM(简介)
- 进程间通信--管道(半双工)
- 表目管理代码示例(迸发访问,多重索引查找,添加,删除,)
- < Kotlin > Android Studio3.0 Kotlin工程问题集
- POJ
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- php 微信公众号企业转账功能
- 如何在编译Python指定使用自定义安装的openssl
- POJ
- Python-string类学习
- Java的单例模式你真的懂了吗?
- Kafka学习总结(二)——Kafka设计原理
- 训练XGBoost的一些脚本,
- Codeforces 810B Summer sell-off 题解