http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607

Mountain Subsequences

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.

示例输入

4
abca

示例输出

4

提示

The 4 mountain subsequences are:

aba, aca, bca, abca

来源

 2013年山东省第四届ACM大学生程序设计竞赛

示例程序

分析:

给你一个长度为n的字符串仅由小写英文字母组成,求满足

a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an

的子串的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子串的数目,要求该子串

最小长度为3,中心元素左右都至少有一个元素。

题目告诉我们输入串仅包含26个英文字母,这样我们只要枚举每一个位置,然后记录每个位置左边满足

条件的个数,右边满足条件的个数,最后乘起来就是了。关键是我们如何统计左右两边满足的个数呢?

本能的反应告诉我可以用DP来做,主要还是因为有之前做过的求最长递增子序列的基础。可是,

老师告诉我说,这不叫DP,看代码会发现,其实就是个递推求解关系式。我总是觉得,之所以能想到

这里,完全是应用了DP的阶段划分的思想,暂且不论也罢。那么,怎么划分阶段呢?我们定义

数组dp[26],dl[maxn],dr[maxn],dp[c]表示以字符c结尾的满足情况的个数,dl[i]表示第i个位置左边满足

条件的个数,dr[i]表示第i个位置右边满足条件的个数。当然,我们可以发现,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两个答案,

而不是只有aba一个。因为前面两个子串形式上看虽然相同,但纠其本质,a和a的来源不同,

可以表示为a1ba和a2ba两个答案。

AC代码:

 #include <iostream>
#include <cstring>
#define maxn 100005
#define mod 2012
using namespace std; char str[maxn];
int dp[],dl[maxn],dr[maxn],s[maxn];
int main()
{
int n;
while(cin>>n)
{
cin>>str;
for(int i=;i<n;i++)
s[i]=str[i]-'a';
memset(dp,,sizeof(dp));
memset(dl,,sizeof(dl));
memset(dr,,sizeof(dr));
for(int i=;i<n;i++)
{
for(int j=;j<s[i];j++)
dl[i]=(dl[i]+dp[j])%mod;
dp[s[i]]=(dp[s[i]]+dl[i]+)%mod;
}
memset(dp,,sizeof(dp));
for(int i=n-;i>=;i--)
{
for(int j=;j<s[i];j++)
dr[i]=(dr[i]+dp[j])%mod;
dp[s[i]]=(dp[s[i]]+dr[i]+)%mod;
}
int ans=;
for(int i=;i<n;i++)
ans=(ans+dl[i]*dr[i])%mod;
cout<<ans<<endl;
}
return ;
}

官方代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = +;
int n;
char s[maxn];
int dl[maxn], dr[maxn], dp[], cnt[];
const int mod = ;
int main() {
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
while(~scanf("%d", &n)) {
scanf("%s", s);
for(int i = ; i < n; i++) s[i] -= 'a';
memset(dp, , sizeof(dp));
memset(cnt, , sizeof(cnt));
memset(dl, , sizeof(dl));
memset(dr, , sizeof(dr));
for(int i = ; i < n; i++) {
for(int j = ; j < s[i]; j++) {
dl[i] += dp[j] + cnt[j];
dl[i] %= ;
}
cnt[s[i]] ++;
cnt[s[i]] %= ;
dp[s[i]] += dl[i];
dp[s[i]] %= ; } memset(dp, , sizeof(dp));
memset(cnt, , sizeof(cnt)); for(int i = n - ; i >= ; i-- ) {
for(int j = ; j < s[i]; j ++) {
dr[i] += dp[j] + cnt[j];
dr[i] %= ;
}
cnt[s[i]] ++;
cnt[s[i]] %= ;
dp[s[i]] += dr[i];
dp[s[i]] %= ;
}
int ans = ;
for(int i = ; i < n; i++) {
ans += dl[i] * dr[i] ;
ans %= ;
}
printf("%d\n", ans);
}
}

官方数据生成代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = +;
int n;
char s[maxn];
int dl[maxn], dr[maxn], dp[], cnt[];
const int mod = ;
int main() {
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
while(~scanf("%d", &n)) {
scanf("%s", s);
for(int i = ; i < n; i++) s[i] -= 'a';
memset(dp, , sizeof(dp));
memset(cnt, , sizeof(cnt));
memset(dl, , sizeof(dl));
memset(dr, , sizeof(dr));
for(int i = ; i < n; i++) {
for(int j = ; j < s[i]; j++) {
dl[i] += dp[j] + cnt[j];
dl[i] %= ;
}
cnt[s[i]] ++;
cnt[s[i]] %= ;
dp[s[i]] += dl[i];
dp[s[i]] %= ; } memset(dp, , sizeof(dp));
memset(cnt, , sizeof(cnt)); for(int i = n - ; i >= ; i-- ) {
for(int j = ; j < s[i]; j ++) {
dr[i] += dp[j] + cnt[j];
dr[i] %= ;
}
cnt[s[i]] ++;
cnt[s[i]] %= ;
dp[s[i]] += dr[i];
dp[s[i]] %= ;
}
int ans = ;
for(int i = ; i < n; i++) {
ans += dl[i] * dr[i] ;
ans %= ;
}
printf("%d\n", ans);
}
}

sdutoj 2607 Mountain Subsequences的更多相关文章

  1. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  2. 2013年山东省赛F题 Mountain Subsequences

    2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...

  3. sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

    Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...

  4. 山东省第四届省赛 E-Mountain Subsequences

    Description Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees ...

  5. 山东省第四届ACM省赛

    排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...

  6. 2013山东省ICPC结题报告

    A.Rescue The Princess 已知一个等边三角形的两个顶点A.B,求第三个顶点C,A.B.C成逆时针方向. 常规的解题思路就是用已知的两个点列出x,y方程,但这样求出方程的解的表达式比较 ...

  7. 山东省第四届ACM大学生程序设计竞赛解题报告(部分)

    2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...

  8. 山东省第四届ACM程序设计竞赛部分题解

    A : Rescue The Princess 题意: 给你平面上的两个点A,B,求点C使得A,B,C逆时针成等边三角形. 思路: http://www.cnblogs.com/E-star/arch ...

  9. [ACM]2013山东省“浪潮杯”省赛 解题报告

    题目地址:http://acm.upc.edu.cn/problemset.php?page=13  2217~2226 A.Rescue The Princess 一个等边三角形告诉前2个点,求逆时 ...

随机推荐

  1. ManualResetEvent和AutoResetEvent的区别实例

    ManualResetEvent和AutoResetEvent的作用可以理解为在线程执行中插入停顿点flag终止程序运行,然后通过设置flag的状态来使得程序继续运行. 两者的区别是:ManualRe ...

  2. mysql慢查询

    查看当前服务器是否开启慢查询: 1.快速办法,运行sql语句show VARIABLES like "%slow%" 2.直接去my.conf中查看. my.conf中的配置(放在 ...

  3. iOS archive(归档)的总结 (序列化和反序列化,持久化到文件)

    http://www.cnblogs.com/ios8/p/ios-archive.html

  4. CSS弹性盒布局

    <html> <head> <meta charset="utf-8"/> <title></title> <st ...

  5. IIS出现HTTP500.24错误

    IIS配置完成后,新建网站,访问时出现如下错误: 解决方法:设置应用池为经典模式(classic)如下: 设置完成后重新打开网站即可.

  6. JQuery直接调用asp.net后台WebMethod方法

    利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法.[WebMethod]   命名空间 1.无参数的方法调用, 注意:1.方法一定要静态方法,而且要有[WebMethod]的 ...

  7. AX中四种库存ABC分析法原理研究

    库存ABC分类,简单的说就是抓大放小,是为了让我们抓住重点,用最大精力来管理最重要的物料,而对于不太重要的物料则可以用较少的精力进行管理.它和我们平常说的八二法则有异曲同工之妙. 既然要应用库存ABC ...

  8. Linux中挂载移动硬盘的方法

    - fdisk -l 找出移动硬盘对应的设备 - mkdir 创建挂载点 mkdir /mnt/usb - mount mount -t ntfs-3g /dev/sdb1  /mnt/usb - u ...

  9. Linux就这个范儿 第9章 特种文件系统

    Linux就这个范儿 第9章 特种文件系统 http://book.douban.com/reading/32081222/ P326 有一种文件系统,根本不在磁盘上.这种文件系统就是大名顶顶的ram ...

  10. centos linux安全和调优 第四十一节课

    centos  linux安全和调优    第四十一节课 上半节课 Linux安全 下半节课 Linux调优 2015-07-01linux安全和调优 [复制链接]--http://www.apele ...