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. fortran中如何提供计算程序运行时间?

    如下: Real time_begin , time_end1 , time_end2 Integer i , j call CPU_TIME(time_begin) write(*,*) time_ ...

  2. 由验证码和session丢失的引发原因

    今天中午,突然完整验证码全部不能显示了,查看gd库是没问题的,然后发现网站登录不上,追查变天,无果. 然后两个小时过后,可能是网站压力小了,网站又恢复正常. 但是过一阵又来. 其实很明显不是代码的问题 ...

  3. 关于优化sql查询的一个方法。

    select * from gmvcsbase.base_file file,gmvcsbase.base_user user,gmvcsbase.base_department dep,gmvcsb ...

  4. This application failed to start because it could not find or load the Qt platform plugin "xcb".

    1.  copy      libQt5DBus.so.5 2.  add    QT_PLUGIN_PATH blog.csdn.net/windows_nt/article/details/242 ...

  5. 注册、启动、停止windows服务

    找到本机InstallUtil.exe命令 命令行下注册服务InstallUtil.exe D:\XXXXService.exe 启动服务 net start XXXXService 停止服务net ...

  6. Linux QtCreator设置mingw编译器生成windows程序

    Qt跨平台,那必须在Linux平台编译一个可以在windows下运行的Qt程序才行,当然还得和QtCreator环境弄在一起才行. 工作环境:Centos 7 yum install qt5-qt* ...

  7. php---PHP setcookie()

    定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...

  8. IOS测试程序运行耗时

    iOS设备相对于电脑,内存和处理能力有限,所以一段代码或者程序运行的时间需要时刻注意,这里提供两种获取精确时间的方法. 方法一:使用系统时间 NSDate* tmpStartData = [[NSDa ...

  9. ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式

    原文:ArcGIS API for Silverlight 地图元素点闪烁,线流动显示的处理方式 <Grid x:Name="LayoutRoot" Background=& ...

  10. 更改Magento的base url

    Magento的Base URL是用于访问商店页面的URL,您也可以为单独一个store view设置一个Base Url.在改这项值之前请确保您的域名已经指向了网站所在服务器的IP,DNS解析完成后 ...