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

  1. 4
  2. abca

Sample Output

  1. 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数组分别记录左侧递增和右侧递减的序列的个数。

代码:

  1. #include <bits/stdc++.h>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <queue>
  10. #include <stack>
  11. #include <map>
  12. #include <set>
  13.  
  14. #define IO ios::sync_with_stdio(false);\
  15. cin.tie();\
  16. cout.tie();
  17. typedef long long LL;
  18. const long long inf = 0x3f3f3f3f;
  19. const long long mod = ;
  20. const double PI = acos(-1.0);
  21. const double wyth=(sqrt()+)/2.0;
  22. const int maxn = +;
  23. const char week[][]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
  24. const char month[][]= {"Janurary","February","March","April","May","June","July",
  25. "August","September","October","November","December"
  26. };
  27. const int daym[][] = {{, , , , , , , , , , , , },
  28. {, , , , , , , , , , , , }
  29. };
  30. const int dir4[][] = {{, }, {, }, {-, }, {, -}};
  31. const int dir8[][] = {{, }, {, }, {-, }, {, -}, {, }, {-, -}, {, -}, {-, }};
  32. using namespace std;
  33. int num[maxn];
  34. int low[maxn];
  35. int high[maxn];
  36. int a[maxn];
  37. int main()
  38. {
  39. int n;
  40. while(cin>>n)
  41. {
  42. memset(num,,sizeof(num));
  43. memset(low,,sizeof(low));
  44. memset(high,,sizeof(high));
  45. char s;
  46. for(int i=; i<n; i++)
  47. {
  48. cin>>s;
  49. a[i]=s-'a';
  50. }
  51. for(int i=; i<n; i++)
  52. {
  53. for(int j=; j<a[i]; j++)
  54. low[i]=(low[i]+num[j])%mod;
  55. num[a[i]]=(num[a[i]]+low[i]+)%mod;
  56. }
  57. memset(num,,sizeof(num));
  58. for(int i=n-; i>=; i--)
  59. {
  60. for(int j=; j<a[i]; j++)
  61. high[i]=(high[i]+num[j])%mod;
  62. num[a[i]]=(num[a[i]]+high[i]+)%mod;
  63. }
  64. LL ans=;
  65. for(int i=; i<n; i++)
  66. ans=(ans+high[i]*low[i])%mod;
  67. cout<<ans<<endl;
  68. }
  69. }

山东省第四届省赛 E-Mountain Subsequences的更多相关文章

  1. 一场刺激的游戏——很文艺的山东省第四届ACM赛总结(菜鸟版)

               人生就像一个个节点,节点中或许有成功,失败,满足,遗憾,但是只要它是不可复制的,在日后,便是美好.                                         ...

  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. 13年山东省赛 Mountain Subsequences(dp)

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

  5. 山东省第四届ACM省赛

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

  6. sdutoj 2607 Mountain Subsequences

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

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

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

  8. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  9. 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server

    点击打开链接 2226: Contest Print Server Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 53  Solved: 18 [Su ...

随机推荐

  1. Let's Encrypt 免费通配 https 签名证书 安装方法2 ,安卓签名无法认证!

    Let's Encrypt 免费通配 https 签名证书 安装方法 按照上文 配置完毕后你会发现 在pc浏览器中正常访问,在手机浏览器中无法认证 你只需要安装一个或多个中级证书 1.查看Nginx ...

  2. 【leetcode 简单】第十六题 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: ...

  3. Vmware安装ubuntu详细教程

    1.环境准备: (1) 范例系统为WIN10 64位家庭普通版,已正确安装虚拟机VMware Workstation 12 Pro.(2) 下载Ubuntu系统. 2.安装过程: 2.1 VMware ...

  4. thinkphp 5.0 代码执行漏洞

    https://github.com/vulhub/vulhub/blob/master/thinkphp/5-rce docker-compose -f /home/root/compose.yml ...

  5. .NET Framework 4安装失败

    #刚装系统遇到之前所遇到的问题.之前因为这个事情还被困扰了好一阵子.特此写出来分享给大家. 环境:WIN10 企业版 在使用一些需要较高.net版本的时候无法更新.你可以试一下.在服务里面开启再更新. ...

  6. 16 - 文件操作-StringIO-BytesIO

    目录 1 文件操作 1.1 open函数介绍 1.2 打开操作 1.2.1 mode模式 1.2.2 文件指针 1.2.3 缓冲区 1.2.4 encoding编码 1.2.5 其他参数 1.3 读写 ...

  7. webconfig的配置解析

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  8. java中8种数据类型和默认值所占字节数

    java 8种基本数据类型的默认值及所占字节数 通过一段代码来测试一下 8种基本数据类型的默认值 1 package dierge; 2 3 public class Ceshi { 4 int a; ...

  9. Mysql自带profiling性能分析工具使用分享

    1. show variables like '%profiling%';(查看profiling信息)       2. set profiling=1;(开启profiling)   3. 执行S ...

  10. [HTML]增加input标签的multiple属性上传的文件数

    .发现问题 <input type="file" name="myfile[]" multiple="multiple"/> 最 ...