山东省第四届省赛 E-Mountain Subsequences
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
4
abca
Sample Output
4
HINT
The 4 mountain subsequences are:
aba, aca, bca, abca
题意:
给你一个长度为n的字符串仅由小写英文字母组成,求满足
a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an
的子串的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子串的数目,要求该子串
最小长度为3,中心元素左右都至少有一个元素。
思路:
对于每一个字符,求出其左侧递增的序列个数,以及右侧递减的序列个数,然后相乘即可。
举个例子来说:
abca 对于a来说左侧没有递增的序列,所以为0,右侧不用计算了。然后对b来说左侧递增序列只有1个,右侧递减序列也只有1个,那么以b为中心的满足条件的个数为1个。
接下来对于c来说,左侧递增序列为3个,右侧递减序列为1个,那么以c为中心的有3个。最后的a右侧没有递减的,所以为0;所以此样例结果为1+3=4;
求每个字符的左侧递增和右侧递减实际上是一个相反的过程,只需要求出一个即可,具体实现过程看代码,代码中num数组是用来记录字母出现的个数,low和high数组分别记录左侧递增和右侧递减的序列的个数。
代码:
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set> #define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
typedef long long LL;
const long long inf = 0x3f3f3f3f;
const long long mod = ;
const double PI = acos(-1.0);
const double wyth=(sqrt()+)/2.0;
const int maxn = +;
const char week[][]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const char month[][]= {"Janurary","February","March","April","May","June","July",
"August","September","October","November","December"
};
const int daym[][] = {{, , , , , , , , , , , , },
{, , , , , , , , , , , , }
};
const int dir4[][] = {{, }, {, }, {-, }, {, -}};
const int dir8[][] = {{, }, {, }, {-, }, {, -}, {, }, {-, -}, {, -}, {-, }};
using namespace std;
int num[maxn];
int low[maxn];
int high[maxn];
int a[maxn];
int main()
{
int n;
while(cin>>n)
{
memset(num,,sizeof(num));
memset(low,,sizeof(low));
memset(high,,sizeof(high));
char s;
for(int i=; i<n; i++)
{
cin>>s;
a[i]=s-'a';
}
for(int i=; i<n; i++)
{
for(int j=; j<a[i]; j++)
low[i]=(low[i]+num[j])%mod;
num[a[i]]=(num[a[i]]+low[i]+)%mod;
}
memset(num,,sizeof(num));
for(int i=n-; i>=; i--)
{
for(int j=; j<a[i]; j++)
high[i]=(high[i]+num[j])%mod;
num[a[i]]=(num[a[i]]+high[i]+)%mod;
}
LL ans=;
for(int i=; i<n; i++)
ans=(ans+high[i]*low[i])%mod;
cout<<ans<<endl;
}
}
山东省第四届省赛 E-Mountain Subsequences的更多相关文章
- 一场刺激的游戏——很文艺的山东省第四届ACM赛总结(菜鸟版)
人生就像一个个节点,节点中或许有成功,失败,满足,遗憾,但是只要它是不可复制的,在日后,便是美好. ...
- 2013年山东省赛F题 Mountain Subsequences
2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...
- 13年山东省赛 Mountain Subsequences(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mountain Subsequences Time Limit: 1 Sec ...
- 山东省第四届ACM省赛
排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...
- sdutoj 2607 Mountain Subsequences
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...
- 山东省第四届ACM大学生程序设计竞赛解题报告(部分)
2013年"浪潮杯"山东省第四届ACM大学生程序设计竞赛排名:http://acm.upc.edu.cn/ranklist/ 一.第J题坑爹大水题,模拟一下就行了 J:Contes ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server
点击打开链接 2226: Contest Print Server Time Limit: 1 Sec Memory Limit: 128 MB Submit: 53 Solved: 18 [Su ...
随机推荐
- 分享一个彻底冻结对象的函数——来自阮一峰老师的《ECMAScript 6 入门》
var constantize = (obj) => { Object.freeze(obj); Object.keys(obj).forEach( (key, i) => { if ( ...
- Java 里快如闪电的线程间通讯
这个故事源自一个很简单的想法:创建一个对开发人员友好的.简单轻量的线程间通讯框架,完全不用锁.同步器.信号量.等待和通知,在Java里开发一个轻量.无锁的线程内通讯框架:并且也没有队列.消息.事件或任 ...
- JavaScript定义类的几种方式
提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...
- 超酷动态图片展示墙JS特效制作方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 那些让 Web 开发者们深感意外的事情
作为 Web 开发者,对自己的行业前景,人人都有自己的看法,然而,任何行业都有出人意料的地方.著名的 Web 开发设计博客 Nope.com 曾向他们的读者做了一个调查,请他们列举 Web 开发领域那 ...
- 通过jquery.validate.js校验表单字段是否合法
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- javaScript语法和风格的检查工具
一.JSLint. JSHint. JSCS. ESLint 1.JSLint是由Douglas Crockford开发的,可能是最早的JavaScript Lint工具.JSLint定义了一组编码约 ...
- React 16 源码瞎几把解读 【二】 react组件的解析过程
一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...
- 自动化运维工具SaltStack详细部署【转】
==========================================================================================一.基础介绍==== ...