codeforces 814 C An impassioned circulation of affection
来源:互联网 发布:湖南学历网络教育学校 编辑:程序博客网 时间:2024/05/21 01:30
Problem
codeforces.com/contest/814/problem/C
Reference
Markdown Syntax
MaHua
Meaning
给定一串长为 n 的只有小写字母的字符串,q 个询问。
对每一个询问,给出字母 c 和最多的使用次数 m,表示最多将原串中 m 个位置的字符替还为 c。
求替换后的串中,最长的全为 c 的子串的长度。
Analysis
因为是连续的,就考虑将邻近的只有 c 的片段连起来。
预初理出每个字母的片段,然后用尺取枚举将哪些片连起来。
注意一些细节的处理。
Code
#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int N = 1500;struct seq{ int st, ed; seq() {} seq(int a, int b): st(a), ed(b) {} int len() { return ed - st; }};char s[N+2];vector<seq> g[26];int solve(int x, int m, int n){ int res = min(m, n), tmp = 0; for(int l=0, r=0, add, e=g[x].size(); l<e; ) { if(l == r) { if(++r >= e) { res = max(res, g[x][l].len() + min(m, g[x][l].st + n-g[x][l].ed)); break; } if(m >= g[x][r].st - g[x][l].ed) { m -= g[x][r].st - g[x][l].ed; tmp = g[x][r].st - g[x][l].ed + g[x][l].len() + g[x][r].len(); ++r; } else { res = max(res, g[x][l].len() + min(m, g[x][l].st + n-g[x][l].ed)); ++l; } continue; } while(r < e && m >= g[x][r].st - g[x][r-1].ed) { m -= g[x][r].st - g[x][r-1].ed; tmp += g[x][r].st - g[x][r-1].ed + g[x][r].len(); ++r; } if(m > 0) add = min(m, g[x][l].st + n-g[x][r-1].ed); else add = 0; res = max(res, tmp + add); if(l + 1 < e) { if(l + 1 != r) { m += g[x][l+1].st - g[x][l].ed; tmp -= g[x][l+1].st - g[x][l].ed + g[x][l].len(); } ++l; } else break; } return res;}int main(){ int n, q; scanf("%d %s %d", &n, s, &q); for(int i=0, j; i<n; i=j) { for(j=i+1; j<n && s[j]==s[i]; ) ++j; g[s[i]-'a'].push_back(seq(i, j)); } for(int m; q--; ) { char c; scanf("%d %c", &m, &c); printf("%d\n", solve(c-'a', m, n)); } return 0;}
阅读全文
0 0
- codeforces C. An impassioned circulation of affection
- 【Codeforces 814 C. An impassioned circulation of affection】+ 尺取法
- codeforces 814C An impassioned circulation of affection
- codeforces 814C An impassioned circulation of affection
- codeforces 814 C An impassioned circulation of affection
- Codeforces 814C-An impassioned circulation of affection
- codeforces 814 C. An impassioned circulation of affection(暴力)
- Codeforces 814C An impassioned circulation of affection【Dp】
- codeforces 814C An impassioned circulation of affection
- Codeforces 814 C An impassioned circulation of affection
- Codeforces 814C An impassioned circulation of affection (dp)
- CodeForces-814C An impassioned circulation of affection
- Codeforces 814C An impassioned circulation of affection(dp)
- 814C An impassioned circulation of affection
- Codeforces#418C An impassioned circulation of affection
- Codeforces Round #418 C--An impassioned circulation of affection
- Codeforces#418 An impassioned circulation of affection
- An impassioned circulation of affection CodeForces
- H.265 对直播和点播平台的意义
- 云白条,做有温度的金融,帮助有困难的人
- Java NIO之FileChannel学习
- [JAVA学习笔记-88]DelayQueue实现Leader-Follower pattern
- OpenJudge 2.5-1756 八皇后【回溯算法】
- codeforces 814 C An impassioned circulation of affection
- 添加ubuntu升级库
- 学习php
- 实战json、html、jsx的互转
- 关于连续点击的处理、避免多次点击
- 语义分割--Attention to Scale: Scale-aware Semantic Image Segmentation
- java中内存中的存储栈和堆
- Android中BitmapFactory.Options详解
- javascript之DOM技术(一)