Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection 预处理

Linux笔记 最全的Linux笔记 阅读详情

C. An impassioned circulation of affection
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!

Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomityof the garland.

For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.

But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.

Input

The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.

The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.

The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.

The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.

Output

Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

Examples
input
6
koyomi
3
1 o
4 o
4 m
output
3
6
5
input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
output
3
4
5
7
8
1
2
3
4
5
input
10
aaaaaaaaaa
2
10 b
10 z
output
10
10
Note

In the first sample, there are three plans:

  • In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3is the best achievable;
  • In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
  • In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.

#include<bits/stdc++.h>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define MEM(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
#define lson l,m,node<<1
#define rson m+1,r,node<<1|1
char c;
char ch[2000];
int ans[100][2000];
int need[100][2000];
int main()
{	
	int n,i,j,q,m,k;
	cin>>n;
	cin>>ch+1;
	for(i=0;i<26;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(ch[j]=='a'+i)
			need[i][j]=need[i][j-1];
			else
			need[i][j]=need[i][j-1]+1;
		}
		for(j=1;j<=n;j++)
		{
			for(k=1;k<=j;k++)
			{
				int now=need[i][j]-need[i][k-1];
				int len=j-k+1;
				ans[i][now]=max(ans[i][now],len);
			}
		}
		for(j=1;j<=n;j++)
		{
			ans[i][j]=max(ans[i][j],ans[i][j-1]);
		}
	}
	cin>>q;
	while(q--)
	{
		scanf("%d %c",&m,&c);
		printf("%d\n",ans[c-'a'][m]);
	}
	return 0;
 }



最速下降法、牛顿法、拟牛顿法,Python实现高维二次目标函数优化 原理什么的就不说了,只有代码。。。 十元二次目标函数,求极小值点。 1.最速下降法 import random import numpy as np import matplotlib.pyplot as plt from sympy import * def goldsteinsearch(f, df, d, x, alpham, rho, t): flag = 0 a = 0 b = alpham fk = f(x) gk = df(x) 阅读详情

相关推荐

Vectorless dynamic动态功耗分析——Voltus

power 计算工具会根据设计信息和用户提供的activity和switching约束来生成虚拟的worst-case的power vector,难点在于哪些instances视为switching,在没有仿真的前提下如何确定开关的时间,使用timing文件(TWF)

qq_44347276的博客 2790

codeforces 814C An impassioned circulation of affection

DP

依然一笑作春温。 536

CMMI AIM 人工智能成熟度框架:31个过程域的157项AI实践集成

CMMI AIM人工智能成熟度框架为企业提供了系统化管理AI项目全生命周期的解决方案,包含31个过程域的157项AI实践。该框架通过四个演进阶段(混沌与风险、结构与凝聚、验证与信任、价值与增长),帮助组织建立可持续的AI能力成长路径,降低项目失败率并提升商业价值。

weixin_42613501的博客 351

Extension Writing Part II: Parameters, Arrays, and ZVALs

http://devzone.zend.com/317/extension-writing-part-ii-parameters-arrays-and-zvals/#Heading1 Introduction Accepting Values The ZVAL Creating ZVALs Arrays Symbol Tables as Arrays Refe

空灵游侠 10万+

[个人自用,全网最优]EDEM-FLUENT个人自学教程,拒绝手忙脚乱无用功!

[个人自用,全网最优]EDEM-FLUENT个人自学教程,拒绝手忙脚乱无用功!

zheaa的博客 4827

ROS机器人未知环境自主探索功能包explore_lite最全源码详细解析(二)

本系列文章主要针对ROS机器人常使用的未知环境自主探索功能包explore_lite展开全源码的详细解析,并进行概括总结。 本系列文章共包含六篇文章,前五篇文章主要介绍explore_lite功能包中 explore.cpp、costmap_tools.h、frontier_search.cpp、costmap_client.cpp等源码实现文件中全部函数的详细介绍,第六篇文章进行概括总结,包含自主探索功能包explore_lite的简介,实现未知环境自主探索功能的原理及流程总结,效果演示,使用功能包exp

慕羽★的博客 1648

signature=45841f81ba52c91c59e42a0a13fa2fe5,ALUMINA HAVING A COMPLEX PORE STRUCTURE, AND CATALYST AND...

CROSS REFERENCE OF RELATED APPLICATIONSThe present application claims the benefit of the Chinese Patent Applications No. 200610029961.0, filed on Aug. 11, 2006; 200610029963.x, filed on Aug. 11, 2006;...

weixin_42300435的博客 2万+

【QNX+Android虚拟化方案】129 - USB眼图参数配置

【QNX+Android虚拟化方案】129 - USB眼图参数配置

|~~~热爱生活、努力学习的小伙汁~~~| 821

Anatomy of Nvidia's Voxel Cone Tracing Code (VXGI)

Here is diffuse cone tracing code. 1 // 2 Generated by Microsoft (R) HLSL Shader Compiler 10.0.10011.16384 3 Buffer Definitions: 4 cbuffer AbstractTracingCB 5 { 6 struct Vx...

weixin_34409741的博客 1万+

Multica 自托管部署与使用入门:从上线到让 AI Agent 真正干活

Multica是一个将AI Agent作为团队成员的任务协作平台,支持自托管部署。本文介绍了完整的单机部署流程: 核心架构包括前端界面、后端服务、PostgreSQL数据库和Runtime执行机器 推荐使用Docker Compose部署,需配置数据持久化目录 提供详细的docker-compose.yml和.env配置文件说明 重点注意事项:数据库密码和JWT密钥必须修改,生产环境配置必须设为production 需正确配置前端访问地址和WebSocket连接 部署完成后,用户可创建AI Agent并将其

q742971636的博客 1703

电路设计仿真软件:Mentor Graphics_(11).MentorGraphics软件操作与技巧

/ 定义自定义仿真命令// 加载设计文件// 运行仿真// 主函数return 1;return 0;// 自定义设计检查// 获取设计中的所有模块// 遍历所有模块// 检查模块名称是否符合规范if (!// 检查模块中的所有引脚if (!// 主函数main() {通过本节的学习,您应该已经掌握了如何在 Mentor Graphics 中进行二次开发,包括使用 Tcl、C++ 和 Python 进行基本和高级操作。

kkchenjj的博客 1638

Codeforces Round #418 (Div. 2) -- C. An impassioned circulation of affection(DP预处理)

题意: 给你一个字符串, 告诉你喜欢的字母,和改变字母的次数,求在次数内 在长连续喜欢字母的长度? 思路: 直接令dp[i][j] 表示改变i 次,喜欢字母为j 的最大长度。 直接n*n*26预处理即可。 #include using namespace std; const int maxn = 1500 + 10; char s[maxn]; int dp[maxn][30]

aozil_yang的博客 157

【C2000系列DSP的Bootloader详解】如何打包源文件为库文件且加密仅授权使用,一步步教你如何实现

如果想要在 TI 的 CCS 开发环境中为 C2000 系列 MCU 生成 lib 库文件,并对这些库文件进行加密保护,确保只有授权方(持有密钥)才能调用和使用,以此保护源代码的知识产权。

昔时扬尘处 177

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection (DP)

题目链接 题解:简单DP, dp[ch][ i ][ j ] 表示到第 i 位为止填了 j 个 ch 字母空位的最长连续长度,如果用 ans[ch][i] 表示 ch 字母至多填 i 次空位的最大连续长度,则 dp 数组可以省去一维。 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef...

ttoobne 374

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(尺取法)

//https://codeforces.com/contest/814/problem/B //C. An impassioned circulation of affection #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 100; vector&...

八百标兵奔北坡的博客 288

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection

C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Nadeko's ...

aocan6909的博客 89

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

C. An impassioned circulation of affection(DP+two points)Codeforces Round #418 (Div. 2)

题目链接:http://codeforces.com/contest/814/problem/C C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

bamen50833的专栏 160

Codeforces Round #418 (Div. 2) 814 C. An impassioned circulation of affection

题目大意给你一个长度为n的字符串S, 只包含小写字母, q个请求, 每个请求由m c构成, 问改变字符串中的m个字母, 求只包含字母c的最长连续子串长度是多少解题思路设sum[ch][i] := S前i个字母包含字符ch的个数, 预处理出这个, 可以快速求出任意区间内某字母的个数 dp[ch][i] := 改变i个字符, 最长的只包含ch的子串的长度 对于区间[i, j], 字母k, 有dp[k

litmxs的博客 709

CF Round #418( Div.2) An impassioned circulation of affection

CF Round #418( Div.2) An impassioned circulation of affection Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!

Dust_heart的博客 707

Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(尺取)

题意:给你一个长度为n的字符串,q次询问m ch,每次问可最多改变m个字符,问最多连续多少个ch。 n 思路:尺取法预处理记录出每个字母,改变m次最长连续。 代码: #include using namespace std; typedef long long ll; const int maxn = 1505; char str[maxn]; int n, dp[30]

CillyB的博客 551

CF-Codeforces Round #418 (Div. 2)-C-An impassioned circulation of affection

ACM模版描述 题解对于这种海量查询的问题,不用多想,99 成需要预处理,而常见的预处理手段除了线段树、树状数组、RMQ 之类,还有 DP 等等,这里用的就是 DP 预处理。设 dp[i][j]dp[i][j] 表示对于字符 ii 进行 jj 次修改的最优解,初始化 dp[i][j]=jdp[i][j] = j,因为不管序列如何,我们都可以肯定的是经过 j 次修改至少有 j 个连续的字符 ii。其

逐梦者 557

【Codeforces Round 418】An impassioned circulation of affection DP

C. An impassioned circulation of affection time limit per test 2 seco...

weixin_30802171的博客 68

cf round #418 div2 C. An impassioned circulation of affection(暴力)

题目链接:http://codeforces.com/contest/814/problem/C 分析:设t[c][k]为连出长度为k的c串所需要的最小代价,暴力处理处t,然后二分找出小于等于m的最大值即可,复杂度为O(n^2+qlogn)。 1 #include<iostream> 2 #include<cstdio> 3 #include&l...

weixin_30412013的博客 76

Codeforces Round #418 (Div. 2)C.An impassioned circulation of affection(暴力枚举)

C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Nadeko's birthda

WhaleFall 378
上一篇: TIANKENG’s restaurant(Ⅱ) hdu 4886 思维暴力
下一篇: qduoj 河神的难题 思维 实验室腊新呐 枚举
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值