博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
山东省第四届省赛 E-Mountain Subsequences
阅读量:6703 次
发布时间:2019-06-25

本文共 2919 字,大约阅读时间需要 9 分钟。

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

4abca

Sample Output

4

Hint

The 4 mountain subsequences are:

aba, aca, bca, abca

 题意:

求满足以某元素为中心,左边递增右边递减的子串数目

Ps:最小长度为3,切中心元素左右至少各一个元素

Pps:aaba中,前两个a是不同的有a1ba、a2ba两个答案

题解:

求出每个字符为中心的左侧递增子序列和右侧递减子序列

然后左右相乘求和

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define is_lower(c) (c >= 'a' && c <= 'z')#define is_upper(c) (c >= 'A' && c <= 'Z')#define is_alpha(c) (is_lower(c) || is_upper(c))#define is_digit(c) (c >= '0' && c <= '9')#define min(a, b) ((a) < (b) ? (a) : (b))#define max(a, b) ((a) > (b) ? (a) : (b))#define PI acos(-1)#define IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0);#define For(i, a, b) for (int i = a; i <= b; i++)typedef long long ll;typedef unsigned long long ull;typedef pair
pii;typedef pair
pll;typedef vector
vi;const ll inf = 0x3f3f3f3f;const double EPS = 1e-10;const ll inf_ll = (ll)1e18;const ll maxn = 100005LL;const ll mod = 2012LL;const int N = 100000 + 5;int dp1[N],dp2[N],dp[30],s[N];char str[N];int main() { int n; while(cin >> n && n) { scanf("%s", str); For(i, 0, n-1) { s[i] = str[i] - 'a'; } memset(dp1 , 0, sizeof(dp1)); memset(dp2 , 0, sizeof(dp2)); For(i, 0, n-1) { For(j, 0, s[i]-1) { dp1[i] = (dp1[i] + dp[j]) % mod; } dp[s[i]] = (dp[s[i]] + dp1[i] + 1) % mod; } memset(dp, 0, sizeof(dp)); for(int i = n - 1; i >= 0; i--) { for(int j = 0; j < s[i]; j++) { dp2[i] = (dp2[i] + dp[j]) % mod; } dp[s[i]] = (dp[s[i]] + dp2[i] + 1) % mod; } ll res = 0; for(int i = 0; i < n; i++) { res = (res + dp1[i] * dp2[i]) % mod; } cout << res << endl; }}

 

转载于:https://www.cnblogs.com/GHzz/p/8629834.html

你可能感兴趣的文章
应用图片加载服务与第三方实现库的解耦
查看>>
高并发的核心技术-幂等的实现方案
查看>>
微波炉炖蛋
查看>>
C#调用C/C++ DLL 参数传递和回调函数的总结
查看>>
非spring组件servlet、filter、interceptor中注入spring bean
查看>>
SQL Server中SELECT会真的阻塞SELECT吗?
查看>>
class path and classloader
查看>>
文字检测与识别 资源
查看>>
外包筛选心得
查看>>
Warning: skipping non-radio button in group
查看>>
Android 悬浮窗权限校验
查看>>
mysql 创建数据库 并设置utf8格式
查看>>
IDA 逆向工程 反汇编使用
查看>>
CentOS7单独安装Apache Bench压力测试工具
查看>>
python植入后门backdoor程序的方法?
查看>>
WPF 使用 Direct2D1 画图 绘制基本图形
查看>>
导入其他python文件或者python文件的函数
查看>>
80端口被NT kernel & System 占用pid 4
查看>>
Multiverse in Doctor Strange // Multiverse在《神秘博士》
查看>>
ASP.NET MVC(Razor)上运用UEditor和xhEditor编辑器检测到有潜在危险的 Request.Form的真正解决办法...
查看>>